1. AOP (Aspect Oriented Programming)
AOP是Spring框架面向切面的编程思想,AOP采用一种称为“横切”的技术,将涉及多业务流程的通用功能抽取并单独封装,形成独立的切面,在合适的时机将这些切面横向切入到业务流程指定的位置中。
2. 引入AOP依赖
在springboot项目中使用aop,首先需要使用引入spring-boot-starter-aop
依赖,完成aop自动化配置.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
3.定义切面
定义切面: 创建一个普通Java类TestAspect作为切面类,需要使用@Aspect
注解标注它,并使用@Component
注解标注该类交由spring容器管理。
定义切入点: 定义一个普通的方法,public修饰并且返回值为空,作为切入点的签名,方法名pointCutName就是切入点的名称,方法使用@PointCut
修饰。@PointCut注解接受一个字符串类型的参数,值为"execution(public * com.example.demo.DemoApplication.*(..))"
,表示连接点是DemoApplication类中的所有公共方法。之后就可以通过该切入点对匹配的这些所有的方法进行增强。
定义通知: 这里定义了一个前置通知执行方法doBefore,需要使用@Before
注解标注,参数是切入点的签名"pointCut()"
,当切入点表达式匹配的方法执行之前,先执行这个前置通知方法deBefore。
@Component
@Aspect
public class TestAspect {
// @Pointcut("within(com.example.demo.*)")
// @Pointcut("this(com.example.demo.DemoApplication)")
@Pointcut("execution(public * com.example.demo.DemoApplication.*(..))")
public void pointCut(){}
// @Before("execution(public * com.example.demo.DemoApplication.*(..))")
@Before("pointCut()")
public void doBefore(JoinPoint joinPoint){
//简单的打印一些请求信息
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
System.out.println(request.getRequestURL().toString());
System.out.println(request.getMethod());
System.out.println(request.getRemoteAddr());
System.out.println(request.getParameterNames());
System.out.println(request.getParameterMap());
System.out.println("before....");
}
}
package com.example.demo;
@SpringBootApplication
@RestController
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("/hello")
public String hello(){
System.out.println("run hello()");
return "hello world!";
}
}
以上只是一个简单的示例,启动以上项目,访问http://localhost:8080/hello,此时执行以上的doBefore然后才会运行hello方法,运行结果:
4. 切入点表达式
切入点表达式中除了返回值类型匹配、名称匹配和参数匹配之外所有部分都是可选的。
返回值类型: 用于匹配返回值的类型,通常使用*
号,表示匹配所有的返回值类型。要匹配指定的返回值类型时需要使用类型的全限路径名。
名称匹配: 就是用于匹配方法名称,可以使用 *
号匹配方法名称的全部或部分。如果要匹配指定类型的方法,可以在类名称之后使用.
与方法名称拼接起来。
参数匹配: 使用()
匹配不带参数的方法,使用(..)
配带0个或多个参数的方法,使用(*)
匹配带一个参数的方法或像这样(*,String)
表示匹配带两个参数的方法,第一个参数可以是任何类型,第二参数只能是String类型的参数。
示例:
1.匹配任何public修饰的公共方法
execution(public * *(..))
2.匹配以set开头的方法名
execution(* set*(..))
3.匹配AccountSerivce中的所有方法
execution(* com.xyz.service.AccountService.*(..))
4.匹配com.xyz.service包中所有类的所有方法
execution(* com.xyz.service.*.*(..))
5.匹配com.xyz.service包及其子包中的所有类的所有方法
execution(* com.xyz.service..*.*(..))
关于切入点表达式的完整详情参考:
https://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html
https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/core.html#aop-pointcuts
5. 通知类型
通知与切入点表达式相关联,并在切入点匹配的方法执行之前、之后或周围运行。切入点表达式可以是对命名切入点的简单引用,也可以是声明到位的切入点表达式。
解释一下:以上的@Before("pointCut()")
方式就是对命名切入点的简单引用,而@Before("execution(public * com.example.demo.DemoApplication.*(..))")
方式就是使用声明到位的切入点表达是。
@Before
前置通知: 在连接点前面执行,前置通知不会影响连接点的执行,除非此处抛出异常。
@AfterReturning
返回后通知: 在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行。
@AfterThrowing
异常抛出后通知: 在连接点抛出异常后执行。
@After
后置通知: 在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容。
@Around
环绕通知: 环绕通知围绕在连接点前后。
详情参考:
https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/core.html#aop-advice
The AspectJTM Programming Guide
The AspectJTM 5 Development Kit Developer’s Notebook