Sunday, March 17, 2013

Spring Framework- Example to Use Pointcuts in AOP


In Aspect Oriented Programming (AOP) Advice define the what and when we call . The Pointcuts define where.A pointcut definition matches one or more jointpoints .Here discuss a simple example to use pointcut -

Directory Structure-


Circle Class-


package com.test.App;

public class Circle {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Triangle Class-

package com.test.App;

public class Triangle {
String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}
Class shape-

package com.test.service;

import com.test.App.Circle;
import com.test.App.Triangle;

public class Shape {

Circle circle;
Triangle triangle;
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}

}

Class LogAspect-

package com.test.aspect;

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

@Aspect
public class LogAspect {

   @Before("allGetters() && allcircle()")
public void loggingAdvice()
{
System.out.println("Wel Come With....logging advice");
}
 
   @Pointcut("execution(* get*())")
public void allGetters()
{
System.out.println("Wel Come With....allgetters");

}
 
   @Pointcut("within(com.test.App.Circle)")
   public void allcircle()
   {
System.out.println("Wel Come With....circle");

   }
}
Class Main-

package com.test.aop;

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

import com.test.service.Shape;

public class Main {


public static void main(String[] args) {

ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Shape sh=context.getBean("shape", Shape.class);
System.out.println(sh.getTriangle().getName());
System.out.println(sh.getCircle().getName());
}

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

  <aop:aspectj-autoproxy/>
<bean id="triangle" class="com.test.App.Triangle">
 <property name="name" value="triangle drawn"></property>
 </bean>
 <bean id="circle" class="com.test.App.Circle">
  <property name="name" value="circle drawn"></property>
 </bean>
<bean id="shape" class="com.test.service.Shape" autowire="byName">
</bean>
<bean id="log" class="com.test.aspect.LogAspect"></bean>
</beans>


Output
triangle drawn
Wel Come With....logging advice
circle drawn
Here logginAdvice() combind all type of getter and setter method and call the function.Method allCircle() call when Circle class's methods calls.so all other methods indirectly pass to logginAdvice().


For Further Reading,
General, Java, spring

0 comments:

Post a Comment


 

Site Status

Man Behind Technical Today

Hello, I am Navin Bansal. I am a student of MCA in Rajsthan Institute of Engineering and Technology and owner of this blog. I share my view and ideas among people.