简体   繁体   English

Spring AOP:在返回方法中获取返回类型

[英]Spring AOP: Getting return types in after-returning method

I was trying to implement after-returning in Spring AOP and the basic implementation works fine:我试图在 Spring AOP 中实现after-returning并且基本实现工作正常:

public void afterExecution(JoinPoint jp){
    System.out.println("Returning");
    System.out.println("Returning from: " + jp.toString());
            // How to get the return type object here?
}

How to get the return type object in the above method?如何获取上述方法中的返回类型对象?

This is what I added in the context xml file:这是我在上下文 xml 文件中添加的内容:

<aop:pointcut id="emplRet" expression="execution(java.lang.String com.model.Employee.get*())"/>
    <aop:aspect ref="aspect">
        <aop:after-returning pointcut-ref="emplRet" method="afterExecution"/>
    </aop:aspect>

Please advice.请指教。

You can specify您可以指定

returning="retVal"

in your pointcut expression and add a parameter to your method.在您的切入点表达式中并为您的方法添加一个参数。 You would have to reference the bound value retVal in your after-returning advice.您必须在after-returning建议中引用绑定值retVal

Spring AOP documentation. Spring AOP 文档。

Let us understand this through an example:让我们通过一个例子来理解这一点:

I have a class:我有一堂课: 在此处输入图片说明

For this getter method ie getList, I want to get returnType in my Aspect Class.对于这个getter 方法,即getList,我想在我的Aspect 类中获取returnType。

I can access the returnType using @AfterReturning annotation's returning parameter.我可以使用 @AfterReturning 注释的返回参数访问 returnType。

For example :例如 : 在此处输入图片说明

Output from the method is like :该方法的输出如下: 在此处输入图片说明

Hope it clarifies.希望它澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM