我想知道爲什麼字段注入在@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