package xin.students.aopxml;
//被增强的类
public class Book {
public void buy() {
System.out.println("buy");
}
}
创建被增强类
package xin.students.aopxml;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
public class BookProxy {
public void before() {
System.out.println("before.....................");
}
//后置通知,方法返回结果后执行
public void afterReturning() {
System.out.println("afterReturning.....................");
}
//最终通知,方法结束后通知
public void after() {
System.out.println("after.....................");
}
//异常通知
public void afterThrowing() {
System.out.println("afterThrowing.....................");
}
//环绕通知
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("环绕前通知.....................");
proceedingJoinPoint.proceed(); //执行被增强的方法
//当发生异常时不会执行下面的代码和后置通知,只会执行最终通知
System.out.println("环绕后通知.....................");
return null;
}
}
创建测试方法
package xin.students.aopxml;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("aopxml.xml");
Book book = ac.getBean("book", Book.class);
book.buy();
}
}