[英]Spring boot No default constructor found on @SpringBootApplication class
I wonder why the field injection works in the @SpringBootApplication
class and the constructor injection does not. 我想知道为什么字段注入在
@SpringBootApplication
类中@SpringBootApplication
而构造函数注入不起作用。
My ApplicationTypeBean
is working as expected but when I want to have a constructor injection of CustomTypeService
I receive this exception: 我的
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>()
Is there any reason why it does not work at @SpringBootApplication
class? 它有什么理由不能在
@SpringBootApplication
类中运行吗?
My SpringBootApplication class: 我的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;
}
My @Service class: 我的@Service类:
@Service
public class CustomTypeService {
public void process(){
System.out.println("CustomType");
}
}
My @Component class: 我的@Component类:
@Component
@ConfigurationProperties("application.type")
public class ApplicationTypeBean {
private String type;
SpringBootApplication
is a meta annotation that: SpringBootApplication
是一个元注释:
// Other annotations
@Configuration
@EnableAutoConfiguration
@ComponentScan
public @interface SpringBootApplication { ... }
So baiscally, your ThirdPartyGlobalAndCustomTypesApplication
is also a Spring Configuration
class. 因此,您的
ThirdPartyGlobalAndCustomTypesApplication
也是一个Spring Configuration
类。 As Configuration
's javadoc states: 正如
Configuration
的javadoc所述:
@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning (typically using Spring XML's element) and therefore may also take advantage of @Autowired/@Inject at the field and method level ( but not at the constructor level ).
@Configuration使用@Component进行元注释,因此@Configuration类是组件扫描的候选者(通常使用Spring XML的元素),因此也可以在字段和方法级别利用@Autowired / @Inject( 但不能在构造函数中使用)水平 )。
So you can't use constructor injection for Configuration
classes. 因此,您不能对
Configuration
类使用构造函数注入。 Apparently it's going to be fixed in 4.3 release, based on this answer and this jira ticket . 显然它将在4.3版本中修复,基于这个答案和这个jira票 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.