Spring AOP-Advice
Advice is the implementation of Aspect. An Advice provides the code for implementation of the service. As an example consider logging service, logging is an Aspect and Advice denotes the implementation of Log4j.
Types of Advices
- Before Advice
- After Advice
- Throws Advice
- Around Advice
Let us see one by one with explanation and example.
Before Advice
- This advice contains implementation of the services which are need to be applied before business logic of method is going to execute.
- During compilation time the services will not be applied to our logic, services will apply only at run time.
- In order to create a Before advice, class should implement MethodBeforeAdvice interface.
- MethodBeforeAdvice interface is given in org.sp-fw.aop.* package.
- If we implement MethodBeforeAdvice interface then we need to override a method called before() method.
- The services which are implemented in before() method are executed at before business logic.
Syntax Of MethodBeforeAdvice — before() Method
1 2 3 4 5 6 7 |
public class beforeAdvice implements MethodBeforeAdvice { public void before(Method m,Object args[], Object target)throws Exception { //My Before Logic... } } |
- The first parameter Method m, is a class at java.lang.reflect.* package, and this parameter is used to access the name of the business method, through getName(), i will show you this in the example.
- Second parameter object[] is used to access the parameter values of the business method, the parameters of business method stored in object array and those parameters are given to before() method by the container in the form of objects only.
- The third parameter is an object to whom this service will be going to apply, usually this will taken care by container, as a programmer we no need to care this
- The service implemented in before advice will be called from a proxy class which is generated by the container for a target class.
Target Class
1 2 3 4 5 6 7 |
public class MyOwnBusinessClass { public void businessMethod() { //Our business logic will goes here..... } } |
Before Services
1 2 3 4 5 6 7 |
public class BeforeAdvice implements MethodBeforeAdvice { public void before(Method m,Object args[], Object target)throws Exception { //My Before Logic... } } |
Proxy Class
1 2 3 4 5 6 7 8 |
public class MyOwnBusinessClass-Proxy (This class name will be given by container) { public void businessMethod() { before() // logic in before() method from Before Services will be executed here //Our business logic will goes here..... } } |
Explanation:
- See MyOwnBusinessClass.java is our own java class file, just our business logic will goes here
- BeforeAdvice.java contains the services, what we need to execute before our business logic
- So proxy is the class generated by the container at run time, see at run time container is executing the logic of before() method from BeforeAdvice class, then our business method logic as a single class.
- In fact we cannot see this proxy class physically , container will creates in the memory and gives the output normally.
Hope you are clear so far.
After Advice (After Returning Advice)
- In the annotations After Advice and After Returning Advice are different, but here both are almost same
- This is also same as Before Advice, But this advice contains services which are applied after completion of our business method logic
- In order to create an after returning advice in spring, our class should implement an interface called AfterReturningAdvice, given in org.sp-fw.aop.* package and we need to override a method given by this interface called afterReturning()
Syntax Of AfterReturningAdvice — afterReturning() Method
1 2 3 4 5 6 7 |
public class afterAdvice implements AfterReturningAdvice { public void afterReturning(Object returnValue,Object args[], Object target)throws Exception { //My Before Logic... } } |
First parameter contains the return value of the business method or null, rest is same as Before Advice.
post a comment