Reputation: 131
I want to know how spring aop works. AspectJAwareAdvisorAutoProxyCreator
is the class that create the aop proxy. It implements InstantiationAwareBeanPostProcessor
.
My question is why it does not just implement BeanPostProcessor
which returns aop proxy in postProcessBeforeInitialization
method. Why does it need to create proxy object before doGetBean()
.
Upvotes: 0
Views: 1262
Reputation: 67317
If you want to understand Spring AOP on that level, you need to read the source code. The simple answer is: A BeanPostProcessor
is a very general interface of which many specialisations exist as well as classes implementing those sub-interfaces. Here is the class/interface hierarchy top-down:
BeanPostProcessor (org.springframework.beans.factory.config)
DataSourceInitializedPublisher (org.springframework.boot.autoconfigure.orm.jpa)
ServletContextAwareProcessor (org.springframework.web.context.support)
WebApplicationContextServletContextAwareProcessor (org.springframework.boot.context.embedded)
AdvisorAdapterRegistrationManager (org.springframework.aop.framework.adapter)
ResourceServerFilterChainOrderProcessor in OAuth2ResourceServerConfiguration (org.springframework.boot.autoconfigure.security.oauth2.resource)
OAuth2ExpressionHandlerInjectionPostProcessor in OAuth2MethodSecurityConfiguration (org.springframework.boot.autoconfigure.security.oauth2.method)
AbstractAdvisingBeanPostProcessor (org.springframework.aop.framework)
AbstractBeanFactoryAwareAdvisingPostProcessor (org.springframework.aop.framework.autoproxy)
MethodValidationPostProcessor (org.springframework.validation.beanvalidation)
AsyncAnnotationBeanPostProcessor (org.springframework.scheduling.annotation)
HalObjectMapperConfigurer in HypermediaAutoConfiguration (org.springframework.boot.autoconfigure.hateoas)
EmbeddedServletContainerCustomizerBeanPostProcessor (org.springframework.boot.context.embedded)
DestructionAwareBeanPostProcessor (org.springframework.beans.factory.config)
ScheduledAnnotationBeanPostProcessor (org.springframework.scheduling.annotation)
SimpleServletPostProcessor (org.springframework.web.servlet.handler)
InitDestroyAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
CommonAnnotationBeanPostProcessor (org.springframework.context.annotation)
ApplicationListenerDetector (org.springframework.context.support)
BeanValidationPostProcessor (org.springframework.validation.beanvalidation)
InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config)
SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config)
InstantiationAwareBeanPostProcessorAdapter (org.springframework.beans.factory.config)
ScriptFactoryPostProcessor (org.springframework.scripting.support)
RequiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
ImportAwareBeanPostProcessor in ConfigurationClassPostProcessor (org.springframework.context.annotation)
JsonMarshalTestersBeanPostProcessor in JsonTestersAutoConfiguration (org.springframework.boot.test.autoconfigure.json)
MockitoPostProcessor (org.springframework.boot.test.mock.mockito)
SpyPostProcessor in MockitoPostProcessor (org.springframework.boot.test.mock.mockito)
AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
BeanNameAutoProxyCreator (org.springframework.aop.framework.autoproxy)
AbstractAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy)
DefaultAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy)
AspectJAwareAdvisorAutoProxyCreator (org.springframework.aop.aspectj.autoproxy)
AnnotationAwareAspectJAutoProxyCreator (org.springframework.aop.aspectj.annotation)
InfrastructureAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy)
CommonAnnotationBeanPostProcessor (org.springframework.context.annotation)
BeanPostProcessorChecker in PostProcessorRegistrationDelegate (org.springframework.context.support)
OAuth2SsoCustomConfiguration (org.springframework.boot.autoconfigure.security.oauth2.client)
DataSourceInitializerPostProcessor (org.springframework.boot.autoconfigure.jdbc)
LoadTimeWeaverAwareProcessor (org.springframework.context.weaving)
ErrorPageRegistrarBeanPostProcessor (org.springframework.boot.web.servlet)
PropertyMappingCheckBeanPostProcessor in PropertyMappingContextCustomizer (org.springframework.boot.test.autoconfigure.properties)
ConfigurationPropertiesBindingPostProcessor (org.springframework.boot.context.properties)
ApplicationContextAwareProcessor (org.springframework.context.support)
MergedBeanDefinitionPostProcessor (org.springframework.beans.factory.support)
ScheduledAnnotationBeanPostProcessor (org.springframework.scheduling.annotation)
RequiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
InitDestroyAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
CommonAnnotationBeanPostProcessor (org.springframework.context.annotation)
AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
ApplicationListenerDetector (org.springframework.context.support)
Impressive, eh? Now you are interested in AspectJAwareAdvisorAutoProxyCreator
, so let me condense the above tree to this sub-tree:
BeanPostProcessor (org.springframework.beans.factory.config)
InstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config)
SmartInstantiationAwareBeanPostProcessor (org.springframework.beans.factory.config)
AbstractAutoProxyCreator (org.springframework.aop.framework.autoproxy)
AbstractAdvisorAutoProxyCreator (org.springframework.aop.framework.autoproxy)
AspectJAwareAdvisorAutoProxyCreator (org.springframework.aop.aspectj.autoproxy)
AnnotationAwareAspectJAutoProxyCreator (org.springframework.aop.aspectj.annotation)
As you can see, indirectly AspectJAwareAdvisorAutoProxyCreator
also implements BeanPostProcessor
. Read the Javadoc descriptions for each class/interface in condensed sub-tree, then you understand more about which specific case each of them describes or implements.
P.S.: I created the tree in IntelliJ IDEA by navigating to the BeanPostProcessor
class (search via Ctrl-N
) and pressing Ctrl-H
(open class hierarchy view), then clicked the "subtypes hierarchy" button there and "export to text file" afterwards. From the dialog you can just copy the tree in plain text without saving to a file.
Upvotes: 2