Monday, March 18, 2013

Spring Framework-Example to use JoinPoint in AOP


Hello Friends, After discussing the pointcuts in last post Now discuss on Joinpoint . A Joinpoint is a point in the execution of the application where an aspect can be plugged in. this point could be a method being called Here is a example to understands the use of pointcuts-



Circle.java-
 package com.test.App;

public class Circle {
String name;

public String getName() {
return name;
}

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

}

Triangle.java
package com.test.App;

public class Triangle {
String name;

public String getName() {
return name;
}

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

}

LogAspect.java
package com.test.aspect;

import org.aspectj.lang.JoinPoint;
//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()")
public void loggingAd(JoinPoint jp)
{
System.out.println(jp.toString());
 
   @Before("allcircle()")
public void loggingAdvice(JoinPoint jp)
{
System.out.println(jp.toString());
}
   @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");

   }
}

Shape.java
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;
}
}

Main.java-

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
execution(Triangle com.test.service.Shape.getTriangle())
execution(String com.test.App.Triangle.getName())
triangle drawn
execution(Circle com.test.service.Shape.getCircle())
execution(String com.test.App.Circle.getName())
execution(String com.test.App.Circle.getName())
circle drawn
JoinPoint class contain the information related to the object which pass by pointcut.


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.