【发布时间】:2016-08-10 08:35:03
【问题描述】:
我想知道为什么字段注入在@SpringBootApplication 类中有效,而构造函数注入则无效。
我的 ApplicationTypeBean 正在按预期工作,但是当我想要 CustomTypeService 的构造函数注入时,我收到了这个异常:
Failed to instantiate [at.eurotours.ThirdPartyGlobalAndCustomTypesApplication$$EnhancerBySpringCGLIB$$2a56ce70]: No default constructor found; nested exception is java.lang.NoSuchMethodException: at.eurotours.ThirdPartyGlobalAndCustomTypesApplication$$EnhancerBySpringCGLIB$$2a56ce70.<init>()
它在@SpringBootApplication类中不起作用有什么原因吗?
我的 SpringBootApplication 类:
@SpringBootApplication
public class ThirdPartyGlobalAndCustomTypesApplication implements CommandLineRunner{
@Autowired
ApplicationTypeBean applicationTypeBean;
private final CustomTypeService customTypeService;
@Autowired
public ThirdPartyGlobalAndCustomTypesApplication(CustomTypeService customTypeService) {
this.customTypeService = customTypeService;
}
@Override
public void run(String... args) throws Exception {
System.out.println(applicationTypeBean.getType());
customTypeService.process();
}
public static void main(String[] args) {
SpringApplication.run(ThirdPartyGlobalAndCustomTypesApplication.class, args);
}
public CustomTypeService getCustomTypeService() {
return customTypeService;
}
我的@Service 类:
@Service
public class CustomTypeService {
public void process(){
System.out.println("CustomType");
}
}
我的@Component 类:
@Component
@ConfigurationProperties("application.type")
public class ApplicationTypeBean {
private String type;
【问题讨论】:
标签: java spring spring-boot