Spring原始碼註解解析day03
開啟AOP的註解--》@EnableAspectJAutoProxy
@EnableAspectJAutoProxy詳情:
裡面匯入了AspectJAutoProxyRegistrar.class類,該類是幹嘛的呢?
原來是一個註冊類,@EnableAspectJAutoProxy註解就是相當於就是在ioc容器裡面註冊了一個AnnotationAwareAspectJAutoProxyCreator的bean,名字叫做internalAutoProxyCreator;
通過名字叫做“註解裝配模式AspectJ自動代理建立器”;所以說,名字雖長可不是一件壞事情,顧名思義,贊!
AnnotationAwareAspectJAutoProxyCreator詳情:
該類的上下級關係:
->implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
->AbstractAutoProxyCreator
->AbstractAdvisorAutoProxyCreator
->AspectJAwareAdvisorAutoProxyCreator
-->AnnotationAwareAspectJAutoProxyCreator
AnnotationAwareAspectJAutoProxyCreator
關注後置處理器(在bean初始化完成前後做事情)、自動裝配BeanFactory;
由此可見:AnnotationAwareAspectJAutoProxyCreator是一個後置處理器,又是一個BeanFactory的實現類;
接下來看看,該類作為這兩個角色,分別都做了什麼事情:
流程:
1)首先肯定是傳入配置類,建立Ioc容器
2)然後就是在有參構造器裡面呼叫refresh()方法
3)在refresh()方法裡面呼叫一個registerBeanPostProcessors(beanFactory);
* 1)、先獲取ioc容器已經定義了的需要建立物件的所有BeanPostProcessor
//主要是你在配置類中所需要的後置處理器、以及容器本身所有的後置處理器
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
* 2)、給容器中加別的BeanPostProcessor
beanFactory.addBeanPostProcessor(new BeanPostProcessorChecker(beanFactory, beanProcessorTargetCount));
* 3)、優先註冊實現了PriorityOrdered介面的BeanPostProcessor;
//相當於優先權,所以你想要優先順序高一點,就將自己定義的後置處理器實現PriorityOrdered介面就好
if (beanFactory.isTypeMatch(ppName, PriorityOrdered.class)) {
* 4)、再給容器中註冊實現了Ordered介面的BeanPostProcessor;
//然後在看是否實現了Ordered.class,優先順序第二
else if (beanFactory.isTypeMatch(ppName, Ordered.class)) {
* 5)、註冊沒實現優先順序介面的BeanPostProcessor; * 6)、註冊BeanPostProcessor,實際上就是建立BeanPostProcessor物件,儲存在容器中;
* * 1)、建立Bean的例項 * * 2)、populateBean;給bean的各種屬性賦值 * * 3)、initializeBean:初始化bean;
@Override
public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
return doGetBean(name, requiredType, null, false);
}
//接著去獲取一個單例項的bean,但是肯定是獲取不到的,返回的是null
sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
@Override
public Object getObject() throws BeansException {
try {
//當獲取不到就會呼叫這個
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
}
});
//開始建立例項
Object exposedObject = bean;
try {
populateBean(beanName, mbd, instanceWrapper);//給bean的屬性賦值
if (exposedObject != null) {
exposedObject = initializeBean(beanName, exposedObject, mbd);//初始化bean
}
}
* * * 1)、invokeAwareMethods():處理Aware介面的方法回撥 * * * 2)、applyBeanPostProcessorsBeforeInitialization():應用後置處理器的 postProcessBeforeInitialization() * * * 3)、invokeInitMethods();執行自定義的初始化方法 * * * 4)、applyBeanPostProcessorsAfterInitialization();執行後置處理器的 postProcessAfterInitialization();
//初始化前操作
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//初始化操作
invokeInitMethods(beanName, wrappedBean, mbd);
//初始化後操作
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
* * 4)、BeanPostProcessor(AnnotationAwareAspectJAutoProxyCreator)建立成功;--》aspectJAdvisorsBuilder