Advices In Spring

Advices In Spring

Advice is the implementation of Aspect. An Advice provides the code for implementation of the service or Aspect. As an example consider logging service, logging is an Aspect and Advice denotes the implementation of Log4j.

In general, all the advices code will not be included in business methods at compile time, these services will be included at runtime.

Spring Framework is providing the following various advices.

  1. Before Advice
  2. After Advice
  3. After-Returning
  4. After-throwing
  5. Around Advice
1. Before Advice

Before advice contains service/Aspect implementation , it will be executed before executing the respective business method.

To represent Before Advice, Spring Framework has provided a predefined interface in the form of “org.springframework.aop.MethodBeforeAdvice”.

If we want to use Before Advice in Spring applications , first, we have to declare an user defined class, it must implement org.springframework.aop. MethodBeforeAdvice interface and we must provide implementation for the following method provided by MethodBeforeAdvice interface.

public void before(Method m, Object[] Bus_Meth_Params, Object target) throws Exception
  • Where java.lang.reflect.Method parameter is able to provide metadata of the Business method to which BeforeAdvice is applied.
  • Where Object[] is providing business method parameter values in the form of Objects.
  • Where Object is representing the target Object.

Note

The services which are implemented in before() method are executed at before executing business logic.

E.g

public class BeforeAdviceImpl implements MethodBeforeAdvice {
   public void before(Method method, Object[] params, Object target)throws Exception {
      // Service implementation
   }
}
2, 3 After Advice [After Returning Advice]

It is same as Before Advice, But this advice contains services which are applied after completion of our business method logic.

To create an after returning advice in spring, we have to declare an user defined class, it must implement org.springframework.aop.After ReturningAdvice and we must implement the following method.

public void afterReturning(Object returnValue,Object[] args, Object target) throws Exception
  • Where first parameter is representing return value in the form of Object type.
  • Where second parameter is able to represent business method parameters in the form of Object[].
  • Where third parameter is representing Target Object.

E.g

public class AfterReturningAdviceImpl implements AfterReturningAdvice{
   public void afterReturning(Object returnValue,Object[] args, Object target) throws Exception { 
      ----
   }
}

Note

In Schema Based implementation, After Advice and After-Returning Advice are same, but, in Annotation approach both are different.

4 . After-throwing or Throws Advice

This advice will be executed after throwing an exception from the business method.

To represent After-Throwing Advice, Spring Framework has provided a predefined interface in the form of “org.springframework.aop.Throws Advice”.

If we want to use After Throwing Advice in Spring applications , first, we have to declare an user defined class, it must implement org.springframe work.aop.ThrowsAdvice interface and we must provide implementation for the following method provided by ThrowsAdvice interface.

public void afterThrowing([Method, args, target], ThrowableSubclass)

Some Examples of the above form are

public void afterThrowing(Exception ex)
public void afterThrowing(RemoteException)
public void afterThrowing(Method method, Object[] args, Object target, Exception ex)
public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)
  • Where java.lang.reflect.Method parameter is able to provide metadata of the Business method to which BeforeAdvice is applied.
  • Where Object[] is providing business method parameter values in the form of Objects.
  • Where Object is representing the target Object.
  • Where Exception is representing the generated Exception.

E.g

public class ThrowsAdviceImpl implements ThrowsAdvice {
   public void afterThrows(Method method, Object[] params, Object target)throws Exception {
      -----Service implementation-----
   }
}
5. Around Advice
  • Around Advice will be executed before and after executing the business method.
  • Around Advice is combination of both Before and After Advice.
  • Around Advice is not given by spring framework and it is from Open Source implementation called AOP alliance.
  • Around Advice can be used by any framework which supports AOP.

To represent Around Advice, Spring AOP Aliance has provided a predefined interface in the form of org.aopalliance.intercept. MethodInterceptor.

MethodInterceptor has provided the following method in order to provide services before and after execution of the business method.

public Object invoke(MethodInvocation mi)throws Throwable

In Around Advice, we will implement Before and After Advice in invoke() method, in invoke() method will provide before advice logic before calling proceed() method and we will provide After Advice logic after calling proceed() method.

Note

Around Advice can access the return value of business method and it can modify the value and it can return a different value back to the client, as return type is Object, but in the After Advice its not possible right, as its return type is void.

public class AroundAdviceImpl implements MethodInterceptor {
   public Object invoke(MethodInvocation mi)throws Throwable {
      //Before Logic
      Object ob = mi.proceed();
      //After logic
      return ob;
   }
}
Advices In Spring


Scroll to top