2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Why @Configuration classes must have a default/no-arg constructor?

I don"t declare any constructor into my configuration classes annotated by the @Configuration annotation.

But as per the Spring doc it is compulsory that @Configuration class have the default/no-arg constructor

The default constructor is the one inherited by the super class?

userimage

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.

Almost all Java developers knows that compiler adds a default constructor or better known as no argument constructor in every Java class, but many of them forget that, it only does when you don't provide any other constructor.

Many of open source framework, uses reflection to create instance or Object at runtime, based upon name of class. Its generally call the Class.newInstance() method which means its want no-arg contractor.

Answer is