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

醉酒当歌 提交于 2019-12-01 14:19:46

问题


I am studying for Spring Core certification and, on the provided study stuff, I have this question but I can't give an answer to it.

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

I don't declare any constructor into my configuration classes annoted by the @Configuration annotation. The default constructor is the one inherited by the super class? or what? Why I must have a default constructor and I can't override it?

Tnx


回答1:


According to official spring javadoc, spring @Configuration annotated classes are required to have default no-arg constructor

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

The reason is that spring uses 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.



来源:https://stackoverflow.com/questions/29073254/why-i-must-have-a-default-constructor-in-a-spring-configuration-class-annoted-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!