Pin It

How to implement Before Advice using @AspectJ Annotation-Driven AOP in Java ?.

A simple application to demonstrate how to implement Before Advice using @AspectJ Annotation-Driven AOP in Java.

Step 1:- Create a Interface Subtraction.java

package com.hubberspot.aspectj.annotation.beforeadvice;

// Its a simple interface for the Subtraction service.
// It contains one single method called as subtract().
// This method takes in two arguments both of the type
// int.

public interface Subtraction {

 public void subtract(int a , int b);

}




Step 2:- SubtractionImpl.java Service Implementation Class

package com.hubberspot.aspectj.annotation.beforeadvice;

// It is the implementation class for the 
// Subtraction service. It just calculates and prints 
// subtraction of two numbers passed to it as arguments.
// After printing subtraction on the console it just
// throws new RuntimeException().

public class SubtractionImpl implements Subtraction {

 @Override
 public void subtract(int a, int b) {

  if(a > b) {

   System.out.println((a-b));

  }
  else {

   System.out.println((b-a));

  }
 }

}



Step 3:- Before Advice Implementation class

package com.hubberspot.aspectj.annotation.beforeadvice;

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

// @Aspect annotation treats this Java class as 
// Aspect. Its not ordinary POJO class.
@Aspect
public class BeforeSubtractAdvice {

 // Method beforeSubtractAdvice() is a before advice
 // implemented by providing @Before annotation.
 // This annotation takes in Pointcut expression, which 
 // indicates when this advice executes.
 // This Pointcut expression tells that beforeSubtractAdvice()
 // before subtract method of Subtraction interface.
 // The before advice takes in JoinPoint which here represents
 // method execution.
 @Before("execution(* com.hubberspot.aspectj.annotation.beforeadvice.Subtraction.subtract(..))")
 public void beforeSubtractAdvice(JoinPoint joinPoint) {

  System.out.println("Call Before Subtract method ... ");

 }

}



Step 4 :- Spring Configuration file



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.

<?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.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">


    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

    <!-- Implementation Class -->
    <bean id="subtract"
 class="com.hubberspot.aspectj.annotation.beforeadvice.SubtractionImpl" />

    <bean id="beforeAdvice"
 class="com.hubberspot.aspectj.annotation.beforeadvice.BeforeSubtractAdvice" />

</beans>


Step 5:- Test class

package com.hubberspot.aspectj.annotation.beforeadvice;

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

public class SubtractionTest {

 public static void main(String[] args) {

  // ApplicationContext is a Spring interface which 
  // provides with the configuration for an application. 
  // It provides us with all the methods that BeanFactory 
  // provides. It loads the file resources in a older 
  // and generic manner. It helps us to publish events to the
  // listener registered to it. It also provides quick support
  // for internationalization. It provides us with the object 
  // requested, it reads the configuration file and provides
  // us with the necessary object required.
  // We are using concrete implementation of AbstractApplicationContext
  // here called as ClassPathXmlApplicationContext because this 
  // bean factory reads the xml file placed in the classpath of 
  // our application. We provide ClassPathXmlApplicationContext
  // with a configuration file called as after_returning_advice.xml placed
  // at classpath of our application.

  ApplicationContext context = 
    new ClassPathXmlApplicationContext("before_advice.xml");

  // In order to get a object instantiated for a particular bean 
  // we call getBean() method of ClassPathXmlApplicationContext
  // passing it the id for which the object is to be needed. 
  // Here getBean() returns an Object. We need to cast it back 
  // to the Subtraction object. Without implementing new keyword we 
  // have injected object of Subtraction just by reading an xml 
  // configuration file.

  Subtraction subtract = (Subtraction)context.getBean("subtract");

  subtract.subtract(10, 6);
  subtract.subtract(5, 10);

 }
}



Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot