Java 如何使用EclipseLink&;配置动态编织;春天

Java 如何使用EclipseLink&;配置动态编织;春天,java,spring,tomcat,jpa,eclipselink,Java,Spring,Tomcat,Jpa,Eclipselink,如何使用EclipseLink和Spring配置动态编织?现在,我正试图通过Junit测试来实现这一点,但稍后我将不得不让它与Tomcat一起工作(我的部门在这方面已经标准化了大约10年) 我遇到了两个主要问题: 1。Spring需要一个加载时间编织器: 例外情况: java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.TestConte

如何使用EclipseLink和Spring配置动态编织?现在,我正试图通过Junit测试来实现这一点,但稍后我将不得不让它与Tomcat一起工作(我的部门在这方面已经标准化了大约10年)

我遇到了两个主要问题:

1。Spring需要一个加载时间编织器:

例外情况:

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:181)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
<snip>
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.weaving.AspectJWeavingEnabler#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
<snip>
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadTimeWeaver': Initialization of bean failed; nested exception is java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:527)
<snip>
Caused by: java.lang.IllegalStateException: ClassLoader [sun.misc.Launcher$AppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar
    at org.springframework.context.weaving.DefaultContextLoadTimeWeaver.setBeanClassLoader(DefaultContextLoadTimeWeaver.java:91)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1437)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1408)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    ... 44 more
2。Eclipselink对Spring仪表不满意:

错误:

(similar error messages repeated many times)

[EL Severe]: 2011-06-20 16:12:57.951--ServerSession(979781372)--Local Exception Stack: 
Exception [EclipseLink-0] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions: 
---------------------------------------------------------

Exception [EclipseLink-60] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The method [_persistence_set_shellType_vh] or [_persistence_get_shellType_vh] is not defined in the object [ps.entity.JudicialArtifactFamily].
Internal Exception: java.lang.NoSuchMethodException: ps.entity.JudicialArtifactFamily._persistence_get_shellType_vh()
Mapping: org.eclipse.persistence.mappings.ManyToOneMapping[shellType]
Descriptor: RelationalDescriptor(ps.entity.JudicialArtifactFamily --> [DatabaseTable(JUDICIAL_ARTIFACT_FAMILY)])

Exception [EclipseLink-218] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: A NullPointerException would have occurred accessing a non-existent weaved _vh_ method [_persistence_get_shellType_vh].  The class was not weaved properly - for EE deployments, check the module order in the application.xml deployment descriptor and verify that the module containing the persistence unit is ahead of any other module that uses it.
我试图通过使用EclipseLink weaver解决这个问题:

-javaagent:/dev/tools/javaagents/eclipselink.jar
但问题1也有同样的例外

库版本

我正在使用:

  • Java 1.6.0_24
  • 日食2.2.0.v20110202-r8913
  • 弹簧3.1.0平方米
配置文件

这是我的persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             version="2.0"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="eclipseLinkHashPU" transaction-type="RESOURCE_LOCAL">
        <class>ps.entity.Artifact</class>
        <class>ps.entity.JudicialArtifactFamily</class>
        <class>ps.entity.ShellType</class>
        <class>ps.entity.SourceArtifact</class>
        <class>ps.entity.Health</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>    
            <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
            <property name="eclipselink.ddl-generation.output-mode" value="database"/>
        </properties>
    </persistence-unit>
</persistence>

ps.entity.Artifact
ps.entity.JudicialArtifactFamily
ps.entity.ShellType
ps.entity.SourceArtifact
私人实体健康
假的
以下是my application.xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxrs="http://cxf.apache.org/schema/jaxrs" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://cxf.apache.org/schema/jaxrs
           http://cxf.apache.org/schema/jaxrs.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd          
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">

    <context:component-scan base-package="ps">
    </context:component-scan>

    <context:load-time-weaver/>

    <alias alias="entityManagerFactory" name="eclipseLinkEntityManagerFactory"/>

    <bean id="eclipseLinkEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:eclipselink-persistence.xml" />
        <property name="persistenceUnitName" value="eclipseLinkHashPU" />
        <property name="jpaPropertyMap">
            <map>
                <entry key="eclipselink.logging.level" value="Severe"/>
                <entry key="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.OraclePlatform"/>
                <entry key="javax.persistence.nonJtaDataSource" value-ref="hash1DataSource"/>
                <!-- it's not good that I'm turning weaving off -->
                <!-- <entry key="eclipselink.weaving" value="false"/> -->
            </map>
        </property>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
                <property name="databasePlatform" value="oracle" />
                <property name="database" value="ORACLE" />
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <!-- datasources, etc -->
</beans>

用弹簧编织我相信你需要这样的东西

<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>

jdbc/_默认值
jdbc/jta
这里有一些信息

在访问Spring上下文之前,请确保您没有访问类


另一个解决方案是使用静态编织。

org.springframework.instrument-3.1.0.M2.jar
复制到
tomcat/lib
目录中,然后重试。

现在,我也在使用
EclipseLink 2.2
Spring 3.1
,如下所示。我的配置与你的略有不同。 您的
EclipseLineKentityManager工厂中没有
loadTimeWeaver
配置。我想,如果你用下面的方法就可以了

请使用您的
Oracle DB
信息尝试以下操作

<?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>

假的

考虑到单元测试的工具,可以将其作为VM参数添加到maven surefire插件中

<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>

org.apache.maven.plugins
maven surefire插件
-javaagent:lib/test/spring-instrument-4.0.2.RELEASE.jar
假设弹簧仪器罐放置在相应位置

(为此,可以将以下依赖项临时添加到项目pom.xml中

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

org.springframework
弹簧乐器
测试

然后在构建之后,相应的工件从local.m2目录复制到project的lib/test目录。)

Hi@James“确保在访问Spring上下文之前您没有访问您的类。”如何实现这一点,因为我面临着同样的问题。请提出建议。到2017年有解决方案吗?
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-instrument</artifactId>
    <scope>test</scope>
</dependency>