Search code examples
javaspringspring-bootaopspring-aop

Is a proxy created for each Bean in Spring?


So, I was reading online and you cannot make Configuration final because CGLIB extends the class to create a proxy. However, I was reading the documentation for @Scope, and the default proxyMethod value is that no proxy is created (link here- https://docs.spring.io/springframework/docs/current/javadoc-api/org/springframework/context/annotation/Scope.html).

So, my main question is, is a proxy created for each @Configuration?


Solution

  • Spring creates proxy for a bean only if required ( example:Transaction management). I have explained this for another SO question here , please go through A2 section of the answer for more details.

    For a @Configuration annotated class a proxy will always be created , which implies that it is required. Why it is required can be understood from the following references.

    @Bean : Read through sections @Bean Methods in @Configuration Classes and @Bean Lite Mode

    Do read through this excellent answer from @kriegaex to understand the inner workings of @Configuration class.

    So to answer your question is a proxy created for each @Configuration ? Yes , unless the proxyBeanMethods for @Configuration is configured explicity.

    Hope this helps.