1、@AfterReturning有两个参value、returning,vlue=excution()标明切片的切入位置,returning切入的目标类的返回值。@AfterReturning注解的切面方法是在目标方法执行后执行。
doOther方法是接口的实现方法,该方法新建并且返回一个对象student
public Student doOther(String name, String age) {
Student s = new Student();
s.setAge(age);
s.setName(name);
return s;
}
doLogsAfter是一个切面方法,
@AfterReturning(value = "execution(* *..doOther2(String,String))",returning = "res")
public static void doLogsAfter(JoinPoint joinPoint,Student res){
System.out.println("没修改之前" + res.getAge());
System.out.println("业务之后执行了日志记录" + new Date());
res.setAge("18");
}
2、xml配置文件省略
3、当调用方法doOther()的时候传入参数age和name,在堆区中存放了Student对象age=21,name=ljq。最后由于在doOther()方法执行后再执行切面方法,修改了age=18。最后age的输出结果为18。
String config = "ApplicationConfig.xml";
ApplicationContext ac = new ClassPathXmlApplicationContext(config);
DoService ds = (DoService) ac.getBean("Myservice");
ds.doServiceAop("ljq");
Student s = ds.doOther("ljq","21");
System.out.println(s.getAge());
输出结果:
21
业务之后执行了日志记录Sun Jun 06 12:56:09 CST 2021
18