JoinPoint have the information about actual method call
which trigger this advice. If getter is calling JoinPoint have the information
about getter, if setter is called then JoinPoint have the information about
setter and if any other method is called then JoinPoint have the information about that method.
public void
LoggingAdvice(JoinPoint joinPoint){
//System.out.println(joinPoint.toString()
);
//System.out.println(joinPoint.getTarget()
); // it will return object
//Circle
circle = (Circle) joinPoint.getTarget();
}
JoinPoint the place where you can apply advice.
To Map the with arguments.
@Before("args(name)")
AopMain.java
package org.yash.watertechsol;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yash.watertechsol.service.ShapeService;
public class AopMain {
public static void main(String
args[]){
ApplicationContext
ctx = new ClassPathXmlApplicationContext("spring.xml");
ShapeService
shapeService = ctx.getBean("shapeService", ShapeService.class);
shapeService.getCircle().setName("Dummy
name");
System.out.println(shapeService.getCircle().getName());
}
}
Circle.java
package org.yash.watertechsol.model;
public class Circle {
private String name;
public void setName(String
name) {
this.name = name;
}
public String
getName() {
return name;
}
}
Triangle.java
package org.yash.watertechsol.model;
public class Triangle {
private String name;
public void setName(String
name) {
this.name = name;
}
public String
getName() {
return name;
}
}
ShapeService.java
package org.yash.watertechsol.service;
import org.yash.watertechsol.model.Circle;
import org.yash.watertechsol.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;
}
}
LoggingAspect.java
package org.yash.watertechsol.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 org.yash.watertechsol.model.Circle;
@Aspect
public class LoggingAspect {
@Before("allCicleMethods()")
public
void LoggingAdvice(JoinPoint joinPoint){
//System.out.println(joinPoint.toString()
);
//System.out.println(joinPoint.getTarget()
); // it will return object
//Circle
circle = (Circle) joinPoint.getTarget();
}
@Before("args(name)")
//public
void sringArgumentMethods(String name){
public
void sringArgumentMethods(String name){
//System.out.println("A
method that takes string argument has been called");
System.out.println("A
method that takes string argument has been called. The value is: " +
name);
}
@Pointcut("execution(*
get*())")
public
void allGetters(){}
//@Pointcut("execution(*
* org.yash.watertechsol.model.circle.*(..))")
@Pointcut("within(org.yash.watertechsol.model.Circle)")
public
void allCicleMethods(){}
}
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"
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 id="triangle"
class="org.yash.watertechsol.model.Triangle">
<property name="name"
value="Triangle Name"></property>
</bean>
<bean id="circle"
class="org.yash.watertechsol.model.Circle">
<property name="name"
value="Circle Name"></property>
</bean>
<bean id="shapeService"
class="org.yash.watertechsol.service.ShapeService"
autowire="byName"/>
<bean id="loggingAspect"
class="org.yash.watertechsol.aspect.LoggingAspect"/>
</beans>
No comments:
Post a Comment