是否允许在一个@Configuration类中使用@EnableJpaRepositories和@EnableSolrRepositories?

时间:2014-07-12 14:27:04

标签: spring-data spring-data-jpa spring-data-solr

快速回答:将Spring Framework更新至至少4.0.4

这听起来像Spring Data Commons中的一个错误。 根据{{​​1}}和@EnableSolrRepositories的顺序,Spring尝试在@EnableJpaRepositories上设置solrOperationsJpaRepositoryFactoryBean上的entityManager

SolrRepositoryFactoryBean

之前

EnableSolrRepositories

EnableJpaRepositories

结果

@Configuration
@EnableSolrRepositories("package.a")
@EnableJpaRepositories("package.b")
@EnableTransactionManagement
public class MyConfig {
    ...
}
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'solrOperations' of bean class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]: Bean property 'solrOperations' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064) at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:922) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82) at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489) ... 107 more

之前

EnableJpaRepositories

EnableSolrRepositories

结果

@Configuration
@EnableJpaRepositories("package.b")
@EnableSolrRepositories("package.a")
@EnableTransactionManagement
public class MyConfig {
    ...
}

我正在使用最新的Spring Data版本

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'entityManager' of bean class [org.springframework.data.solr.repository.support.SolrRepositoryFactoryBean]: Bean property 'entityManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:922)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489)
    ... 77 more

作为旁注

在我使用相同配置类的测试中不会发生此错误:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>1.6.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-solr</artifactId>
    <version>1.2.1.RELEASE</version>
</dependency>

仅在Tomcat上部署期间发生(我的版本为7.0.54)。 有人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:1)

快速回答:将Spring Framework更新为至少4.0.4

答案很长: 在调试存储库初始化之后,我得出结论,根本原因是一个错误的实现 AnnotationReadingVisitorUtils#getMergedAnnotationAttributes中使用的AnnotationRepositoryConfigurationSource方法。

此方法接收在MyConfig类上设置的所有注释,并应返回EnableJpaRepositoryEnableSolrRepository的注释属性。 在这种情况下,EnableJpaRepository的所有属性都被EnableSolrRepository覆盖,反之亦然。

有关详细信息,请参阅SPR-11649。此问题已在4.0.4版中修复