AOP原理
看给容器当中注入了什么组件?这个组件的功能是什么,什么时候开始工作?
1.@EnableAspectJAutoProxy
@EnableAspectJAutoProxy开启基于注解的AOP
@Import(AspectJAutoProxyRegistrar.class) 为容器中注册bean
AspectJAutoProxyRegistrar 45
AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(registry);
AopConfigUtils 121
注册bean的类型是:AnnotationAwareAspectJAutoProxyCreator.class
注册bean的id是:internalAutoProxyCreator
2.AnnotationAwareAspectJAutoProxyCreator
继承关系
AnnotationAwareAspectJAutoProxyCreator
AspectJAwareAdvisorAutoProxyCreator
AbstractAdvisorAutoProxyCreator
AbstractAutoProxyCreator implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
SmartInstantiationAwareBeanPostProcessor(bean的后置处理器 在bean初始化前后做事)
BeanFactoryAware:自动装配BeanFactory
AbstractAutoProxyCreator
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
if (beanName != null) {
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
this.targetSourcedBeans.add(beanName);
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
}
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean, String beanName) {
return true;
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}