How to declare Before and After Advice using @AspectJ

This example shows how to use annotations to write an aspect using @AspectJ feature which was introduced in AspectJ 5. With this we do not need to define the advices and pointcuts in XML file.

In following example we have written an aspect named which contains two advices named ourBeforeAdvice() and ourAfterAdvice().

ourBeforeAdvice() is a before-advice called every time before a method starts its execution and ourAfterAdvice() is a after-advice called every time after a method completes its execution.


1) Write EmployeeService interface and its implementation EmployeeServiceImpl.
2) Write MainApp class which use methods of  EmployeeService interface.
3) Write aspect class named using annotations.
4) Enabling @AspectJ support in XML file

Technologies Used
1) Spring-framework-3.0.6.RELEASE
2) Eclipse Java EE IDE Helios Release (Eclipse version 3.6)
3) Java SE 6

package examples.springaop.anno;

public interface EmployeeService{
    public void addEmployee();
    public String getEmployeebyName(String id);

package examples.springaop.anno;

public class EmployeeServiceImpl implements EmployeeService{
    public void addEmployee(){
        System.out.println("addEmployee() called ! One Employee Added !");
    public String getEmployeebyName(String id){
        String empName = "Name"+id;
        return empName;

package examples.springaop.anno;

import org.springframework.context.ApplicationContext;

public class MainApp{
public static void main(String args[]){
    ApplicationContext appContext = new ClassPathXmlApplicationContext("resources/ApplicationContext.xml");
    EmployeeService empService = (EmployeeService)appContext.getBean("empService");

package examples.springaop.anno;

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

public class OurAspect{

@Pointcut("execution(* examples.springaop.xml.EmployeeService.addEmployee(..))")
public void addEmpPointcut(){

 public void ourBeforeAdvice(){
     System.out.println("\n\nThis is Before Advice, called before the method.\n");

public void ourAfterAdvice(){
    System.out.println("\nThis is After Advice, called after the method.");

Here we have used @Aspect annotation on class OurAspect  to declare it as an aspect.

Next we have used @Pointcut annotation to declare pointcuts. @Pointcut is declared on methods. Here method names will be treated as pointcut ids, similar to what we do in :

tag.(see example).

We have used @Before and @After annotations to mark before and after advices respectively.


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=""




<bean id="empService" class="examples.springaop.anno.EmployeeServiceImpl"/>

<bean id="empAspect" class="examples.springaop.anno.OurAspect"/>



For enabling @AspectJ support we have added following in XML file.


This will create proxies for the methods which are specified in pointcuts so that advice logic would be called before/after the method execution.

Note here pointcuts and advices are not defined in XMl file.

This is Before Advice, called before the method.

addEmployee() called ! One Employee Added !

This is After Advice, called after the method.

Jars Used


I would like to know your comments and if you liked the article then please share it on social networking buttons.

No comments:

Post a Comment