Spring AOP - Before Advice Example With @Before Annotation

In this tutorial, we will show you a step by step example declaring before advice with @Before annotation using Spring AOP and @AspectJ annotation style. Before advice is executed before join point but which does not have an ability to prevent execution flow proceeding to the join point. In Spring AOP, join point always represents a method execution.

You may want to explore examples for different types of advice and here is the complete list for you


Project Dependencies


Create following pom.xml containing required dependencies to execute the project.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.javakart.springaop</groupId>
 <artifactId>before-advice</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>3.2.3.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aspects</artifactId>
   <version>3.2.3.RELEASE</version>
  </dependency>
 </dependencies>
</project>

Service Class Containing Join point


package com.javakart.springaop.service;
import org.springframework.stereotype.Service;

@Service
public class MyService {

 public void doBusiness(){
  System.out.println("Business service is executed");
 }
}

Aspect Class Containing Before Advice


package com.javakart.springaop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class BeforeAspect {

 @Before("execution(* com.javakart.springaop.service.MyService.doBusiness())")
 public void beforeBusiness(){
  System.out.println("before aspect is called");
 }
}

Above before advice is applied on doBusiness method of MyService class. Following are few other useful point cut expressions. For detailed point cut expression examples, please see pointcut expressions 

  • Any public method within MyService class

          @Before("execution(* com.javakart.springaop.service.MyService.*(..)")

  • Any public method within com.javakart.springaop.service package
         @Before("execution(* com.javakart.springaop.service.*.*(..)")

Spring Context File

<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/>
 <bean id="beforeAspect" class="com.javakart.springaop.aspect.BeforeAspect">
  <!-- configure properties of aspect here as normal -->
 </bean>
 
 <bean id="myService" class="com.javakart.springaop.service.MyService">
 </bean>
</beans>

Program Execution

package com.javakart.springaop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.javakart.springaop.service.MyService;

public class ExecuteTest {

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

Output

before aspect is called
Business service is executed

Download the source code

No comments:

Post a Comment