简体   繁体   中英

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:

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:

<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.

Spring AOP documentation.

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.

I can access the returnType using @AfterReturning annotation's returning parameter.

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

Output from the method is like : 在此处输入图片说明

Hope it clarifies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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