这是代码,它给了我以下例外:
@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:创建名称为&#39; bookController&#39;的注册自动连接依赖项失败;嵌套异常是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:没有为依赖项找到类型为[com.neighbourspoon.services.search.repositories.BookRepository]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ 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] at 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] at 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:没有为依赖项找到类型为[com.neighbourspoon.services.search.repositories.BookRepository]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者。依赖注释:{@ 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符合此依赖关系的autowire候选者。依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373)~ [spring-beans-4.2.6.RELEASE.jar:4.2.6.RELEASE] at 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 INFO 35332 ---
答案 0 :(得分:1)
stacktrace包含
没有类型的限定bean [com.neighbourspoon.services.search.repositories.BookRepository]
这是因为BookRepository中的@NoRepositoryBean
注释。你应该删除它。这仅用于自定义中间存储库。在这里,您可以直接扩展Spring Data存储库。