我试图在同一个项目中使用ElasticSearch和MySQL。 我在不同的项目中定义了两个存储库,但我总是收到此错误消息:
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.owish.services.UserServices com.owish.controllers.UserController.userServices; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServices': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.owish.repositories.search.UserSearchRepo com.owish.services.UserServices.userSearchRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSearchRepo': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property index found for type User!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
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:83)
at com.owish.services.AppServices.main(AppServices.java:25)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.owish.services.UserServices com.owish.controllers.UserController.userServices; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServices': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.owish.repositories.search.UserSearchRepo com.owish.services.UserServices.userSearchRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSearchRepo': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property index found for type User!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 13 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServices': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.owish.repositories.search.UserSearchRepo com.owish.services.UserServices.userSearchRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSearchRepo': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property index found for type User!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
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.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 15 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.owish.repositories.search.UserSearchRepo com.owish.services.UserServices.userSearchRepo; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSearchRepo': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property index found for type User!
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userSearchRepo': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property index found for type User!
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
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.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
... 28 more
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property index found for type User!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:77)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:329)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:309)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:272)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:243)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:235)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:373)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:353)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:84)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:62)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:100)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:211)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:74)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:416)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
... 38 more
当我刚刚使用JpaRepository
时我没有问题。
只有当我添加另一个扩展ElasticsearchRepository
的存储库时,我才会遇到这个问题。
JPA和Elastic搜索的接口定义在不同的包中定义。
我使用弹性搜索2.2.1与Spring数据弹性搜索1.3.4
我认为这是一个配置问题。
下面是我正在使用的应用程序配置。如果我错过了什么,你能告诉我吗?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd">
<context:component-scan base-package="com.owish." />
<elasticsearch:node-client id="client" local="true" />
<elasticsearch:repositories base-package="com.owish.repositories.search" />
<elasticsearch:transport-client id="client"
cluster-nodes="localhost:9200,someip:9200" />
<bean class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/owish"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="generateDdl" value="true"></property>
<property name="database" value="MYSQL"></property>
</bean>
</property>
<property name="packagesToScan" value="com.owish.model" />
<property name="persistenceUnitName" value="jpa.sample"></property>
</bean>
<!-- Configure the transaction manager bean -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<jpa:repositories base-package="com.owish.repositories" />
<!-- Enable annotation driven transaction management -->
<tx:annotation-driven />
我的用户实体声明如下:
@Entity
@Table (name ="user")
@Document(indexName = "user")
public class User {
@Id
@Column (name = "id")
@GeneratedValue (strategy = GenerationType.AUTO)
private long id;
@Column (name = "username", nullable = false)
private String username;
@Column (name = "password", nullable = false)
private String password;
我的ElasticSearch回购定义如下:
@Repository
public interface UserSearchRepo extends ElasticsearchRepository<User, Long> {
User findByUsername(String username);
List<User> findAllByOrderById ();
}
和我的JPA回购
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
答案 0 :(得分:5)
ElasticsearchRepository
有一个名为index
的方法,没有任何损害,但是因为你确实将 ElasticSearch存储库放在com.owish.repositories.search
包中,它会由 Spring Data JPA扫描仪扫描:
<jpa:repositories base-package="com.owish.repositories" />
因此,Spring Data JPA会尝试代理您的UserSearchRepo
,但在index
类中找不到任何名为User
的属性,因此出现错误:
找不到类型User!
的属性索引
无论如何,如果你将ElasticSearch存储库从com.owish.repositories
包及其子包移开,你会没事的。例如,您可以将ElasticSearch存储库移动到com.owish.search
以及:
<elasticsearch:repositories base-package="com.owish.search" />
建议,尝试为应用程序的每个体系结构方面定义一个配置,例如:一个用于数据,一个用于Web,一个用于安全性,并且仅在每个中放置相关配置。我没有在数据访问相关配置中获得此组件扫描程序的角色:
<context:component-scan base-package="com.owish." />
为了使您的应用程序正常运行,可能是必要的,但我严重怀疑这属于配置这一事实。
答案 1 :(得分:0)
当您仔细阅读错误时,您将了解其原因
线程“main”中的异常org.springframework.beans.factory.BeanCreationException:创建名为'userController'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.owish.services.UserServices com.owish.controllers.UserController.userServices;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userServices'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.owish.repositories.search.UserSearchRepo com.owish.services.UserServices.userSearchRepo;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为'userSearchRepo'的bean时出错:init方法的调用失败;嵌套异常是org.springframework.data.mapping.PropertyReferenceException:找不到类型User的属性索引!
所以它的最后一行说没有类型User的属性索引,
这意味着您有一个名为User的类,并且您有一个属性,您可以使用该属性映射名为index的表。