java - 找不到类型用户的属性索引

标签 java spring spring-mvc elasticsearch spring-data

我正在尝试在同一项目中将 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和Elasticsearch的接口(interface)定义是在不同的包中定义的。 我正在使用 Elasticsearch 2.2.1 和 Spring 数据 Elasticsearch 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> {
}

最佳答案

ElasticsearchRepository有一个名为 index 的方法,这没有什么坏处,但是因为您确实将 ElasticSearch Repositories 放在 com.owish.repositories.search 中包,它将被 Spring Data JPA Scanner 扫描:

<jpa:repositories base-package="com.owish.repositories" />

因此,Spring Data JPA 将尝试代理您的 UserSearchRepo 并且在 User 类中找不到任何名为 index 的属性,因此错误:

No property index found for type User!

无论如何,如果您将 ElasticSearch 存储库从 com.owish.repositories 包及其子包中移开,就可以了。例如,您可以将 ElasticSearch 存储库移动到 com.owish.search 并且:

<elasticsearch:repositories base-package="com.owish.search" />

还有一个建议,尝试为应用程序的每个架构方面定义一个配置,例如一个用于数据,一个用于网络,一个用于安全,并且只在每个中放置相关配置。我没有在你的数据访问相关配置中得到这个组件扫描器的作用:

<context:component-scan base-package="com.owish." />

也许为了使您的应用程序启动和运行是必要的,但我严重怀疑这属于配置的事实。

关于java - 找不到类型用户的属性索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252099/

相关文章:

java - 如何在java代码中调用Spring资源上的当前属性占位符?

java - 如何在java中从一个应用程序获取tomcat web绝对路径到centos中的另一个应用程序?

java - 打印并返回正确的列表

java - 如何与来自不同类的 UI 交互

java - 使用 @SuppressWarnings ("unchecked"转换 Java 泛型是否会导致运行时异常

spring - 如何匹配 Spring MVC 中的 URL 部分?

java - 查看 Springs 下拉列表的排序顺序

java - Controller 中的 Spring Autowired 不起作用

java - Spring @autowired

java - 检查我的窗口监听器是否正常工作