Java springaop执行命令
我是Spring AOP的新手,这是我的测试代码:Java springaop执行命令,java,spring,aop,spring-aop,Java,Spring,Aop,Spring Aop,我是Spring AOP的新手,这是我的测试代码: com.kk.entity包内的目标: @Component public class AopTargetOne { private String name; private String password; private String email; private String address; //getter and setters omitted } @Component public cl
com.kk.entity
包内的目标:
@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
}
方面:
@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 ****");
}
}
应用程序:
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);
我得到了输出:
******** 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
这让我很困惑!代码似乎没有按正常顺序运行
虽然我希望输出如下所示:
******** 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 ****
....
有什么问题吗?有没有办法解决这个问题?这只是在两个不同的输出流上执行输出的结果。您正在写入
err
和out
写入相同的OutputStream
,您将看到预期的输出
如果我们去掉包含星星的输出,我们得到
value is:aopTargetOne address
value is:aopTargetOne email
value is:aopTargetOne name
value is:aopTargetTwo name
value is:aopTargetTwo email
也可以使用System.out.flush()和System.err.flush()进行刷新
value is:aopTargetOne address
value is:aopTargetOne email
value is:aopTargetOne name
value is:aopTargetTwo name
value is:aopTargetTwo email