July 15 2016

Spring AOP AspectJ annotation example

Spring AOP AspectJ annotation example

Spring AspectJ provides annotation support for Spring AOP. AspectJ automatically generates proxy for the bean to intercept method invocations as configured.

How to enable @AspectJ support?

If you are using xml configuration file include <aop:aspectj-autoproxy/>

<aop:aspectj-autoproxy/>

If you are using configuration class add @EnableAspectJAutoProxy annotation

@Configuration
@EnableAspectJAutoProxy
public class ConfigurationClass{

}

Aspect

The combination of advice and pointcut is called as aspect. It is declared with @Aspect annotation.

@Aspect
public class Interceptor {

}

Join point

Join point is a point during the execution of the method or handling of an exception. Spring supports only the method execution join point

Spring AspectJ supports five types of advices

Before Advice (@Before) – executes before joinpoint execution

After Advice (@After) – executes after joinpoint execution (normal and abnormal termination)

After Returning Advice (@AfterReturning) – executes after joinpoint execution (normal termination)

After Throwing Advice (@AfterThrowing) – executes after joinpoint execution (abnormal termination/exception)

Around Advice(@Around) – Can surround the joinpoint with all the above advices

Spring AOP AspectJ annotation example

In the below example, ‘Interceptor’ class is configured with @Aspect annotation and added in configuration file. Spring AspectJ automatically generates a proxy for the ‘EmployeeService’ bean and invokes ‘Before Advice’ and ‘After Advice’ for all the methods.

Aspect – Interceptor.java


package com.answersz.aspectJ;

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

@Aspect
public class Interceptor {

@Before("execution(* *(..)).")
 public void before(JoinPoint joinPoint) {
 System.out.println("** Before Method **");

}

@After("execution(* *(..)).")
 public void after(JoinPoint joinPoint) {
 System.out.println("** After **");
 }

}


Target Bean – EmployeeService.java


package com.answersz.aspectJ;

public class EmployeeService {

public void save() {
 System.out.println(" Save ");
 }

public void update() {
 System.out.println(" Update ");
 }

}


Configuration xml – aspectjConfiguration.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "
 xmlns:aop="http://www.springframework.org/schema/aop">

 <aop:aspectj-autoproxy />

 <bean id="employeeService" class="com.answersz.aspectJ.EmployeeService" />

 <!-- Aspect -->
 <bean id="interceptor" class="com.answersz.aspectJ.Interceptor" />

</beans>

Test Class – PointCutTest.java


package com.answersz.aspectJ;

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

public class SpringAOPAspectJTes {

public static void main(String[] args) {

// Spring AOP AspectJ annotation example

ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "aspectjConfiguration.xml" });
 EmployeeService employeeService = (EmployeeService) context.getBean("employeeService");

employeeService.save();
 employeeService.update();
 }

}


Output


** Before Method **
 Save 
** After **
** Before Method **
 Update 
** After **


Copyright © 2014. All rights reserved.

Posted July 15, 2016 by answersz in category Spring

Leave a Comment

Your email address will not be published. Required fields are marked *

*