Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to implement After Returning Advice using Classic Spring Proxy-Based AOP in Java ?.

A simple application to demonstrate how to implement After Returning Advice using Classic Spring Proxy-Based AOP in Java.

Scenario 1 ....

Step 1:- Subtraction.java Interface

 

package com.hubberspot.classic.aop.afterreturningadvice;

// Its a simple interface for the Subtraction service.
// It contains one single method called as subtract().
// This method takes in two arguments both of the type
// int.

public interface Subtraction {

 public void subtract(int a , int b);

}




Step 2:- SubtractionImpl.java Service Implementation Class

 package com.hubberspot.classic.aop.afterreturningadvice;

 // It is the implementation class for the 
 // Subtraction service. It just calculates and returns 
 // subtraction of two numbers passed to it as arguments.

 public class SubtractionImpl implements Subtraction {

  @Override
  public int subtract(int a, int b) {

   if(a > b) {

    return (a-b);

   }
   else {

    return (b-a);

   }

  }

 }




Step 3:- After Returning Advice Implementation class

package com.hubberspot.classic.aop.afterreturningadvice;

import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;

// This class is a simple After Returning Advice which implements 
// interface called as AfterReturningAdvice. This interface 
// has a method by name afterReturning(). This advice method is 
// called just before the subtract() method in the SubtractionImpl
// class. It just outputs to the console a simple message.

public class AfterReturningSubtractAdvice implements AfterReturningAdvice {

 // afterReturning() method has to be implemented in this advice class.
 // It takes in four parameters:
 // 1. Object which holds the value returned from the method invoked
 // 2. java.lang.reflect.Method object which refers to the method
 // to which the Advice is applied.
 // 3. A simple array of Objects that are the arguments which are
 // passed to the method which is been implemented in the service
 // class. 
 // 4. The target object on which the method is been called.

 @Override
 public void afterReturning(Object returnValue, Method method, Object[] args,
   Object target) {

  System.out.println("Subtraction of two numbers done .... ");

 }

}




Step 4 :- Spring Configuration file


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.

<?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-3.1.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


<!-- After Returning Advice:- Advice is been configured as beans -->
<bean id="afterReturningSubtract"
class="com.hubberspot.classic.aop.afterreturningadvice.AfterReturningSubtractAdvice" />

<!-- Implementation Class -->
<bean id="subtractImpl"
class="com.hubberspot.classic.aop.afterreturningadvice.SubtractionImpl" />

<!-- Proxy Implementation Class:- This configuration binds advices to the 
 implementation code using a proxy factory bean. In order to apply the advice 
 final bean has to be proxied. Spring's ProxyFactoryBean is a factory that 
 creates a proxy which implies one or more interceptors to bean -->
<bean id="subtract" class="org.springframework.aop.framework.ProxyFactoryBean">
 
<!-- This proxy bean definition has three properties 
  
 1. target :- It tells ProxyFactoryBean to which bean it will be 
 making proxy. 

 2. intercepterNames :- Interceptor Names property is configured 
 as a list of interceptor or advices which is being applied to 
 this proxy. 
  
 3. proxyInterfaces :- It just tells to ProxyFactoryBean that 
 which interface Proxy bean has to implement. 
  
 -->
 
 
 <property name="target">
  <ref bean="subtractImpl" />
 </property>

 <property name="interceptorNames">
  <list>
   <value>afterReturningSubtract</value>
  </list>
 </property>

 <property name="proxyInterfaces">
  <value>com.hubberspot.classic.aop.afterreturningadvice.Subtraction
  </value>
 </property>
 </bean>
</beans>


Step 5:- Test class

package com.hubberspot.classic.aop.afterreturningadvice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SubtractionTest {

 public static void main(String[] args) {

  ApplicationContext context = 
    new ClassPathXmlApplicationContext("after_returning_advice.xml");
  Subtraction subtract = (Subtraction)context.getBean("subtract");

  int result = subtract.subtract(10, 6);

  System.out.println("Result = " + result);

  result = subtract.subtract(5, 10);

  System.out.println("Result = " + result);



 }

}





Output of the program : 




















Scenario 2 .... Changes done only to step 2 and 4 in previous scenario

Step 2:- SubtractionImpl.java Service Implementation Class

package com.hubberspot.classic.aop.afterreturningadvice;


public class SubtractionImpl implements Subtraction {

 @Override
 public void subtract(int a, int b) {



  if(a > b) {

   System.out.println((a-b));

   throw new RuntimeException();

  }
  else {

   System.out.println((b-a));

   throw new RuntimeException();

  }
 }

}




Step 4:- Test class

package com.hubberspot.classic.aop.afterreturningadvice;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SubtractionTest {

 public static void main(String[] args) {

  ApplicationContext context = 
    new ClassPathXmlApplicationContext("after_returning_advice.xml");
  Subtraction subtract = (Subtraction)context.getBean("subtract");

  subtract.subtract(10, 6);
  subtract.subtract(5, 10);





 }

}


Output of the program :
 

 
 
© 2021 Learn Java by Examples Template by Hubberspot