1.Aop概述:
AOP(Aspect Oriented Programming),即面向切面编程 。在不修改原有代码的基础上,对代码进行增强。
2.术语图示
3.底层原理
4.Aop入门案例
1.环境搭建
2.坐标:web、test、aop
<!--确定spring boot版本-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--web开发启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--test 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--aop 启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
3.启动类:AopApplication
package com.czxy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AopApplication {
public static void main(String[] args) {
SpringApplication.run(AopApplication.class, args);
}
}
4.目标类:UserService、UserServiceImpl
package com.czxy.service;
public interface UserService {
public Integer insert();
public Integer update(String text);
}
package com.czxy.service.impl;
import com.czxy.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Override
public Integer insert() {
System.out.println("user service impl insert");
return 1;
}
@Override
public Integer update(String text) {
System.out.println("user service impl update: " + text);
return 100;
}
}
5.测试类:TestUserService
package com.czxy;
import com.czxy.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AopApplication.class)
public class TestUserService {
@Resource
private UserService userService;
@Test
public void testInsert() {
Integer result = userService.insert();
System.out.println(result);
}
@Test
public void testUpdate() {
Integer result = userService.update("溜溜溜");
System.out.println(result);
}
}
6.AOP代码
package com.czxy.aspect;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component //将当前类,加载到Spring容器
@Aspect //声明切面类
public class MyAspect {
//前置通知注解
@Before("execution(* com.czxy.service..*.*(..))")
public void before() {
System.out.println("开启事务");
}
//后置通知
@AfterReturning("execution(* com.czxy.service..*.*(..))")
public void ar() {
System.out.println("提交事务");
}
}
7.通知类型
try {
//1.前置通知 @Before //3.环绕通知(前) @Around
// 业务代码
//2.后置通知 @AfterReturning //3.环绕通知(后) @Around
} catch (Exception e) {
//4.抛出异常通知 @AfterThrowing
} finally {
//5.最终通知 @After
}
package com.czxy.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component //将当前类,加载到Spring容器
@Aspect //声明切面类
public class MyAspect {
//前置通知注解
@Before("execution(* com.czxy.service..*.*(..))")
public void before(JoinPoint joinPoint) {
// 通过连接点(joinPoint)获得方法签名(Signature),从而获得方法的名称
System.out.println("前置通知: " + joinPoint.getSignature().getName());
}
//后置通知,可以获得返回值的,通过returning设置变量名
@AfterReturning(value = "execution(* com.czxy.service..*.*(..))", returning = "r")
public void ar(JoinPoint joinPoint, Object r) {
System.out.println("后置通知:" + joinPoint.getSignature().getName() + ", 返回值:" + r);
}
//环绕通知:需要手动执行目标方法,返回值类型Object
@Around("execution(* com.czxy.service..*.*(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前:" + proceedingJoinPoint.getSignature().getName());
//手动执行目标方法
Object r = proceedingJoinPoint.proceed();
System.out.println("环绕后");
return r;
}
//抛出异常通知,获得异常类型,通过throwing设置变量名
@AfterThrowing(value = "execution(* com.czxy.service..*.*(..))", throwing = "e")
public void afterThrowing(Exception e) {
System.out.println("抛出异常通知:" + e.getMessage());
}
//最终异常
@After("execution(* com.czxy.service..*.*(..))")
public void after() {
System.out.println("最终通知");
}
}
8.抽取切入点
package com.czxy.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component //将当前类,加载到Spring容器
@Aspect //声明切面类
public class MyAspect2 {
// 抽取切入点表达式
@Pointcut("execution(* com.czxy.service..*.*(..))")
private void myPointcut(){
}
@Before("myPointcut()")
public void before(JoinPoint joinPoint) {
System.out.println("前置通知2: " + joinPoint.getSignature().getName());
}
@AfterReturning(value = "myPointcut()", returning = "r")
public void ar(JoinPoint joinPoint, Object r) {
System.out.println("后置通知2:" + joinPoint.getSignature().getName() + ", 返回值:" + r);
}
}
5.切入点表达式
切入点指示符用来指示切入点表达式目的
AspectJ提供多种指示符:
6.execution
语法:
execution(访问修饰符? 返回值 包名.类名.方法名(方法参数) throws 异常?)
其中带
?
的表示可以省略的部分表达式中支持使用一些特殊符号进行模糊匹配
*
用于匹配1个或多个位置
..
用于匹配0个或多个位置
//实例
//返回值任意 报名. 当前包及其子包 类名任意 方法名任意 参数任意
* com.czxy.service .. * . * (..)
//各个成员取值:
返回值:
int、String 等,具体的类型
* 任意类型
包名:
com.czxy 具体的包
com.czxy.. 当前包及其子包
com.czxy.*service 以Service结尾的包
类名:
User* 以User为前缀
*Service 以Service为后缀
* 任意
方法名
select* 以select开头
save* 以save开头
参数
int 一个参数
int,int 两个参数
.. 任意