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 Around Advice using Classic Spring Proxy-Based AOP in Java ?.

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

Step 1:- Division.java Interface

package com.hubberspot.classic.aop.aroundadvice;

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

public interface Division {

 public int divide(int a , int b);

}




Step 2:- DivisionImpl.java Service Implementation Class

package com.hubberspot.classic.aop.aroundadvice;

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

public class DivisionImpl implements Division {

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

  System.out.println("Number are : " + a + ", " + b);

  return a/b;
 }

}




Step 3:- Around Advice Implementation class

package com.hubberspot.classic.aop.aroundadvice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

// This class implements MethodInterceptor interface
// having a method by name invoke(). This Around Advice
// is implementing both before and after advice simultaneous.
// It can also alters the returned value from the advised
// method. It allows some post processing to the methods
// returned value.

// MethodInterceptor has invoke() method which takes in 
// argument as MethodInvocation. This MethodInvocation object
// has a detailed information about the method being intercepted
// It has all the information about the arguments of the 
// method. It also has a method called as proceed() which
// transfer call to target method or the intercepted method

public class AroundDivisionAdvice implements MethodInterceptor {

 // invoke() method decides whether divide() method should be
 // called or not based on the value passed by the user. 
 // If the denominator passed as zero than new exception is 
 // thrown.
 // methodInvocation.proceed() helps calling the target method
 // if not called divide method doesnt gets called. 

 @Override
 public Object invoke(MethodInvocation methodInvocation) throws Throwable {

  Object args[] = methodInvocation.getArguments();

  int number = ((Integer)args[1]).intValue();

  if(number == 0) {
   throw new Exception("Cannot divide with 0.");
  }
  return methodInvocation.proceed();
 }

}




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.

<?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">


<!-- Around Advice:- Advice is been configured as beans -->
<bean id="aroundDivision"
 class="com.hubberspot.classic.aop.aroundadvice.AroundDivisionAdvice" />

<!-- Implementation Class -->
<bean id="divisionImpl" class="com.hubberspot.classic.aop.aroundadvice.DivisionImpl" />

<!-- 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="divide" 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="divisionImpl" />
 </property>

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

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



Step 5:- Test class


package com.hubberspot.classic.aop.aroundadvice;

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

public class DivisionTest {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of AbstractApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as around_advice.xml placed
  // at classpath of our application.


  ApplicationContext context = 
    new ClassPathXmlApplicationContext("around_advice.xml");

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Divide object. Without implementing new keyword we 
  // have injected object of Divide just by reading an xml 
  // configuration file.

  Division divide = (Division)context.getBean("divide");

  int result = divide.divide(10 , 5);

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

  result = divide.divide(10, 0);

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


 }

}




Output of the program : 


 
 
© 2021 Learn Java by Examples Template by Hubberspot