服务bean在spring引导中未自动连接
我正在创建一个具有一个控制器、服务和存储库类的Spring boot应用程序。所有这些都定义如下: 主类服务bean在spring引导中未自动连接,spring,spring-mvc,spring-boot,spring-bean,Spring,Spring Mvc,Spring Boot,Spring Bean,我正在创建一个具有一个控制器、服务和存储库类的Spring boot应用程序。所有这些都定义如下: 主类 @SpringBootApplication public class MainClass extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
@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);
}
}
控制器:
@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);
}
}
请告诉我哪里出了问题
谢谢 从控制器中删除
@EnableAutoConfiguration
,它已经在@SpringBootApplication
中。
您的MainClass应该在根包中。
还要尝试将@ComponentScan(“your.root.package”)
添加到MainClass中
MainClass
位于工作包的顶部,以及所有其他
下的课程李>
@EnableAutoConfiguration
添加到
控制器,如@SpringBootApplication
中已经包含了它CustomReportServiceImpl
中,您正在混合字段和构造函数自动
对于customReportRepository
,请仅选择一个您的类
CustomReportServiceImpl
不是由Spring管理的,就是不能被Spring发现。请查看配置类中的@ComponentScan
注释,并对其进行相应的编辑,以便Spring container可以找到它。请告诉我它是否修复了它@EnableAutoConfiguration
仅适用于@configuration
类。因此,在您的情况下,它不起任何作用,使用@SpringBootApplication
也暗示了这一点。我怀疑您的MainClass
f不在com.test
中,而是在它的某个子包中。建议将引导类放在您案例中可能的最高包中com.test
或任何实际包中。由于@ComponentScan
也由@SpringBootApplication
暗示,但是它是从包开始的,MainClass
是在中定义的,而不是在其他包中(因此是根包)。@M.Deinum.您是正确的。我的主课是在一个子包里。当我把它放到根包中时,一切都正常了。是的,不需要添加EnableAutoConfiguration注释。当我的服务没有自动连线时,我只是添加了它,看看这是否会有什么不同。忘了把它取下来。谢谢@AbdullahKhan正如Deinum在我将主类移动到根包时所建议的那样,服务注入起了作用。我不需要添加ComponentScan注释。谢谢你的帮助!
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]