Spring AOP creates extra bean

北慕城南 提交于 2019-12-05 11:03:40

Though I'm not sure, my guess is that spring first instantiates the regular class, and then makes a CGLIB proxy, which is a subclass. Note that for initialization you should use @PostConstruct, which is guaranteed to be used once.

To verify my hypothesis, add a breakpoint in the constructor and see when it is invoked - one of the times it should be right after the CModel$EnhancedByCGLIB something

When Spring creates a proxy to your class, it will use CGLIB to generate a class that subclasses CModel. The net affect is your constructor will be called twice.

Check out the Spring documentation for more detail (specifically the third bullet): http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-proxying

As a side note, Spring will use the JDK proxying mechanism if your class implements an interface -- and the JDK proxying mechanism will not call your constructor.

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