现象
如题,启动项目时,报错如下:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field messageTemplateService in com.xxx.XxxxxController required a bean of type 'com.xxx.XxxxxService' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.xxx.XxxxxService' in your configuration.
代码
- 代码1
//引用的地方
@Autowired
private MessageTemplateService messageTemplateService;
- 代码2
@Configuration
@ConditionalOnClass(XxxxxService.class)
public class XxxxxServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(XxxxxService.class)
public XxxxxService uploadService(TokenConfigService tokenConfigService) {
log.info("XxxxxService build start");
XxxxxService xxxxxService = new XxxxxService();
xxxxxService.setTokenConfigService(tokenConfigService);
log.info("XxxxxService build end");
return xxxxxService;
}
}
- 代码3
@Configuration
@ConditionalOnClass(UploadService.class)
public class UploadServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean(UploadService.class)
public UploadService uploadService(TokenConfigService tokenConfigService) {
log.info("UploadService build start");
UploadService uploadService = new UploadService();
uploadService.setTokenConfigService(tokenConfigService);
log.info("UploadService build end");
return uploadService;
}
}
分析
- 本质是对象XxxxxService在spring容器不存在。
- 删除【代码2】,恢复正常
- 分析【代码2】与【代码3】互相影响,比对发现@Bean修饰的方法名称重复了,改成不一样的解决~