Followers

springAOP : @AfterReturning

By using after returning advice, we can get the result in the advice.

Create the class that contains business logic.

Employee.java
==========

package com.mohan; 
public  class Employee{ 
   
    public int m(){System.out.println("m method invoked");return 2;} 
    public int k(){System.out.println("k method invoked");return 3;} 
    public void k1(){System.out.println("k1 method invoked");}
}      

Create the aspect class that contains after returning advice.

TrackEmployee.java
=============

package com.mohan; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.AfterReturning; 
import org.aspectj.lang.annotation.Aspect; 
 
@Aspect 
public class TrackEmployee{ 
    @AfterReturning( 
              pointcut = "execution(* Employee.*(..))", 
              returning= "result") 
               
    public void myadvice(JoinPoint jp,Object result)//it is advice (after returning advice) 
    { 
        System.out.println("additional concern"); 
        System.out.println("Method Signature: "  + jp.getSignature()); 
        System.out.println("Result in advice: "+result); 
        System.out.println("end of after returning advice..."); 
    } 

Now create the Beans.xml file that defines beans.

Beans.xml
=======

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans.xsd  
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop.xsd"> 
 
 
    <bean id="empBean" class="com.mohan.Employee">   </bean> 
    <bean id="trackEmp" class="com.mohan.TrackEmployee"></bean> 
     
    <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"></bean> 
         
</beans>  

Now, let's call the actual method.


Test.java
======
package com.mohan; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
public class Test{ 
    public static void main(String[] args){ 
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); 
        Employee e = (Employee) context.getBean("empBean"); 
        System.out.println("calling m..."); 
        System.out.println(e.m()); 
        System.out.println("calling k..."); 
        System.out.println(e.k());
        System.out.println("calling k1..."); 
        e.k1();
       
    } 

Expected Output

calling m... 
m() method invoked 
additional concern 
Method Signature: int com.javatpoint.Operation.m() 
Result in advice: 2 
end of after returning advice... 
calling k... 
k() method invoked 
additional concern 
Method Signature: int com.javatpoint.Operation.k() 
Result in advice: 3 
end of after returning advice... 

You can see that return value is printed two times, one is printed by TrackEmployee class and second by Test class.

No comments:

Post a Comment