@After的实现源码
@Afterruning的实现源码
@After:是在finally中执行的
@Afterruning:是在被代理方法返回后执行
案例:
@Aspect
@Component
public class YjzAspect {
@AfterReturning("execution(public void com.yjz.service.UserService.test())")
//@After("execution(public void com.yjz.service.UserService.test())")
public void afterReturning(){
System.out.println("方法返回后执行");
}
}
@Component
public class UserService {
public void test(){
System.out.println("test");
//throw new NullPointerException();
}
}
public class Test {
public static void main(String[] args) {
// 创建一个Spring容器
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = (UserService) applicationContext.getBean("userService");
userService.test();
}
}
@AfterReturning执行结果:
@After执行结果: