初始化 SolrRepository 时出错:通过字段表示的依赖关系不满足

Posted

技术标签:

【中文标题】初始化 SolrRepository 时出错:通过字段表示的依赖关系不满足【英文标题】:Error while initialising SolrRepository: Unsatisfied dependency expressed through field 【发布时间】:2016-11-04 18:50:36 【问题描述】:

这是给我以下异常的代码:

@SpringBootApplication
@ComponentScan(basePackages =  "com.ns.services.search", "com.ns.services.commons.exceptions" )
@EnableSolrRepositories(value = "com.ns.services.search", schemaCreationSupport = true, multicoreSupport =     true)
@EnableSwagger2
public class SearchServiceApplication 

    public static void main(String[] args) 
        SpringApplication.run(SearchServiceApplication.class, args);
    

    @Bean
    public HttpSolrClient solrClient() 
        return new HttpSolrClient("http://localhost:8983/solr");
    

    @Bean
    public SolrTemplate solrTemplate(SolrClient client) throws Exception 
        return new SolrTemplate(client);
    

    @Bean
    public Docket api() 
        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    

    @Bean
    public SolrTemplate bookTemplate() throws Exception 
        SolrTemplate solrTemplate = new SolrTemplate(solrClient());
        solrTemplate.setSolrCore("demo");
        return solrTemplate;
    


BookRepository.java

@NoRepositoryBean
public interface BookRepository extends SolrCrudRepository<Book, String> 

    List<Book> findByName(String name);


BookService.java

public interface BookService 

    Book addBookToIndex(Book book);


BookServiceImpl.java

@Service
public class BookServiceImpl implements BookService 

    public BookServiceImpl() 
        super();
    

    @Autowired
    public BookRepository bookRepository;

    @Override
    public Book addBookToIndex(Book book) 
        return bookRepository.save(book);
    


尝试启动服务时出现异常跟踪:

org.springframework.beans.factory.BeanCreationException:创建名为“bookController”的bean时出错:注入自动装配的依赖项失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配字段:私有 com.neighbourspoon.services.search.repositories.BookRepository com.neighbourspoon.services.search.api.rest.BookController.bookRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.neighbourspoon.services.search.repositories.BookRepository] ​​找到依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:@org.springframework.beans.factory.annotation.Autowired(required=true) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538) ~[spring-context-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 在 org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.5.RELEASE.jar:1.3.5.RELEASE] 在 com.neighbourspoon.services.search.main.SearchServiceApplication.main(SearchServiceApplication.java:26) [bin/:na] 引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.neighbourspoon.services.search.repositories.BookRepository com.neighbourspoon.services.search.api.rest.BookController.bookRepository;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.neighbourspoon.services.search.repositories.BookRepository] ​​找到依赖项:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者。依赖注解:@org.springframework.beans.factory.annotation.Autowired(required=true) 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] ...省略了17个常用框架 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到[com.neighbourspoon.services.search.repositories.BookRepository]类型的合格bean:预计至少有1个bean有资格作为此依赖的自动装配候选者。依赖注解:@org.springframework.beans.factory.annotation.Autowired(required=true) 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] ...省略了19个常用框架

2016-07-03 15:08:34.596 信息 35332 ---

【问题讨论】:

【参考方案1】:

堆栈跟踪包含

没有符合条件的 bean 类型 [com.neighbourspoon.services.search.repositories.BookRepository]

这是因为 BookRepository 中的 @NoRepositoryBean 注释。你应该删除它。这仅用于自定义中间存储库。在这里,您直接扩展了 Spring Data 存储库。

【讨论】:

以上是关于初始化 SolrRepository 时出错:通过字段表示的依赖关系不满足的主要内容,如果未能解决你的问题,请参考以下文章

使用 1.1 版初始化 Terraform 时出错

尝试取消引用 2D 矢量中的元素时出错

Swiftui - 显示解码数据以查看时出错

C++:使用函数为二维数组分配内存时出错

SparkContext:运行 Spark 作业时初始化 SparkContext 时出错

初始化 ADB 时出错:找不到调试桥