java – 为什么我必须在@Constructor注释注释的Spring配置类中有一个默认构造函数?

我正在攻读Spring Core认证,并且根据所提供的学习内容,我有这个问题,但我无法给出答案.

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

我没有将任何构造函数声明为@Configuration批注注释的配置类.默认构造函数是超类继承的构造函数?或者是什么?为什么我必须有一个默认的构造函数,我不能覆盖它?

TNX

最佳答案
根据official spring javadoc,spring @Configuration注释类需要具有默认的no-arg构造函数

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

原因是春天使用CGLIB to proxy @Configuration类,并且在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.

转载注明原文:java – 为什么我必须在@Constructor注释注释的Spring配置类中有一个默认构造函数? - 代码日志