==================== Spring AOP切入点增强 ==================== | |__before - 前置增强(通知) |__after - 后置增强(通知) |__after-throwing - 异常增强(通知) |__after-returning - 返回增强(通知) |__around - 环绕增强(通知) 在cn.tedu.springaop.util包中创建类: public class Pointcuts { public void pointcut(boolean throwException) { System.out.println("-切入点-"); if (throwException) throw new NullPointerException(); } } 在cn.tedu.springaop.aspect包中创建类: public class Advices { public void before() { System.out.println("前置增强"); } public void after() { System.out.println("后置增强"); } public void after_throwing() { System.out.println("异常增强"); } public void after_returning() { System.out.println("返回增强"); } public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("环增切前"); Object retval = pjp.proceed(); // 执行切入点 System.out.println("环增切后"); return retval; } } 配置Bean和AOP: ... ... ... ... ... ... 测试: public class AdvicesTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Pointcuts pointcuts = context.getBean("pointcuts", Pointcuts.class); pointcuts.pointcut(true); } } 前置增强 环增切前 -切入点- 异常增强 后置增强 Exception in thread "main" java.lang.NullPointerException ... 测试: public class AdvicesTest { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); Pointcuts pointcuts = context.getBean("pointcuts", Pointcuts.class); pointcuts.pointcut(false); } } 前置增强 环增切前 -切入点- 环增切后 返回增强 后置增强 例程:SpringAOP 拦截器中的代码类似下面这个样子: try { before(); // 前置增强 around(); // 环增切前 // -切入点- -- // 环增切后 |抛 after_returning(); // 返回增强 |出 } |异 catch (Exception e) { |常 after_throwing(); // 异常增强 <- } finally { after(); // 后置增强 }