Spring Aspect (AOP) Example with @AspectJ Annotation

This article demonstrates the example of Aspect Oriented Programming (AOP) using @AspectJ annotation in Spring. Please click Spring AOP Example with XML Configuration to see the example of the same using XML or Schema style. At the end of this tutorial, you will be able to apply various aspects on your service methods. 

Following jars (over and above your existing setup) will be needed in the classpath in order to execute the examples.
aspectjrt-1.6.11.jar
aspectjweaver-1.6.11.jar
aopalliance-1.0.jar
cglib-nodep-2.1_3.jar
commons-logging-1.1.1.jar
spring-aop-3.0.5.RELEASE.jar
spring-aspects-3.0.5.RELEASE.jar

Service Class

Let's create a sample service class on which Aspect will be applied.

package com.mycompany.myapp.service;

public class MyService {
    public void doService() {
        System.out.println("my service is called");
    }
}
Aspect Class

Let's create a sample Aspect Class. Please see Examples of common pointcut expressions link to see examples of common pointcut expressions.
package com.mycompany.myapp.aspect;

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

@Aspect
public class MyAspect {

 // Before Pointcut This method will be called before any method in MyService
 // is executed
 @Before("execution(* com.mycompany.myapp.service.MyService.*(..))")
 public void doLog() {
  System.out.println("do logging");
 }

 // After pointcut This method will be called after any method in MyService
 // is executed
 @After("execution(* com.mycompany.myapp.service.MyService.*(..))")
 public void doSysout() {
  System.out.println("do sysout");
 }
}

Spring XML Configuration

Let's create a spring configuration file to declare and define beans for service and aspect classes

<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"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
 <context:annotation-config />
 <context:component-scan base-package="com.mycompany.myapp.aspect" />

 <bean id="myAspect" class="com.mycompany.myapp.aspect.MyAspect">
  <!-- configure properties of aspect here as normal -->
 </bean>

 <bean id="myService" class="com.mycompany.myapp.service.MyService">
 </bean>
</beans>
Test Code
    Following output will be printed on console.
do logging
my service is called
do sysout
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mycompany.myapp.service.MyService;

public class AspectTest {

 public static void main(String[] args) {
  ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
  MyService service = (MyService) applicationContext.getBean("myService");
  service.doService();
 }
}

No comments:

Post a Comment