简体   繁体   English

Spring boot在@SpringBootApplication类上找不到默认构造函数

[英]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: 正如Configurationjavadoc所述:

@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.

相关问题 Spring Boot:属性类在@SpringBootApplication类中是“可用的”,但在其他所有地方都为NULL - Spring Boot: properties class is “usable” in @SpringBootApplication class, but NULL everywhere else 为什么 spring 引导可以在没有默认构造函数的情况下反序列化 class? - Why spring boot can deserialize class without default constructor? Spring MVC没有找到默认构造函数? - Spring MVC no default constructor found? Spring @Autowired构造函数给出没有找到默认构造函数 - Spring @Autowired constructor gives No default constructor found @SpringBootApplication如何在spring boot中实际工作? - how does @SpringBootApplication actually work in spring boot? Unabel在Spring Boot(Eclipse)中找到@EnableAutoConfiguration或@SpringBootApplication - Unabel to find @EnableAutoConfiguration or @SpringBootApplication in Spring boot (Eclipse) 使用 Spring Boot 2.3.0.RELEASE 父版本时,SpringBootApplication class 没有编译 - While using Spring Boot 2.3.0.RELEASE parent version, SpringBootApplication class is not compiling 没有为接口 java.util.List 找到主构造函数或默认构造函数 - No primary or default constructor found for interface java.util.List Rest API Spring boot Spring Boot + LocalDate:“没有主要或默认构造函数” - Spring Boot + LocalDate: “No primary or default constructor” Spring 自动连线启动条件需要默认构造函数 - Spring boot condition with autowired requires a default constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM