How to declare Before and After Advice


This example shows how to write an aspect as a simple java class (i.e a POJO) with no AOP related info defined in it. AOP related info is defined declaratively in XML configuration file.

Following example contains an aspect named OurAspect.java which contains two advices named ourBeforeAdvice() and ourAfterAdvice(). These advices are applied to the methods (pointcuts) defined in XML file.

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.

Steps

1. Write EmployeeService interface and its implementation EmployeeServiceImpl.
2. Write MainApp class which uses the methods of  EmployeeService interface.
3. Write an aspect class named OurAspect.java.
4. Define pointcuts(methods) where advices would be applied 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




Code

EmployeeService.java

package examples.springaop.xml;

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

EmployeeServiceImpl.java

package examples.springaop.xml;

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;
       
    }
   
}

MainApp.java

package examples.springaop.xml;

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



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

OurAspect.java

package examples.springaop.xml;

public class OurAspect{
   
 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.");
   
 }
}

ApplicationContext.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"

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.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

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

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

<aop:config>

    <aop:aspect ref="empAspect">

    <aop:before pointcut= "execution(* examples.springaop.xml.EmployeeService.addEmployee(..))"

        method="ourBeforeAdvice"/>

<aop:after pointcut= "execution(* examples.springaop.xml.EmployeeService.addEmployee(..))"

        method="ourAfterAdvice"/>

</aop:aspect>

</aop:config>

</beans>


Output

    This is Before Advice, called before the method.

     addEmployee() called ! One Employee Added !

    This is After Advice, called after the method.


Jars Used
org.springframework.context-3.0.6.RELEASE.jar
org.springframework.core-3.0.6.RELEASE.jar
org.springframework.beans-3.0.6.RELEASE.jar
commons-logging-1.1.1.jar
org.springframework.asm-3.0.6.RELEASE.jar
org.springframework.aop-3.0.6.RELEASE.jar
com.springsource.org.aopalliance-1.0.0.jar
org.springframework.expression-3.0.6.RELEASE.jar
aspectjweaver-1.6.8.jar


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