Avoiding problems with spring cglib proxy

Using cglib proxy in spring calls:

a) double call constructor

b) do not allow to intercept a method called in another way

but why does spring create a bean and then a proxy? Is it possible to dynamically generate a class that extends the specified bean class and then calls the constructor only once? which will allow a) and b) to be solved for publicly and secure methods. Did I miss something?

+6
source share
1 answer
Good question. I think this is due to the way the Spring application boot context: it first creates all the raw beans, and then uses post-processors, for example. Add AOP (including transactions). For this layered architecture, you first need to create a normal bean and then wrap it. It can be argued that this approach follows composition over the principle of inheritance.

Also note that a) should not be a problem. The class should not perform initialization in the constructor, but in the @PostConstruct method, which is called only once. On the other hand, this leads to another problem:

c) constructor injection with CGLIB proxies cannot be used, see SPR-3150

But I understand your disappointment. Suppose the only acceptable solution to the problem is to weave AspectJ completely.

+4
source

Source: https://habr.com/ru/post/928174/


All Articles