静态编织eclipselink jpa与spring和tomcat

时间:2015-02-12 12:09:50

标签: java spring spring-mvc jpa eclipselink

我正在尝试使用maven实现Eclipselink + Spring + JPA的静态编织。我已经使用Java Configuration配置了我的spring项目,没有context.xml。我试过以下事情:

  1. 在persistence.xml中:
  2. 添加了以下编织规范。

    <property name="eclipselink.weaving" value="static" />
    
    1. 在DBConfig.java中,我已经配置了bean
    2. 如下:

       @Bean()
          public DataSource getDataSource() {
              BoneCPDataSource ds = new BoneCPDataSource();
              log.debug("Driver Name : " + driverClassName);
              try {
                  Class.forName(driverClassName);
              } catch (ClassNotFoundException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              log.debug("DB Url : " + url);
              ds.setDriverClass(driverClassName);
              ds.setJdbcUrl(url);
              ds.setUsername(username);
              ds.setPassword(password);
              ds.setMaxConnectionsPerPartition(5);
              ds.setMinConnectionsPerPartition(2);
              ds.setAcquireIncrement(2);
              ds.setDefaultAutoCommit(false);     
              Properties props = new Properties();
              props.put("eclipselink.weaving", "static");
              try {
                  ds.setProperties(props);
              } catch (Exception e) {         
                  e.printStackTrace();
              }
              return ds;
          }
      
          @Bean(name = "em")
          public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean() {
              LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
              em.setDataSource(getDataSource());      
              em.setJpaDialect(jpaDialect());
              em.setPackagesToScan("com.cdl.hp50.model", "com.cdl.hp50.model.person");
              em.setPersistenceUnitName(persistenceUnitName);
              DatabasePlatform dp = new MySQLPlatform();
              em.setJpaVendorAdapter(getEclipseLinkJpaVendorAdapter());       
              return em;
          }
      
      1. 在pom.xml中,我按照eclipselink documentation上的说明添加了以下插件:
      2. 如下:

                      <plugin>
                            <groupId>de.empulse.eclipselink</groupId>
                            <artifactId>staticweave-maven-plugin</artifactId>
                            <version>1.0.0</version>
                            <executions>
                                <execution>
                                    <phase>process-classes</phase>
                                    <goals>
                                        <goal>weave</goal>
                                    </goals>
                                    <configuration>
                                        <persistenceXMLLocation>META-INF/persistence.xml</persistenceXMLLocation>
                                        <logLevel>FINE</logLevel>
                                    </configuration>
                                </execution>
                            </executions>
                            <dependencies>
                                <dependency>
                                    <groupId>org.eclipse.persistence</groupId>
                                    <artifactId>org.eclipse.persistence.jpa</artifactId>
                                    <version>${eclipselink.version}</version>
                                </dependency>
                            </dependencies>
                        </plugin>
        

        我明确指出要使用静态(构建时间)编织。当我使用maven install创建war时,persistence.xml中列出的实体已成功编织。但是当我将war部署到tomcat时,它会给出以下异常:

        org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'em' defined in class com.cdl.hp50.config.DBConfig: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
            at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
            at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
            at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
            at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
            at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4992)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
            at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
            at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
            at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
            at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1245)
            at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1895)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
            at java.util.concurrent.FutureTask.run(FutureTask.java:262)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:745)
        Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified
            at org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.addTransformer(SpringPersistenceUnitInfo.java:80)
            at org.eclipse.persistence.jpa.PersistenceProvider.createContainerEntityManagerFactory(PersistenceProvider.java:348)
            at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:341)
            at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1627)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1564)
            ... 25 more
        

        我尝试将eclipselink.weaving设为false。但它仍然给出同样的错误。我不知道为什么要找LoadTimeWeaver。请告诉我在这里我缺少的东西。

        更新

        我上传的初学者项目包括eclipselink,spring mvc,spring data jpa,sitemesh on github

2 个答案:

答案 0 :(得分:2)

编辑:我认为我发现了您的问题

   props.put("eclipselink.weaving", "static");
    try {
        ds.setProperties(props);

您是否尝试将eclipselink属性应用于数据源?如果您检查我的示例context.xml(从下面的应用程序形式),则需要将jpaPropertyMap应用于LocalContainerEntityManagerFactoryBean。

因此,您应该将代码更改为

 em.setjpaPropertyMap(props)

旧回答:

首先,对于这样的问题,一个示例项目将非常好。好吧,我为你做了,可以在https://github.com/baumgartner/eclipselink-static-weaving

找到它

坏消息:我无法重现你的问题,我的应用程序在tomcat 7上工作正常。

如果你的.class文件被static-weaving-plugin扩展了吗?被编码的文件是否在tomcat文件夹中? tomcat wtpwebapps-folder中的文件是否正确? (有时同步失败) 您是否使用JAD(java反编译器)来反编译您的类并检查它们是否得到了增强?增强的实体看起来像这样:

public class asdf.User implements java.lang.Cloneable, org.eclipse.persistence.internal.weaving.PersistenceWeaved, org.eclipse.persistence.internal.descriptors.PersistenceEntity, org.eclipse.persistence.internal.descriptors.PersistenceObject, org.eclipse.persistence.queries.FetchGroupTracker, org.eclipse.persistence.internal.weaving.PersistenceWeavedFetchGroups, org.eclipse.persistence.descriptors.changetracking.ChangeTracker, org.eclipse.persistence.internal.weaving.PersistenceWeavedChangeTracking, org.eclipse.persistence.internal.weaving.PersistenceWeavedRest {

如果部署战争,您的应用是否有效?如果您从IDE运行您的应用程序是否有效?

答案 1 :(得分:1)

我正在尝试使用maven实现Eclipselink + Spring + JPA的静态编织。一切都很好,除了获取类型永远不会工作懒惰。最后,我发现这个article的解决方案对我来说非常合适 解决方案是在我的maven pom.xml中添加一个插件。

<plugin>
    <artifactId>eclipselink-staticweave-maven-plugin</artifactId>
    <groupId>au.com.alderaan</groupId>
    <version>1.0.4</version>
    <executions>
        <execution>
            <goals>
                <goal>weave</goal>
            </goals>
            <phase>process-classes</phase>
            <configuration>
                <logLevel>ALL</logLevel>
            </configuration>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>${eclipselink.version}</version>
        </dependency>
    </dependencies>
</plugin>  

EclipseLink also建议 我对静态编织的唯一问题是,即使你愿意,也无法获得渴望。因此,我没有使用编织而是batch or join fetch