经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
Springboot如何使用Aspectj实现AOP面向切面编程
来源:jb51  时间:2022/1/3 14:29:34  对本文有异议

要在 Springboot中声明 AspectJ 切面

需在 IOC 容器中将切面声明为 Bean 实例 即加入@Component 注解;当在 Spring IOC 容器中初始化 AspectJ 切面之后, Spring IOC 容器就会为那些与 AspectJ 切面相匹配的 Bean 创建代理.

在 AspectJ 注解中, 切面只是一个带有 @Aspect 注解的 Java 类.

引入jar包       

网上都是说springboot使用Aspectj做面向切面编程的时候,只需要引入下面jar包依赖即可

  1. <dependency>
  2. ?? ?<groupId>org.springframework.boot</groupId>
  3. ?? ?<artifactId>spring-boot-starter-aop</artifactId>
  4. </dependency>

但是我去编写的时候,单单引入 spring-boot-starter-aop 的jar依赖的时候,像@Component、@Aspect等這些註解都不能使用,後來發現缺少aspectjweaver 这么个jar包,最后引入了下面的jar才解決問題 

  1. ? ? <dependency>
  2. ?? ?<groupId>aspectj</groupId>
  3. ?? ?<artifactId>aspectjweaver</artifactId>
  4. ?? ?<version>1.5.3</version>
  5. ? ? </dependency>?

网上也有说要在application.properties中添加

spring.aop.auto=true这个配置,才能开启Aspectj注解的扫面,但是我去查询了springboot全局配置文件,里面默认配置为true(spring.aop.auto=true # Add @EnableAspectJAutoProxy),所以我没有去做添加,功能没有问题,切面能正常实现。

最后补充一点小知识

 AspectJ 支持 5 种类型的通知注解

1)@Before:  前置通知:在方法执行之前执行的通知

2)@After: 后置通知, 在方法执行之后执行 , 即方法返回结果或者抛出异常的时候, 下面的后置通知记录了方法的终止.

3)@AfterRunning: 返回通知, 在方法返回结果之后执行

ps:无论方法是正常返回还是抛出异常, 后置通知都会执行. 如果只想在方法返回的时候记录日志, 应使用返回通知代替后置通知.

4)@AfterThrowing: 异常通知, 在方法抛出异常之后

5) @Around: 环绕通知, 围绕着方法执行(即方法前后都有执行)

环绕通知是所有通知类型中功能最为强大的, 能够全面地控制连接点. 甚至可以控制是否执行连接点.

下面是我写的一些通知的实例

大家可以参考一下

  1. ? ? ? ? /*
  2. ?? ? ? ?标识这个方法是个前置通知, ?切点表达式表示执行任意类的任意方法.
  3. ?? ? ? ?第一个 * 代表匹配任意修饰符及任意返回值,?
  4. ?? ? ? ?第二个 * 代表任意类的对象,
  5. ?? ? ? ?第三个 * 代表任意方法,
  6. ?? ? ? ?参数列表中的 .. ?匹配任意数量的参数
  7. ?? ? */
  8. ?
  9. ? ? //@Before: ?前置通知
  10. ? ? @Before("execution (* com.lc.project..controller..*.*(..))")
  11. ? ? public void beforeMethod(JoinPoint joinPoint){
  12. ?? ? ? ?String methodName = joinPoint.getSignature().toString();
  13. ?? ? ? ?Object result= Arrays.asList(joinPoint.getArgs());
  14. ? ? ? ? ? ? System.out.println("The method name:"+methodName+"--value:"+result);
  15. ? ? }
  16. ?
  17. ? ? //@After: 后置通知
  18. ? ? @After("execution (* *.*(..))")
  19. ? ? public void afterMethod(JoinPoint joinPoint){
  20. ? ? ? ? ? ? ? ? String methodName = joinPoint.getSignature().getName();
  21. ? ? ? ? ? ? ? ? System.out.println("The method name:"+methodName+ " ends");
  22. ? ? }
  23. ? ? //@AfterRunning: 返回通知
  24. ? ? @AfterReturning(value="execution (* *.*(..))",returning="result")
  25. ? ? public void afterReturningMethod(JoinPoint joinPoint,Object result){
  26. ? ? ? ? ? ? ? ? String methodName = joinPoint.getSignature().getName();
  27. ? ? ? ? ? ? ? ? System.out.println("The method name:"+methodName+ " ends and result="+result);
  28. ? ? }
  29. ? ? //@AfterThrowing: 异常通知
  30. ? ? @AfterThrowing(value="execution (* *.*(..))",throwing="e")
  31. ? ? public void afterReturningMethod(JoinPoint joinPoint,Exception e){
  32. ? ? ? ? ? ? ? ? String methodName = joinPoint.getSignature().getName();
  33. ? ? ? ? ? ? ? ? System.out.println("The method name:"+methodName+ " ends and result="+e);
  34. ? ? }??

  以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。               

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号