我想知道为什么字段注入在@SpringBootApplication
类中工作,而构造函数注入不能。Spring引导在@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
public class CustomTypeService {
public void process(){
System.out.println("CustomType");
}
}
我@Component类:
@Component
@ConfigurationProperties("application.type")
public class ApplicationTypeBean {
private String type;
感谢您的澄清! – Patrick
报价是关键。我需要从4.3降级。这是可行的。 – sschrass