Why spring aop advice is not triggering(invoking) for setter methods invoked by spring container ?
Problem : The actual problem is explained by using below example.
Bean class:
package org.example;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("In setter");
this.name = name;
}
}
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("In setter");
this.name = name;
}
}
Bean configuration in spring.xml
<aop:aspectj-autoproxy />
<bean id="triangle" class="org.example.Triangle" >
<property name="name" value="Right Angle Triangle"/>
</bean>
<bean id="firstaspect" class="org.example.aspects.SampleAspect"/>
<bean id="triangle" class="org.example.Triangle" >
<property name="name" value="Right Angle Triangle"/>
</bean>
<bean id="firstaspect" class="org.example.aspects.SampleAspect"/>
Aspect class:
package org.example.aspects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class SampleAspect {
@Before("execution(public void setName(..))")
public void testAdvice(JoinPoint joinPoint){
System.out.println("Aspect Signature "+joinPoint.getSignature());
}
}
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class SampleAspect {
@Before("execution(public void setName(..))")
public void testAdvice(JoinPoint joinPoint){
System.out.println("Aspect Signature "+joinPoint.getSignature());
}
}
spring client program:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPClient {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle=ctx.getBean("triangle",Triangle.class);
System.out.println("Triangle name: "+triangle.getName());
}
}
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AOPClient {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle=ctx.getBean("triangle",Triangle.class);
System.out.println("Triangle name: "+triangle.getName());
}
}
output :
In setter
In setter
Triangle name: Right Angle Triangle
As per the spring aop concept the adviser testAdvice must be called before executing the setter method setName(String name) but the adviser is not triggering.
because AOP Proxy works only after the bean complete initialization i.e after setting all the properties through DependecnyInjection.
after succesful initialization of bean if you invoke the setter method again from your client program then it'll trigger the advisor.
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle=ctx.getBean("triangle",Triangle.class);
triangle.setName("Equilateral Triangle");
System.out.println("Triangle name: "+triangle.getName());
}
output:
In setter //This output line is called through spring container
Aspect Signature String org.example.Triangle.getName()
In setter //This output line is called through AOPClient Class
Triangle name:Equilateral Triangle
No comments:
Post a Comment