Monday, March 25, 2013

Spring Framework - Example to use @Before annotation with args() in AOP


Here In AOP, we discuss that how use @Before(args()).Here args() is contain the name of arguments with pass to the function and also can use that argument.Now see a example to understand that-



Class circle.java-

package com.test.App;

public class Circle {
String name;

public String getName() {
return name;
}

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

}
Class Triangle.java-

package com.test.App;

public class Triangle {
String name;

public String getName() {
return name;
}

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

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

}
Class LogAspect.java-

package com.test.aspect;

import org.aspectj.lang.JoinPoint;
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(JoinPoint joinpoint)
{
System.out.println("joinpoint calls");
System.out.println(joinpoint.toString());
System.out.println(joinpoint.getTarget());

}
 
   @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");

   }
 

 @Before("args(name)")
   public void strigArgs(String name)
   {
  System.out.println("string args is used"+name);
   }
}

Here is necessary that String(name) and args (name) . Both underline parameter name is same.

Class 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());
sh.getCircle().setName("navin's");
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
string args is usednavin's
joinpoint calls
execution(String com.test.App.Circle.getName())
com.test.App.Circle@b1074a
navin's
Here define that how can use pointcuts ans joinpoint also,and how we can access the argument using annotations.


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.