Springboot中Aspect实现切面(以记录日志为例——自定义注解的方式,好文章非常重要)

192 篇文章 12 订阅

转载自,原文格式更清晰:https://blog.csdn.net/cleverutd/article/details/8607491

最近boss叫我用spring aop做一个试验,说是之后一个新项目要用到,大体需求如下:拦截某个函数的返回值,并修改之。

    因为之前我对AOP的认识只是停留在上课时老师跟我们传授的理论阶段,从未写过代码实践过,因此这次我花了一定的时间去做了这个试验。一开始打算上网直接查找相关代码,但是发觉都没有达到我预期的效果,于是决定自己写一个。一开始我打算用后置增强来解决,但是发现只能获取返回值,并不能改变它,之后在stackOverflow问了一下,经过一个网友的提示,终于fix掉了,核心是用环绕增强和ProceedingJoinPoint,废话少说,直接贴代码

定义目标业务类


 
 
  1. public class AopDemo implements IAopDemo{
  2. public String doSth(String task){
  3. System.out.println( "do somthing.........."+task);
  4. return task;
  5. }
定义切面,参数ProceedingJoinPoint 通过直接打印pjp,会看到控制台输出一句execution(String com.toby.aop.IAopDemo.doSth(String)),说明pjp是当前被调用的切点方法的引用


 
 
  1. public class Listener {
  2. public Object around(ProceedingJoinPoint pjp) throws Throwable{
  3. System.out.println( "beginning----");
  4. Object object = pjp.proceed(); //运行doSth(),返回值用一个Object类型来接收
  5. object = "Mission Two"; //改变返回值
  6. System.out.println( "ending----");
  7. return object;
  8. }
  9. }
配置xml


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:util= "http://www.springframework.org/schema/util"
  5. xmlns:context= "http://www.springframework.org/schema/context"
  6. xmlns:tx= "http://www.springframework.org/schema/tx"
  7. xmlns:aop= "http://www.springframework.org/schema/aop"
  8. xsi:schemaLocation= "http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd
  12. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  13. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  14. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
  15. <bean id="aopDemo" class="com.toby.aop.AopDemo"/>
  16. <bean id="listener" class="com.toby.aop.Listener"/>
  17. <aop:config>
  18. <aop:aspect id="myListener" ref="listener">
  19. <aop:pointcut expression="execution(* com.toby.aop.AopDemo.*(..))" id="listenerCut"/>
  20. <aop:around method="around" pointcut-ref="listenerCut"/>
  21. </aop:aspect>
  22. </aop:config>
  23. </beans>
测试


 
 
  1. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "spring-context-common.xml");
  2. IAopDemo demo = (IAopDemo)context.getBean( "aopDemo");
  3. System.out.println(demo.doSth( "Mission One"));

程序运行结果 

 

    beginning----

    do somthing..........Mission One

    ending----

    Mission Two

 

   调用doSth()后,@Around定义的around()方法里通过拦截返回值"Mission One",并修改为“Mission Two”返回,也就是说,doSth的返回值最后其实是around()的返回值


如果有更好的方法,也欢迎赐教,大家互相学习!




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值