为什么我必须在由 @Configuration 注释注释的 Spring 配置 class 中有一个默认构造函数?

Why I must have a default constructor in a Spring configuration class annoted by the @Configuration annotation?

我正在学习 Spring 核心认证,根据提供的学习材料,我有这个问题,但我无法给出答案。

Why must you have to have a default constructor in your @Configuration annotated class?

我没有在由 @Configuration 注释注释的配置 classes 中声明任何构造函数。默认构造函数是super继承的class?或者是什么?为什么我必须有一个默认构造函数而且我不能覆盖它?

Tnx

根据 official spring javadoc,spring @Configuration 注解 类 需要有默认的无参数构造函数

@Configuration classes must have a default/no-arg constructor and may not use @Autowired constructor parameters. Any nested configuration classes must be static

原因是spring使用了CGLIB to proxy @Configuration classes and there is limitation in Spring, that classes proxied with CGLIB prior to version 4 are required to have default no-args constructor

Prior to Spring 4, CGLIB-based proxy classes require a default constructor. And this is not the limitation of CGLIB library, but Spring itself. Fortunately, as of Spring 4 this is no longer an issue. CGLIB-based proxy classes no longer require a default constructor.