A simple application to demonstrate how to implement Before Advice using Classic Spring Proxy-Based AOP in Java.
Step 1:- Sum.java Interface
Step 2:- SumImpl.java Service Implementation Class
Step 3:- Before Advice Implementation class
Step 4 :- Spring Configuration file
Step 5:- Test class
Output of the program :
Step 1:- Sum.java Interface
package com.hubberspot.classic.aop.beforeadvice;
// Its a simple interface for the Sum service.
// It contains one single method called as addition().
// This method takes in two arguments both of the type
// int.
public interface Sum {
public int addition(int a , int b);
}
Step 2:- SumImpl.java Service Implementation Class
package com.hubberspot.classic.aop.beforeadvice;
// It is the implementation class for the
// Sum service. It just calculates and returns
// sum of two numbers passed to it as arguments.
public class SumImpl implements Sum {
@Override
public int addition(int a, int b) {
return (a + b);
}
}
Step 3:- Before Advice Implementation class
package com.hubberspot.classic.aop.beforeadvice;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
// This class is a simple Before Advice which implements
// interface called as MethodBeforeAdvice. This interface
// has a method by name before(). This advice method is
// called just before the addition() method in the SumImpl
// class. It just outputs to the console a simple message.
public class BeforeAdditionAdvice implements MethodBeforeAdvice {
// before() method has to be implemented in this advice class.
// It takes in three parameters:
// 1. java.lang.reflect.Method object which refers to the method
// to which the Advice is applied.
// 2. A simple array of Objects that are the arguments which are
// passed to the method which is been implemented in the service
// class.
// 3. The target object on which the method is been called.
@Override
public void before(Method method, Object[] args, Object target) {
System.out.println("Before Calling Addition Method .... ");
}
}
Step 4 :- Spring Configuration file
|
Step 5:- Test class
package com.hubberspot.classic.aop.beforeadvice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SumTest {
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 before_advice.xml placed
// at classpath of our application.
ApplicationContext context =
new ClassPathXmlApplicationContext("before_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 Sum object. Without implementing new keyword we
// have injected object of Sum just by reading an xml
// configuration file.
Sum sum = (Sum)context.getBean("sum");
int result = sum.addition(5, 10);
System.out.println("Result = " + result);
}
}
Output of the program :
