Spring的Aop原理
本文讲解Spring AOP的整体的流程。
一、基本术语:
前置通知:@Before 在执行方法之前执行
后置通知:@After 方法后执行
返回通知:@AfterReturning 方法执行完返回执行
异常通知:@AfterThrowing 出现异常的时候执行
环绕通知:@Around 环绕通知
在切面类上加入@Aspect说明这个类是通知类
在配置类上用@EnableAspectJAutoProxy开启通知的功能
二、@EnableAspectJAutoProxy
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(AspectJAutoProxyRegistrar.class)// 用 @Import 导入AspectJAutoProxyRegistrar组件 让Spring给我们进行管理
public @interface EnableAspectJAutoProxy {
根据源代码,可以知道,底层用的是创建 AnnotationAwareAspectJAutoProxyCreator 组件,这个组件实际上是一个 bean的后置处理器。
我们知道,bean的后置处理器会在创建我们的bean组件的时候,会给他们一次机会创建代理对象,所以,AOP的底层实现就是运用代理来进行实现的。
所以我们要着重看这几个类以及父类:
AnnotationAwareAspectJAutoProxyCreator
AspectJAwareAdvisorAutoProxyCreator
AbstractAdvisorAutoProxyCreator
AbstractAutoProxyCreator
implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware
我们根据 SmartInstantiationAwareBeanPostProcessor 知道,这个对象是一个 BeanPostProcessor
/**
* Apply this BeanPostProcessor <i>before the target bean gets instantiated</i>.
* The returned bean object may be a proxy to use instead of the target bean,
* effectively suppressing default instantiation of the target bean.
* <p>If a non-null object is returned by this method, the bean creation process
* will be short-circuited. The only further processing applied is the
* {@link #postProcessAfterInitialization} callback from the configured
* {@link BeanPostProcessor BeanPostProcessors}.
* <p>This callback will be applied to bean definitions with their bean class,
* as well as to factory-method definitions in which case the returned bean type
* will be passed in here.
* <p>Post-processors may implement the extended
* {@link SmartInstantiationAwareBeanPostProcessor} interface in order
* to predict the type of the bean object that they are going to return here.
* @param beanClass the class of the bean to be instantiated
* @param beanName the name of the bean
* @return the bean object to expose instead of a default instance of the target bean,
* or {@code null} to proceed with default instantiation
* @throws org.springframework.beans.BeansException in case of errors
* @see #postProcessAfterInstantiation
* @see org.springframework.beans.factory.support.AbstractBeanDefinition#getBeanClass()
* @see org.springframework.beans.factory.support.AbstractBeanDefinition#getFactoryMethodName()
*/
Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
/**
* Perform operations after the bean has been instantiated, via a constructor or factory method,
* but before Spring property population (from explicit properties or autowiring) occurs.
* <p>This is the ideal callback for performing custom field injection on the given bean
* instance, right before Spring's autowiring kicks in.
* @param bean the bean instance created, with properties not having been set yet
* @param beanName the name of the bean
* @return {@code true} if properties should be set on the bean; {@code false}
* if property population should be skipped. Normal implementations should return {@code true}.
* Returning {@code false} will also prevent any subsequent InstantiationAwareBeanPostProcessor
* instances being invoked on this bean instance.
* @throws org.springframework.beans.BeansException in case of errors
* @see #postProcessBeforeInstantiation
*/
boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException;
在创建一个bean的实例之前会调用这个方法:
Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException;
返回的对象有可能是代理对象来代替 目标的 bean,
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(Class<?>, String):
@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