I am new in Spring AOP, and this is my codes for testing:
Target inside com.kk.entity
package:
@Component
public class AopTargetOne {
private String name;
private String password;
private String email;
private String address;
//getter and setters omitted
}
@Component
public class AopTargetSecond {
private String name;
private int age;
private String email;
//getter and setters omitted
}
Aspect:
@Component
@Aspect
public class VariableCheckAspect {
//intercept all the setter methods
@Pointcut("execution(* com.kk.entity.*.set*(..))")
private void aopIsSetMethod() {
}
@Before("com.kk.aop.VariableCheckAspect.aopIsSetMethod() && args(val,..)")
public void checkSetValue(JoinPoint joinpoint, String val) {
System.err.println("******** Start check set value with method *********** " + joinpoint.getSignature());
System.out.println("value is:" + val);
System.err.println("******** End ****");
}
}
Application:
AopTargetOne ah = context.getBean(AopTargetOne.class);
ah.setAddress("aopholder address");
ah.setEmail("aopholder email");
ah.setName("aopholder name");
AopTargetSecond ak = (AopTargetSecond) context.getBean("aopTargetSecond");
ak.setName("aopkepper name");
ak.setEmail("aopkepper email");
ak.setAge(23);
And I got the output:
******** Start check set value with method *********** void com.kk.entity.AopTargetOne.setAddress(String)
******** End ****
value is:aopTargetOne address
******** Start check set value with method *********** void com.kk.entity.AopTargetOne.setEmail(String)
******** End ****
******** Start check set value with method *********** void com.kk.entity.AopTargetOne.setName(String)
******** End ****
******** Start check set value with method *********** void com.kk.entity.AopTargetTwo.setName(String)
******** End ****
value is:aopTargetOne email
value is:aopTargetOne name
value is:aopTargetTwo name
******** Start check set value with method *********** void com.kk.entity.AopTargetTwo.setEmail(String)
******** End ****
value is:aopTargetTwo email
Which confused me! It seems that the code does not run with the normal order.
While I expect the output like this:
******** Start check set value with method *********** void com.kk.entity.AopTargetOne.setAddress(String)
value is:aopTargetOne address
******** End ****
******** Start check set value with method *********** void com.kk.entity.AopTargetOne.setEmail(String)
value is:aopTargetOne email
******** End ****
....
What's the problem? Any way to fix this?