Advice In Spring

Advice In Spring

In this tutorial, we are going to discuss what is advice and various types of advice 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 advice codes 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
Advice In Spring
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, we must first declare a user-defined class. It must implement org.springframework.aop. MethodBeforeAdvice interface, and we must provide an implementation for the following method provided by the MethodBeforeAdvice interface.

public void before(Method m, Object[] Bus_Meth_Params, Object target) throws Exception
  • Where java.lang.reflect.Method parameter can 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 the Object represents the target Object.

Note

The services which are implemented in the before() method are executed 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 a user-defined class. It must implement org.springframework.aop.AfterReturningAdvice and we must implement the following method.

public void afterReturning(Object returnValue,Object[] args, Object target) throws Exception
  • Where the first parameter represents the return value in the form of the Object type.
  • Where the second parameter can represent business method parameters in the form of Object[].
  • Where the third parameter represents 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 the same, but in the Annotation approach, both are different.

4 . After-throwing or Throws Advice

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

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

If we want to use After Throwing Advice in Spring applications, we must first declare a user-defined class. It must implement org.springframe work.aop.ThrowsAdvice interface, and we must provide an 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 can 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 represents 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 a combination of both Before and After Advice.
  • Around Advice is not given by the 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 Alliance has provided a predefined interface in the form of org.aopalliance.intercept. MethodInterceptor.

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

public Object invoke(MethodInvocation mi)throws Throwable

In Around Advice, we will implement the 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 the business method and modify the value, and it can return a different value back to the client, as the return type is Object, but in the After Advice, it’s 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;
   }
}
Advice In Spring

Scroll to top