gpt4 book ai didi

spring - 服务 bean 没有在 spring boot 中 Autowiring

转载 作者:行者123 更新时间:2023-12-04 02:04:56 26 4
gpt4 key购买 nike

我正在创建一个包含一个 Controller 、服务和存储库类的 Spring 启动应用程序。所有这些都在下面定义:

主类

@SpringBootApplication
public class MainClass extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MainClass.class);
}

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

}

Controller :

@RestController
@EnableAutoConfiguration
public class ExampleController {

public static final Logger logger = LoggerFactory.getLogger(ExampleController.class);

private CustomReportService customReportService;

@Autowired
public void setCustomReportService(CustomReportService customReportService){
this.customReportService = customReportService;
}

@RequestMapping(value="/api/report/list", method= RequestMethod.GET)
public ResponseEntity<Collection<CustomReport>> listAllCustomReports(){
return new ResponseEntity<>((Collection<CustomReport>) customReportService.listAllCustomReports(), HttpStatus.OK);
}
}

服务接口(interface):

public interface CustomReportService {
Iterable<CustomReport> listAllCustomReports();
}

服务实现:

@Service
public class CustomReportServiceImpl implements CustomReportService{

@Autowired
private CustomReportRepository customReportRepository;

@Autowired
public void setCustomReportRepository(CustomReportRepository customReportRepository){
this.customReportRepository = customReportRepository;
}

@Override
public Iterable<CustomReport> listAllCustomReports() {
return customReportRepository.findAll();
}

}

存储库:

public interface CustomReportRepository extends CrudRepository<CustomReport, Long> {
}

在部署时,我的应用无法启动并出现以下错误:

2017-05-28 15:50:28.723 DEBUG 14708 --- [           main] o.s.b.d.LoggingFailure
AnalysisReporter : Application failed to start due to an exception

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.test.services.customreport.CustomReportService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:366) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1264) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]

at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar!/:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar!/:1.5.3.RELEASE]

请告诉我哪里错了。

谢谢!

最佳答案

从 Controller 中删除@EnableAutoConfiguration,它已经在@SpringBootApplication中了。您的 MainClass 应该在根包中。还尝试将 @ComponentScan("your.root.package") 添加到您的 MainClass

关于spring - 服务 bean 没有在 spring boot 中 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44226447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com