We'll now learn about join points, and how we can use arguments in the advice methods to get information about join points.
package com.venkat.Spring.Aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.venkat.Spring.Aop.service.ShapeService;
public class AopTest {
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ShapeService service=context.getBean("shapeService",ShapeService.class);
service.getCircle().setName("Dummy Circle");
System.out.println(service.getCircle().getName());
}
}
************************************************************************
package com.venkat.Spring.Aop.Aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import com.venkat.Spring.Aop.model.Circle;
@Aspect
public class LoggingAspect {
@Before("allCircleMethods()")
public void loggingAdvice(JoinPoint joinpoint)
{
System.out.println("Run Advice..... get method Executed");
System.out.println(joinpoint.toString());
Circle c=(Circle)joinpoint.getTarget();
System.out.println(c.getName());
}
// @Before("args(name)")
// public void allStringMethodArguments(String name){
//
// System.out.println("A setter method has been executed............"+name);
// }
@Before("args(String)")
public void allStringMethodArguments(){
System.out.println("A setter method has been executed............");
}
@Pointcut("within(com.venkat.Spring.Aop.model.*)")
public void allCircleMethods(){}
}
***************************************************************************
package com.venkat.Spring.Aop.model;
public class Circle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
***************************************************************************
package com.venkat.Spring.Aop.model;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
*************************************************************************
package com.venkat.Spring.Aop.service;
import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;
public class ShapeService {
private Triangle triangle;
private Circle circle;
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
}
**********************************************************************
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
<property name="name" value="triangle name"/>
</bean>
<bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<property name="name" value="circle name"/>
</bean>
<bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
<property name="triangle" ref="triangle"/>
<property name="circle" ref="circle"/>
</bean>
<bean name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>
package com.venkat.Spring.Aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.venkat.Spring.Aop.service.ShapeService;
public class AopTest {
public static void main(String args[])
{
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
ShapeService service=context.getBean("shapeService",ShapeService.class);
service.getCircle().setName("Dummy Circle");
System.out.println(service.getCircle().getName());
}
}
************************************************************************
package com.venkat.Spring.Aop.Aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import com.venkat.Spring.Aop.model.Circle;
@Aspect
public class LoggingAspect {
@Before("allCircleMethods()")
public void loggingAdvice(JoinPoint joinpoint)
{
System.out.println("Run Advice..... get method Executed");
System.out.println(joinpoint.toString());
Circle c=(Circle)joinpoint.getTarget();
System.out.println(c.getName());
}
// @Before("args(name)")
// public void allStringMethodArguments(String name){
//
// System.out.println("A setter method has been executed............"+name);
// }
@Before("args(String)")
public void allStringMethodArguments(){
System.out.println("A setter method has been executed............");
}
@Pointcut("within(com.venkat.Spring.Aop.model.*)")
public void allCircleMethods(){}
}
***************************************************************************
package com.venkat.Spring.Aop.model;
public class Circle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
***************************************************************************
package com.venkat.Spring.Aop.model;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
*************************************************************************
package com.venkat.Spring.Aop.service;
import com.venkat.Spring.Aop.model.Circle;
import com.venkat.Spring.Aop.model.Triangle;
public class ShapeService {
private Triangle triangle;
private Circle circle;
public Triangle getTriangle() {
return triangle;
}
public void setTriangle(Triangle triangle) {
this.triangle = triangle;
}
public Circle getCircle() {
return circle;
}
public void setCircle(Circle circle) {
this.circle = circle;
}
}
**********************************************************************
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy/>
<bean name="triangle" class="com.venkat.Spring.Aop.model.Triangle">
<property name="name" value="triangle name"/>
</bean>
<bean name="circle" class="com.venkat.Spring.Aop.model.Circle">
<property name="name" value="circle name"/>
</bean>
<bean name="shapeService" class="com.venkat.Spring.Aop.service.ShapeService">
<property name="triangle" ref="triangle"/>
<property name="circle" ref="circle"/>
</bean>
<bean name="loggingAdvice" class="com.venkat.Spring.Aop.Aspect.LoggingAspect"/>
</beans>
No comments:
Post a Comment