当前位置: 首页> 文旅> 艺术 > Spring学习04-[Spring容器核心技术AOP学习]

Spring学习04-[Spring容器核心技术AOP学习]

时间:2025/7/9 15:46:27来源:https://blog.csdn.net/weixin_43216437/article/details/140218994 浏览次数:1次

AOP学习

  • AOP介绍
  • 使用
    • 对业务方法添加计算时间的增强
  • @EnableAspectJAutoProxy
  • AOP的术语
  • 通知
    • 前置通知@Before
    • 后置通知@After
    • 返回通知@AfterReturning

AOP介绍

在这里插入图片描述
在这里插入图片描述

  • 如何在Spring中创建一个所谓切面?
    @Aspect+@Component+通知+切点
  • 切面里面的代码怎么运行在业务方法(之前、之后)?
    通知+切点

使用

实现一个对service方法的增强,添加日志

  • 添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency>

在这里插入图片描述

对业务方法添加计算时间的增强

  • 业务类
@Service
public class UserService {public void add(){System.out.println("增加");}public void delete(){System.out.println("删除");}public void update(){System.out.println("修改");}public void query(){System.out.println("查询");}
}
  • 切面类
    啥叫面向切面编程:面向切面编程就是面向切面类进行编程
@Aspect //标记为切面类
@Component
public class LogAspect {//实现方法用时 切点表达式@Around("execution(* com.sping.service.UserService.*(..))")public Object log(ProceedingJoinPoint joinPoint) throws Throwable {//记录方法用时long startTime = System.currentTimeMillis();//执行具体的方法try {joinPoint.proceed(); //连接点就是具体的方法}catch (Exception e){System.out.println("方法执行异常");throw new Exception();}long endTime = System.currentTimeMillis();System.out.println("方法用时:" + (endTime - startTime) + "ms");return null;}}
  • 测试
  @Autowiredprivate UserService userService;@Testpublic void test() throws InterruptedException {userService.add();}

在这里插入图片描述

@EnableAspectJAutoProxy

在这里插入图片描述
在这里插入图片描述

AOP的术语

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
切面、切点、通知、连接点;顾问(通知+切点)
在这里插入图片描述

通知

  • 前置通知
  • 后置通知
  • 返回通知
  • 异常通知
    在定义切点的时候,可以把切点定义到另一个方法中,然后在通知中引入即可
@Aspect
@Component
public class LogAspect{@Pointcut("execution( * com.sping.service.UserService.*(..))")public void PointCut(){}@AfterReturning("PointCut()")public void log(){System.out.println("后置通知执行");}
}

环绕通知和其他通知的区别:环绕通知需要手动去拿到连接点执行目标方法,环绕方法更加的灵活

前置通知@Before

在目标方法执行之前执行

@Aspect
@Component
public class LogAspect{@Before("execution(* com.sping.service.*.*(..))")public void log(){System.out.println("前置通知执行");}
}

可以看到标注了前置通知后,前置通知的增强方法先执行,然后再执行目标方法
在这里插入图片描述

后置通知@After

与前置通知相反,后置通知是目标方法执行之后才会执行,通知里的增强方法

@Aspect
@Component
public class LogAspect{@After("execution(* com.sping.service.*.*(..))")public void log(){System.out.println("后置通知执行");}
}

在这里插入图片描述

返回通知@AfterReturning

其实也是个后置通知,不过就是在方法返回后执行

关键字:Spring学习04-[Spring容器核心技术AOP学习]

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: