java - 初始化SolrRepository时出错:通过字段表示的不满意依赖性

标签 java spring-boot spring-data spring-annotations spring-data-solr

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

@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:没有发现依赖项类型为[com.neighbourspoon.services.search.repositories.BookRepository]的合格Bean:期望至少有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:无法自动连线字段:私有com.neighbourspoon.services.search.repositories.BookRepository com.neighbourspoon.services.search.api.rest.BookController.bookRepository;嵌套的异常是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.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 ---

最佳答案

stacktrace包含


没有合格的Bean类型
[com.neighbourspoon.services.search.repositories.BookRepository]


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

关于java - 初始化SolrRepository时出错:通过字段表示的不满意依赖性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38160986/

相关文章:

java - 如何根据条件取消选择二维数组中的一行并查找其他行的总和

java - 如何使用应用程序属性和 pom 文件共享/传递变量

java - Spring Boot 和 Spring Data Rest

java - Spring Boot JPA 应用程序无法从 Spring Boot 2.0.2 启动,但可以在 Spring Boot 1.5.13 中正常运行

Spring数据示例

java - 进行多对多时如何添加到一组实体

java - StringTokenizer 不显示数字末尾的 0

java - 泛型和变量赋值的问题

java - 添加用于客户端身份验证的 trustStore

java - QueryDSL 生成的类无法访问二级元素进行查询