Prevent AOP proxying of some classes

2019-05-27 05:01发布

问题:

Can AOP proxying be prevented for certain classes?

For instance when using global-method-security from Spring Security. In this case I want most beans with annotations to be proxied. But for some beans I am already using a custom proxy with certain special semantics.

Any ideas appreciated!

回答1:

I think that there is not a simple way to do it.

I see two options:

  • Extends SecuredAnnotationSecurityMetadataSource and override findAttributes() method. Then disable secured-annotation and enable your own SecuredAnnotationSecurityMetadataSource with the metadata-source-ref attribute. (Is simpler but work only with spring security)

  • Other (more generic) way is overriding the shouldSkip() method of AbstractAutoProxyCreator and set your own AutoproxyCreator in a BeanFactoryPostProcessor using the setBeanClassName() method on the bean definition registered by Spring Security, ie AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME. For this approach you need to take care about what implementation of AutoProxyCreator are you finally using (InfrastructureAdvisorAutoProxyCreator, AspectJAwareAdvisorAutoProxyCreator or AnnotationAwareAspectJAutoProxyCreator)