How do I configure dynamic weaving using EclipseLink & Spring?

◇◆丶佛笑我妖孽 提交于 2019-12-04 02:21:18

For weaving with Spring I believe you need something like,

<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
    <property name="defaultDataSource" ref="dataSource" />
    <property name="dataSources">
        <map>
            <entry>
                <key>
                    <value>jdbc/__default</value>
                </key>
                <ref bean="dataSource" />
            </entry>
            <entry>
                <key>
                    <value>jdbc/jta</value>
                </key>
                <ref bean="dataSource" />
            </entry>
        </map>
    </property>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>

There is some information here,

http://wiki.eclipse.org/EclipseLink/Examples/JPA/JPASpring

Ensure you are not accessing your classes before accessing the Spring context.

Another solution is to use static weaving.

Now, I am also using EclipseLink 2.2 and Spring 3.1 as below. My configuration is a little different with yours. There is no loadTimeWeaver configuration in your eclipseLinkEntityManagerFactory. I think, it will be ok if you use as below.

Please, try as below with your Oracle DB information..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:faces="http://www.springframework.org/schema/faces"
       xmlns:int-security="http://www.springframework.org/schema/integration/security"
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:sec="http://www.springframework.org/schema/security"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/integration/security http://www.springframework.org/schema/integration/security/spring-integration-security-2.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/faces http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:annotation-config/>    
    <context:component-scan base-package="your-package"/>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="your-driver"/>
        <property name="url" value="your-database-url"/>
        <property name="username" value="your-username"/>
        <property name="password" value="your-passowrd"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!--        <property name="dataSource" ref="dataSource"/>-->
        <property name="persistenceUnitName" value="your-persistence-unit-name"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaDialect">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/>
        </property>
        <property name="jpaPropertyMap">
            <props>
                <prop key="eclipselink.weaving">false</prop>
            </props>
        </property>

        <property name="loadTimeWeaver">
            <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver">
            </bean>
        </property>
    </bean>

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="databasePlatform" value="org.eclipse.persistence.platform.database.OraclePlatform" />
        <property name="generateDdl" value="false"/>
        <property name="showSql" value="true"/>
    </bean>
</beans>
Zuf

copy org.springframework.instrument-3.1.0.M2.jar into tomcat/lib directory and try it again.

Considering the instrumentation for unit tests, one can add it as VM argument to the maven-surefire-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <argLine>-javaagent:lib/test/spring-instrument-4.0.2.RELEASE.jar</argLine>
    </configuration>
</plugin>

assuming spring-instrument jar was placed at corresponding location.

(For that, the following dependency could be added temporarily to the project pom.xml

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <scope>test</scope>
</dependency>

and then after the build, the corresponding artifact copied from local .m2 directory to project's lib/test directory.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!