SpringBoot系列(1)——AOP-入门
程序员文章站
2022-07-10 11:01:58
摘要aop关键词spring aop小demo概念使用场景:与业务无关的且经常使用到的公共功能如鉴权,日志,性能优化,事务,错误处理,资源池,同步,审计,幂等等优点:降低耦合度,易扩展,高复用实现方式:静态代理(AspectJ) + 动态代理(CGlib/Jdk)aop关键词连接点(Joinpoint) 连接点就是增强的实现**切点(PointCut)**就是那些需要应用切面的方法增强(Advice)前置通知(before)后置通知(after)异常通知(aft...
摘要
- aop关键词
- spring aop小demo
概念
使用场景:与业务无关的且经常使用到的公共功能如鉴权,日志,性能优化,事务,错误处理,资源池,同步,审计,幂等等
优点:降低耦合度,易扩展,高复用
实现方式:静态代理(AspectJ) + 动态代理(CGlib/Jdk)
aop关键词
- 连接点(Joinpoint) 连接点就是增强的实现
- **切点(PointCut)**就是那些需要应用切面的方法
-
增强(Advice)
- 前置通知(before)
- 后置通知(after)
- 异常通知(afterThrowing)
- 返回通知(afterReturning)
- 环绕通知(around)
- 目标对象(Target)
- **织入(Weaving)**添加到对目标类具体连接点上的过程。
- 代理类(Proxy) 一个类被AOP织入增强后,就产生了一个代理类。
-
切面(Aspect) 切面由切点和增强组成,它既包括了横切逻辑的定义,也包括了连接点的定义
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-msMrp9qH-1609345464379)(http://assets.processon.com/chart_image/5fec99ba7d9c0863d3ffd467.png)]
Spring aop测试
pom
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
aspect
@Component
@Aspect
public class DemoAspect {
//切入点1:匹配xxx类下的方法名以demo开头、参数类型为int的public方法
@Pointcut("execution(public * com.bothsavage.service.DemoService.demo*(int))")
public void matchCondition() {}
//切入点2:匹配xxx类下的方法名以demo开头、参数类型为long的public方法
@Pointcut("execution(public * com.bothsavage.service.DemoService.demo1*(long))")
public void matchCondition_() {}
//前置
@Before("matchCondition()")
public void before() {
System.out.println("Before");
}
//全局后置
@After("matchCondition()")
public void after(){
System.out.println("after");
}
//返回后置
@AfterReturning("matchCondition()")
public void afterReturning(){
System.out.println("afterReturning");
}
//抛出后置
@AfterThrowing("matchCondition()")
public void afterThrowing(){
System.out.println("afterThrowing");
}
@Around("matchCondition_()")
public Object around(ProceedingJoinPoint joinPoint) {
Object result = null;
System.out.println("before");
try{
result = joinPoint.proceed(joinPoint.getArgs());//获取参数
System.out.println("after");
} catch (Throwable e) {
System.out.println("after exception");
e.printStackTrace();
} finally {
System.out.println("finally");
}
return result;
}
}
service
@Service
public class DemoService {
public void demo(int arg1){
System.out.println(arg1);
}
public void demo1(long arg1){
System.out.println(arg1);
}
}
test
@SpringBootTest
public class DemoServiceTest {
@Autowired
DemoService demoService;
//测试单独四个
@Test
public void testDemo1(){
demoService.demo(1);
}
//测试around
@Test
public void testDemo2(){
demoService.demo1(1L);
}
}
参考
[2].CGLib动态代理
[3].关于 Spring AOP (AspectJ) 你该知晓的一切
[4].Spring AOP用法详解
本文作者: Both Savage
本文链接: https://bothsavage.github.io/2020/12/28/SpringBoot/SpringBoot系列(1)——AOP-入门/
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
本文地址:https://blog.csdn.net/qq_26624329/article/details/112001003
上一篇: 如何注意网站建设当中的猫腻的方法
推荐阅读
-
Spring Boot入门系列八(SpringBoot 整合Mybatis)
-
荐 Spark快速入门系列(4) | Spark环境搭建—standalone(1) 集群的搭建
-
SpringBoot系列(二)入门知识
-
Android入门到精通|安卓/Android开发零基础系列Ⅱ【职坐标】-学习笔记(1)-- 常用控件及资源介绍
-
Spring全家桶系列–[SpringBoot入门到跑路]
-
ActiveMQ入门系列之应用:Springboot+ActiveMQ+JavaMail实现异步邮件发送
-
SpringBoot系列(1)——AOP-入门
-
Netty入门系列(1) --使用Netty搭建服务端和客户端
-
Docker 入门及安装[Docker 系列-1]
-
Spring Boot入门系列八(SpringBoot 整合Mybatis)