1、AOP基本概念
为什么会诞生AOP呢?实际上在我们的面相对象的编程中,我们通常都是将世界抽象成类,但是如果类中有重复的事情需要处理,比如日志、权限认证或者一些其他的东西,那么我们就得入侵代码并且添加相应的类,在每个需要调用的类的地方调用。举例说明如下:
我们现在有如下的三个类,我们有新需求想在3个类的方法调用前和后都实现日志的打印,那么
1、传统最笨的处理方法就是在每个类中添加是、两个方法,一个是调用前打印,一个是调用后打印!!
2、单独申请一个日志类,然后写两个方法,根据传入值的不同做不同的处理,那么我们还是要在每个类中都进行修改,申请类,调用相关的方法。
3、使用代理,因为我们知道代理是可以持有当前类的示例并且对其进行相关的增强。JDK的动态代理是需要实现公共接口才行,Cglib需要继承类。
Class A{
...
}
class B{
...
}
class C{
...
}
那么其实由方法3就引申出来了AOP,AOP的实现原理是代理+反射,说到这就要提到两个相关的概念,过滤器是函数的回调,拦截器是反射。执行顺序是:
Tomcat----->过滤器------>Servlet------>拦截器------>Controller----->AOP可以实现更加细粒度的分析,最细的粒度可以到方法
2、AOP基本名词概念
1、切面:添加注解@Aspect,就是包含各种切点,实现切入的一个类
2、连接点:Join Point:对某方法实现切入的具体点,所有的AOP都是围绕着这个点进行展开的
3、切点:添加注解@Pointcut,连接点的集合,实际上这个好像才是标注在方法上面的注解
4、通知:切入切点的时机,分为:
4.1、@Before:在切点的方法调用前切入
4.2、@After:在切点的方法调用后切入
4.3、@Around:环绕型通知非常强大,会在方法执行前就切入,可以实现参数的修改,切入会一只执行到方法的结束,可以修改返回值
4.4、@AfterReturning:切入点在方法返回返回值之后
4.5、@AfterThrowing:在抛出异常的时候执行
那么有几个比较容易混乱的执行顺序如下:
: @Around环绕通知
: @Before通知执行
: @Before通知执行结束
: @Around环绕通知执行结束
: @After后置通知执行了!
: @AfterReturning第一个后置返回通知的返回值:18
3、AOP实现原理
AOP的实现原理是反射,具体实现步骤:
3.1、定义相关的注解
3.2、在BeanFactory的时候对BeanDefinition进行扫面,那么我们就可以在BeanDefinition加载的时候就扫描得到需要切入的类和方法了。
3.3、动态生成代理类,对扫描到的类进行增强
3.4、对该类的该方法的调用实现拦截,使得每次调用都是使用的代理类的相应方法。
下面是手写示例:
3.1、注解的定义:
package com.aop.demo.annotation;
import java.lang.annotation.*;
/*
*/
@Documented
@Inherited
//@Target({ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface After {
String value() default "";
}
package com.aop.demo.annotation;
import com.sun.javafx.sg.prism.NodeEffectInput;
import java.lang.annotation.*;
@Documented
@Inherited
//@Target(?)
@Retention(RetentionPolicy.RUNTIME)
public @interface Around {
String value() default "";
}
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/*
这个注解是定义的切面
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Aspect {
}
package com.aop.demo.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Before {
String value() default "";
}
import com.aop.demo.selector.CustomizedImportSelector;
import org.springframework.context.annotation.Import;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/*
AOP开启的开关注解
这里加了Import注解,终于想通了,我们的注解是可以组合的嘛
启动类的@SpringBootApplication就是组合注解
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Import(CustomizedImportSelector.class)
public @interface EnableAspectAutoProxy {
}
package com.aop.demo.annotation;
import org.springframework.context.annotation.Import;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/*
切点
*/
@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
public @interface Pointcut {
String value() default "";
}
3.2、扫描得到切面及切入点
package com.aop.demo.processor;
import com.aop.demo.JunAspectUtil;
import com.aop.demo.annotation.Aspect;
import com.aop.demo.holder.ProxyBeanHolder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableL