Spring AOP After advice / After(finally) advice example


After advice is an Advice that executes after a join point. This is an Advice will be executed regardless of the means by which a join point exits (normal or exceptional return). So this can also be called as After-finally advice. ( Similar to the finally block in java). 

There are two ways to create an Advice.

  • Annotation driven
  • XML configuration driven

Following Spring after advice example explains both the approaches.

Below screenshot shows the project structure;
Following Spring after advice example explains both the approaches.
Below screenshot shows the project structure;
Below screenshot shows the project structure;








I have used the same application used for 'Before Advice'. In that example we were logging before the method execution begins. In this example we will log after the method execution.

After advice is almost same as Before advice. We have to use the keyword 'After' in place of 'Before'.

In this sample application I have a Java Interface, a business class which implements the java interface,
a java class Advice, another class for annotation driven advice, a simple java class with public static void main() to drive this application.
The business class has a public method sayHello() which will get a String argument and append it with the word "Hello" and return. So if you pass 'your_name' it will return 'Hello your_name'.

Our requirement is to log the method execution without writing any logic in the business method. So I have created advices to log the method execution. After advice runs after the method execution and logs the method name. (Here it will just print in the console).

In my ApplicationContext.xml I have created beans for BusinessClass and for the advices. <aop:config> is used to create XML driven aop configurations. <aop:pointcut> is used to define the pointcut expression and <aop:after> tells which method of the advice needs to be executed.

<aop:after pointcut-ref="logPointCut" method="log" /> In this configuration, pointcut-ref is used to match the pointcut expression we have already defined and method tells the method in the referenced advice which needs to be executed. In this example log() method is available in loggingAspect class.

<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.2.xsd
        http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 <bean id="businessClass" class="com.sample.spring.business.BusinessClass">
 </bean>

 <bean id="loggingAspect" class="com.sample.spring.aop.LoggingAspect" />
 <bean id="loggingAspect1" class="com.sample.spring.aop.LogginAspectAnnotationDriven" />
 <aop:aspectj-autoproxy />
 <aop:config>
  <aop:aspect ref="loggingAspect">
   <aop:pointcut id="logPointCut" expression="execution(public * *(..))" />
   <aop:after pointcut-ref="logPointCut" method="log" />
  </aop:aspect>
 </aop:config>
</beans>


Below is my Business Interface;


/**
*
*/
package com.sample.spring.business;

/**
* @author Prabu
*
*/
public interface BusinessInterface {
public String sayHello(String name);
}


My Business Class implementing the interface is given below;


/**
 * 
 */
package com.sample.spring.business;

/**
 * @author Prabu
 *
 */
public class BusinessClass implements BusinessInterface {

 @Override
 public String sayHello(String name) {
  return "Hello "+name;
 }
}


Advice class for the XML driven approach is given below; it just displays the method which is being executed in the console.


/**
 * 
 */
package com.sample.spring.aop;

import org.aspectj.lang.JoinPoint;
/**
 * @author Prabu
 * 
 */
public class LoggingAspect {
 public void log(JoinPoint jp) throws Throwable {
  System.out.println("XML Configuratin driven: After executing "+jp.getSignature().getName()+"()");
 }
}


Advice class for annotation driven approach is given below; this class also does the same process.

@Aspect is used to create the Advice
@After("execution(public * *(..))") tells this method needs to be executed after the execution of any public method's execution.

/**
 * 
 */
package com.sample.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;

/**
 * @author Prabu
 *
 */
@Aspect
public class LogginAspectAnnotationDriven {
 
 @After("execution(public * *(..))")
 public void afterAdvice(JoinPoint jp){
  System.out.println("Annotation driven:After executing "+jp.getSignature().getName()+"()");
 }
}

The driver class for the application is given below which is a simple java class with public static void main(). This class creates business bean object from the ApplicationContext.xml and invokes the business method sayHello().


/**
 * 
 */
package com.sample.spring.driver;

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

import com.sample.spring.business.BusinessInterface;

/**
 * @author Prabu
 *
 */
public class AOPTester {

 /**
  * @param args
  */
 public static void main(String[] args) {
  ApplicationContext contex = new ClassPathXmlApplicationContext("ApplicationContext.xml"); 
  BusinessInterface tester=(BusinessInterface) contex.getBean("businessClass"); 
  String result=tester.sayHello("Prabu");
  System.out.println(result);
 }

}

So the XML driven advice should print "After executing sayHello() and the annotation driven advice should print Annotation driven:After executing sayHello() after the business method retuns 'Hello Prabu' string to the driver class.





          Thus we have created after advice by Annotation driven and XML configuration driven  approaches.



Source code of this application: 

SpringAOPAfterFinallyAdvice.zip







Reactions:

0 comments :

Post a Comment