Question

I am new in Spring AOP, and this is my codes for testing:

Target inside com.kk.entitypackage:

@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?

Was it helpful?

Solution

That's just a result of you performing output on two different output streams. You're writing to both err and out. These don't necessarily get flushed in the same order (between streams).

System.err.println(..);
System.out.println(..);

Write to the same OutputStream and you'll see your expected output.

If we remove the output containing stars, we get

value is:aopTargetOne address
value is:aopTargetOne email
value is:aopTargetOne name
value is:aopTargetTwo name
value is:aopTargetTwo email

which is in the exact order you set them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top