(*告知读者:本文章内容属于本人学习中笔记,若有错误或不当之处欢迎留言指出)
接着上一篇学习spring的第二个中心理念,面向切面编程
2.面向切面编程(Aspect Oriented Programming)
2.1 为什么使用AOP:它能解决面向对象编程不能解决的一些问题,例如事务控制,一些校验工作等。
2.2 AOP术语
1.连接点(join point):具体的拦截对象,只能支持方法上,因为spring只能支持方法
2.切点(point cut):有时候切面不单单用于某个方法,可能用于多个类的不同方法,所以可通过正则式和指示器的规则去定义,从而适配连接点。切点就是提供这样一个功能的概念
3.通知(advice):就是按照约定流程的执行的方法,分为前置通知(before advice),后置通知(after advice),环绕通知(around advice),事后返回通知(afterReturning advice)和异常通知(afterThrowing advice)
4.目标对象(target):通过代理模式代理的目标对象
5.引入(introduction):引入新的类和方法增强Bean的功能
6.织入(weaving):他是通过动态代理技术,为愿有的目标对象生成代理对象,然后将与切点定义匹配的连接点拦截,按照约定将各类通知织入流程的过程
7.切面(aspect):是一个可定义切点,各类通知,和引入的内容,通过它的信息可增强Bean的功能或者将对应的方法织入流程,通常可用一个类表示
2.3 AOP开发步骤
1.确定连接点:即确定要拦截的某一个具体方法
2.定义切面:使用注解@Aspect定义一个切面
@Aspect
@Component
public class UserAspect {
// execution(*com.example.demo.aoptest.service.UserServiceImpl.printUser(..))
// 表示com.example.demo.aoptest.service.UserServiceImpl.printUser这个方法作为连接点
// ..表示任意参数
// *表示任意返回值
@Before("execution(*com.example.demo.aoptest.service.UserServiceImpl.printUser(..))")
public void testBefore(JoinPoint joinPoint){
System.out.println("测试前置通知");
}
@After("execution(*com.example.demo.aoptest.service.UserServiceImpl.printUser(..))")
public void testAfter(){
System.out.println("测试后置通知");
}
@Around("execution(*com.example.demo.aoptest.service.UserServiceImpl.printUser(..))")
public void testAround(ProceedingJoinPoint jp) throws Throwable {
jp.proceed();
System.out.println("测试环绕通知");
}
}
3.定义切点
@Aspect
@Component
public class UserAspect {
// execution(* com.example.demo.aoptest.service.UserServiceImpl.printUser(..))
// 表示com.example.demo.aoptest.service.UserServiceImpl.printUser这个方法作为连接点
// ..表示任意参数
// *表示任意返回值
@Pointcut("execution(*com.example.demo.aoptest.service.UserServiceImpl.printUser(..))")
public void pointCutTest(){}
@Before("pointCutTest()")
public void testBefore(JoinPoint joinPoint){
// 通知获取参数,可用joinPoint.getArgs()方法;
System.out.println("测试前置通知");
}
@After("pointCutTest()")
public void testAfter(){
System.out.println("测试后置通知");
}
@Around("pointCutTest()")
public void testAround(ProceedingJoinPoint jp) throws Throwable {
// 通过此方法回调原目标对象的方法
// 这里不调用jp.proceed(),那么原目标对象的方法将会被取代并且不会执行
jp.proceed();
System.out.println("测试环绕通知");
}
}
可看到定义了切点之后代码简洁了许多
还有一个小知识点,那就是多个切面的执行顺序,则通过注解@Order(1)可实现,里面的数字代表先后,对于前置通知而言是按照从小到大运行,对于后置通知和事后返回通知都是从大到小运行