将spring-data-jpa 1.9.4升级到1.10.2后出现问题

时间:2016-09-01 11:02:47

标签: java spring jpa spring-data spring-data-jpa

我在wildfly服务器中将spring-data-jpa从1.9.4升级到1.10.2后面临一些bean创建问题。以下是我的异常堆栈跟踪

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jpaContext': Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.Set]: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#1': Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: Could not convert factory method argument value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: Failed to convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: no matching editors or conversion strategy found; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#1': Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: Could not convert factory method argument value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: Failed to convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: no matching editors or conversion strategy found
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.orm.jpa.SharedEntityManagerCreator#1': Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: Could not convert factory method argument value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: Failed to convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]; nested exception is java.lang.IllegalStateException: Cannot convert value of type [org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory] to required type [javax.persistence.EntityManagerFactory]: no matching editors or conversion strategy found
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
... 28 more

以下是我的@Configuration for EntityManagerFactory和Spring Data Jpa Repository

@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
public class TransactionConfiguration {

@Bean
public JndiObjectFactoryBean dataSource() throws IllegalArgumentException {
    final JndiObjectFactoryBean dataSource = new JndiObjectFactoryBean();
    dataSource.setExpectedType(DataSource.class);
    dataSource.setJndiName(environment.getProperty("default.jdbc.jndi.dataSource"));
    return dataSource;
}

@Bean
public PlatformTransactionManager transactionManager() {
    return new JtaTransactionManager();
}

@Bean
public EntityManagerFactory entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setJtaDataSource(dataSource());
    entityManagerFactory.setPersistenceUnitName("MyPersistenceUnit");
    entityManagerFactory.setPackagesToScan(new String[] { "org.mycom.**.entity" });
    entityManagerFactory.setJpaVendorAdapter(jpaVendorAdaper());
    entityManagerFactory.setJpaPropertyMap(additionalProperties());
    entityManagerFactory.setValidationMode(ValidationMode.NONE);
    entityManagerFactory.setSharedCacheMode(SharedCacheMode.DISABLE_SELECTIVE);
    ClasspathScanningPersistenceUnitPostProcessor hbmScanner = new ClasspathScanningPersistenceUnitPostProcessor("com.mycomp");
    hbmScanner.setMappingFileNamePattern("**/*hbm.xml");
    entityManagerFactory.setPersistenceUnitPostProcessors(hbmScanner);
    entityManagerFactory.afterPropertiesSet();
    return entityManagerFactory.getObject();
}
}

和我的存储库配置如下

@Configuration 
@EnableJpaRepositories(basePackages="org.mycom.**.repository",
   repositoryFactoryBeanClass=EnversRevisionRepositoryFactoryBean.class)
@EnableJpaAuditing
public class RepositoryConfiguration {

@Autowired
private SecurityUtils securityUtils;

@Bean
@Profile("production")
public  AuditorAware<User> springSecurityAwareAuditor() {
    return () -> securityUtils.getCurrentUser();
}

}

2 个答案:

答案 0 :(得分:1)

这是一个bug in Spring Data JPA,对于Ingalls RC1和Hopper SR3(包括在Spring Boot 1.4.1中)是固定的。

解决方法是在XML配置文件中声明JNDI对象并显式设置expected-type属性。

答案 1 :(得分:0)

您的堆栈跟踪说明了所有内容:

通过带有[java.util.Set]类型的索引0的构造函数参数表示的不满意依赖:创建名为'org.springframework.orm.jpa.SharedEntityManagerCreator#1 '的bean时出错:

通过构造函数参数表达的不满意,索引为0 [javax.persistence.EntityManagerFactory]:无法转换工厂方法类型的参数值 [org.apache.activemq.artemis.jms.client。 ActiveMQJMSConnectionFactory ] 必需类型[javax.persistence。 EntityManagerFactory ]:< / p>

简单地说,当您尝试创建SharedEntityManagerCreator bean时,您提供了错误的参数。