Spring data elasticsearch:我的自定义存储库类没有qulifying bean,注入失败

时间:2018-01-07 19:20:42

标签: java spring maven elasticsearch spring-data

这是控制台上的错误消息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySearchRepository2Service': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'sheffield.elasticsearch.repository.MySearchRepository2<?, ?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    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:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4745)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5207)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'sheffield.elasticsearch.repository.MySearchRepository2<?, ?>' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)

我的服务类的实现:

package sheffield.service;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import sheffield.elasticsearch.repository.MySearchRepository2;
import sheffield.mbg.pojo.Commodity;

@Service("mySearchRepository2Service")
public class MySearchRepository2ServiceImpl implements MySearchRepository2Service {

    @Resource
    private MySearchRepository2 mySearchRepository2;

    public MySearchRepository2ServiceImpl() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public Commodity saveCommodityIndex(Commodity commodity) {
        // TODO Auto-generated method stub
        return (Commodity) mySearchRepository2.save(commodity);

    }

    @Override
    public void deleteCommodityIndex(Long commodityid) {
        // TODO Auto-generated method stub
        mySearchRepository2.delete(commodityid);

    }

    @Override
    public List<Commodity> selectAllCommodityIndex() {
        // TODO Auto-generated method stub
        List<Commodity> list = (List<Commodity>) mySearchRepository2.findAll();
        return list;
    }

    @Override
    public Commodity selectOneCommodityIndex(Long commodityid) {
        // TODO Auto-generated method stub
        return (Commodity) mySearchRepository2.findOne(commodityid);
    }

    @Override
    public List<Commodity> findByCommoditydescriptionOrCommoditynameOrCommoditycategory(String commoditydescription,
            String commodityname, String commoditycategory) {
        // TODO Auto-generated method stub
        return mySearchRepository2.findByCommoditydescriptionOrCommoditynameOrCommoditycategory(commoditydescription,
                commodityname, commoditycategory);
    }

}

我的elasticsearch存储库界面:

package sheffield.elasticsearch.repository;

import java.io.Serializable;

import org.springframework.stereotype.Repository;


public interface MySearchRepository2<Commodity, Long extends Serializable>
        extends sheffield.elasticsearch.MySearchRepository<Commodity, Long> {

}

基本的elasticsearch存储库接口:

    package sheffield.elasticsearch;

import java.io.Serializable;
import java.util.List;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;

@NoRepositoryBean
public interface MySearchRepository<T, ID extends Serializable>
        extends ElasticsearchRepository<T, ID>, PagingAndSortingRepository<T, ID> {
    List<T> findByCommoditydescriptionOrCommoditynameOrCommoditycategory(String commoditydescription,
            String commodityname, String commoditycategory);
}

的applicationContext.xml:

<context:component-scan base-package="sheffield" />
<bean id="mySearchRepository2" name="mySearchRepository2"
        class="sheffield.elasticsearch.repository.MySearchRepository2"></bean>
    <elasticsearch:transport-client id="client"
        cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300"
        client-transport-sniff="false" />
    <!-- <jpa:repositories base-package="sheffield.elasticsearch.repository"></jpa:repositories> -->
    <elasticsearch:repositories base-package="sheffield.elasticsearch.repository"
        elasticsearch-template-ref="elasticsearchTemplate" />
    <bean id="elasticsearchTemplate" name="elasticsearchTemplate"
        class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>

的pom.xml:

<!-- Spring ElasticSearch Data JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
            <version>2.1.9.RELEASE</version>
        </dependency>

不知道出了什么问题。我试图删除

<bean id="mySearchRepository2" name="mySearchRepository2"
        class="sheffield.elasticsearch.repository.MySearchRepository2"></bean>

但同样的错误发生了。我正在使用elasticsearch 2.4.0,所以也许是版本问题? 请帮助我,谢谢大家。

0 个答案:

没有答案