文章目录
1. 初识AOP
1.1什么是AOP?
AOP(面向切面编程),在百度百科中是这么说的,AOP可以对业务逻辑的各个部分进行分离,从而使得业务逻辑的各个部分之间的耦合度降低,提高程序的可用性,提高开发的效率;
在我理解看来,可能就是说AOP就像是一个单独的模块,这个模块可能具有某些功能,当需要的时候就可以直接添加到需要的地方,不需要的时候或者需要修改的时候,又只需要单独改这个模块本身,不需要改动太多的地方;
2. AOP的底层原理
1. AOP底层使用动态代理
这种动态代理有两种情况,一种是有接口的(使用JDK自带的动态代理),第二种是没有接口的情况(使用CGLIB的动态代理);
以前学习过JDK自带的动态代理方式,忘了,再看就是了;
3. AOP中的术语
- 连接点:在类中可以被增强的方法;
- 切入点:实际被增强了的方法;
- 通知(增强):实际被增强的逻辑部分;通知有多种类型:前置通知、后置通知、环绕通知、异常通知、最终通知;
- 把通知应用到切入点的过程;
4. 操作AOP的准备工作
在Spring中一般都是基于AspectJ实现AOP操作;
什么是AspectJ?
AspectJ,独立AOP框架,不属于Spring,只是一般和Spring一起使用;
AspectJ实现AOP操作,一般有XML和注解两种方式;
为了使用AOP,就需要在项目中引入AOP需要的基本依赖;
4.1 切入点如何操作
基本语法结构:
execution(【权限修饰符】【返回类型】 【类全路径】【方法名称】【参数列表】)
例如:
这个类中的test方法:
//*代表所有的修饰符,其它的比如方法,也可以用*,表示所有方法;
execution(* void cn.dxs.OnCollection.Libarary.add())
//对dxs包下,所有的类,所有的方法,进行AOP操作;
execution(* void cn.dxs.*.*())
4.2 AOP简单使用(@Aspect)
先创建一个简单的被增强类(Trisomy),然后再创建一个增强类(TrisomyProxy);
//被增强类
@Component(value = "trisomy")//方便其它注解使用这个名称;
public class Trisomy {
@Value(value = "三体")
private String bname;
public void test(){
System.out.println("这是书"+bname);
}
}
- 创建一个配置文件,用来处理配置信息(也可以用配置类来代替);
配置类:
@Configuration
@ComponentScan(basePackages = {"cn.dxs.OnCollection"})
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class SpringConfig {
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="cn.dxs.OnCollection"/>
<!--一个扫描@Aspect添加代理类的标签-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
- 在增强类上添加注解(@Aspect),也可以添加不同的通知(比如前置通知,后置通知等)
@Component(value = "trisomyProxy")
@Aspect
public class TrisomyProxy {
@Before(value = "execution(* cn.dxs.OnCollection.Trisomy.test(..))")
public void befo(){
System.out.println("这是前置通知!");
}
@AfterReturning(value = "execution(* cn.dxs.OnCollection.Trisomy.test(..))")
public void afeterreturn(){
System.out.println("后置通知");
}
@After(value = "execution(* cn.dxs.OnCollection.Trisomy.test(..))")
public void after(){
System.out.println("最终通知,无论是否有异常都执行");
}
@AfterThrowing(value = "execution(* cn.dxs.OnCollection.Trisomy.test(..))")
public void afterthrow(){
System.out.println("异常通知,只有出现异常才执行");
}
@Around(value = "execution(* cn.dxs.OnCollection.Trisomy.test(..))")
public void around(ProceedingJoinPoint p) throws Throwable {//ProceedingJoinPoint这个类有方法可以直接执行增强方法;
System.out.println("环绕前通知");
p.proceed();//需要抛出异常;
System.out.println("环绕后通知");
}
}
运行结果:
用上面的注解来写代理模式的代码简直不要太简单,基本上就只需要注重自己本身的业务逻辑,其它的代理逻辑基本都不需要考虑,注解真是太方便了;
注意点:
即使改变各种通知方法的位置,但是执行的顺序是不变的;
为了演示上面的异常通知,我直接在方法里面抛出了异常;
//这是需要增强的方法test;
public void test() throws Exception {
System.out.println("这是书"+bname);
throw new Exception("遇到异常了");
}
其它的都不变,最后执行结果:
4.3 相同切入点的抽取
就是指可以将要增强的类的路径抽取出来,这样就可以比较方便的书写;
@Pointcut(value = "execution(* cn.dxs.OnCollection.Trisomy.test(..))")
public void point(){}
@Before(value = "point()")//这个就可以代替之前的execution();
public void befo(){//运行完全没有问题;
System.out.println("这是前置通知!");
}
4.4 如何给多个代理类设置优先级
在增强类上面添加注解@Order(数字类型),最重要的是值越小优先级越高;
由此也可以知道一个被代理类,可以有很多个代理类;