在学习Spring Aop时,遇到一个问题,当 @Around(环绕通知)与 @AfterReturning(后置通知)共存

时,@AfterReturning 通过属性 returning = "var" 获取目标方法的返回值时结果总为null,如下:

接口代码:

package brave.domain;
public interface Cat {
void eat();
String run();
}

目标类代码:

@Component
public class CatImpl implements Cat {
public void eat() {
System.out.println("eat");
}
public String run() {
System.out.println("run");
return "我是返回值";
}
}

切面代码:

@Aspect
@Component
public class CatAdvice {
@Around("execution(* brave.domain.*.*(..))")
public void around(ProceedingJoinPoint pjp){
out.println("begin");
try {
pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
out.println("end");
}
@AfterReturning(pointcut = "execution(* brave.domain.*.*(..))", returning = "var")
public void aferReturning(Object var){ //增强方法的入参名需与returning的值一致
out.println("afterReturning");
out.println(var);
}
}

运行结果:

在目标类的 run()中,我们返回了字符串 “我是返回值”,但运行结果却一直是null,当将环绕通知

注释掉后,结果正确,如图:

为什么会出现这个问题?作为初学者的我也很懵逼,只是在不断地尝试中发现了它,我想将它分享出

来,避免大家也入坑。如有大牛知晓其中的原理,期待您的解惑,谢谢。

Spring @AfterReturning 总是返回null的更多相关文章

  1. spring配置jackson不返回null值

    #json不返回null spring.jackson.default-property-inclusion=non_null

  2. spring jdbcTemplate query 返回值为null

    spring jdbcTemplate query 返回值为null 今天使用以下方法从数据库中查询数据,返回列表 public List<BookBean> getBooks(){ St ...

  3. mybatis有结果返回null

    解决:application.yml 中mybatis此项(解决驼峰及数据库字段有下划线问题) map-underscore-to-camel-case: true 问题: mybatis debug ...

  4. json_decode返回NULL

    最近在调用某公司的API时,将对方返回的数据,使用PHP的json_decode函数解析,但是返回NULL,最终排查为对方传送来的json格式有误 打印$_REQUEST,数据结构大致如下: arra ...

  5. $.parseJson 在 firefox 下返回 null 的问题

    最近调查一个浏览器兼容性问题,在 IE, chrome下都运行正常,但是在 firefox 下运行时: $.parseJson(xxx) 返回 null,所以导致了 无法正常运行,调查的结果是因为 返 ...

  6. Type.GetType()反射另外项目中的类时返回null的解决方法

    项目1:ProjectA namespace ProjectA { public class paa { .... } } Type.GetType("paa")返回null Ty ...

  7. findViewById返回null

    Q:findViewById返回null? A: 代码逻辑错误: 最终,发现错误竟然是在layout文件中把android:id写成了android:name. android:name=" ...

  8. Xamarin +vs2015 Android 开发GPS loaction 返回 null 小结

    最近公司要开发android 所以研究了一下Xamarin  to android 中个GPS 废话不多说,说重点. 想获取手机上的gps信息必不可少的就是要使用 LocationManager Lo ...

  9. c#泛型方法返回null的问题

    c#的泛型方法实现和java实现有点不同,在java中,所有的泛型方法运行时类型必须是引用类型,所以和非泛型一样可以返回null. 但是c#中有点不同,可以同时是值类型和引用类型,而值类型不能赋值nu ...

随机推荐

  1. markdown流程图画法小结

    markdown流程图画法小结 markdown 画图 流程图 最简单的流程图为例 ```mermaid!  graph TD  A --> B //在没有(),[].{}等括号的情况之下,图标 ...

  2. CSS 圣杯布局升级版---多个固定宽度一个自适应宽度

    1.一个div固定,一个div自适应宽度.两种情况,固定在左或者在右. HTML: <div class="box1"> <div class="mai ...

  3. EmguCv“线段” 结构类型学习

    1. 文件所在 Namespace: Emgu.CV.Structure Assembly: Emgu.CV (in Emgu.CV.dll) Version: 3.0.0.2157 (3.0.0.2 ...

  4. RestTemplate 支持服务器内302重定向

    Stack Overflow 里找到的代码,可以正常返回服务器302重定向后的响应 final RestTemplate restTemplate = new RestTemplate(); fina ...

  5. Python基于Flask框架配置依赖包信息的项目迁移部署小技巧

    一般在本机上完成基于Flask框架的代码编写后,如果有接口或者数据操作方面需求需要把代码部署到指定服务器上. 一般情况下,使用Flask框架开发者大多数都是选择Python虚拟环境来运行项目,不同的虚 ...

  6. 看eShopOnContainers学一个EventBus

    最近在看微软eShopOnContainers 项目,看到EventBus觉得不错,和大家分享一下 看完此文你将获得什么? eShop中是如何设计事件总线的 实现一个InMemory事件总线eShop ...

  7. react-dom.js 源码

    /** *以下这是 react-dom.js 的源代码 * ReactDOM v15.3.1 * * Copyright 2013-present, Facebook, Inc. * All righ ...

  8. 利用 jQuery 来验证密码两次输入是否相同

    html <div class="row"> <div class="panel panel-info"> <div class= ...

  9. [php] php操作xml

    xml文件 <?xml version="1.0" encoding="ISO-8859-1"?> <root> <item id ...

  10. Ubuntu Desktop变为Ubuntu Server服务器版的方法

    去Ubuntu官网看到有好几种版本可以下载,alternate(文本安装).desktop9(桌面).netbook(上网本).server(服务器). 使用server版某个理由: 32位的系统可以 ...