实现AOP — Spring AOP
[size=large] AOP系列文章: Spring AOP:[url]http://ray-yui.iteye.com/blog/2024759[/url] CGLIB:[url]http://ray-yui.iteye.com/blog/2026426[/url] Javassist:[url]http://ray-yui.iteye.com/blog/2029261[/url] [/size] [size=large] 什么是AOP? AOP是对传统面向对象开发的一种有效的补充,在AOP中概念非常多,请容许笔者省略数千字的概念,只举出一个示例,[b][color=red]面向对象中的类就好比是士兵,我们编写每个类就等同于为每个士兵增加装备和训练士兵的体能,从而达到可以上战场打仗的目的,然而有一天AOP这个魔法师出现了,打开了一个魔法门,告诉每个士兵(类),不用再每个士兵单独这样训练了(编码),从我这个魔法门(切面)走过就能增强士兵自身的能力[/color][/b],而这种方式,就是AOP,面向切面编程 [/size] [size=large] AOP的实现方式: Spring中有AOP的实现,但Spring的AOP更多是借鉴了AspectJ的方式,[b][color=red]SpringAOP是基于动态代理的,所以只是方法级别的连接点模型,无法做到例如字段或构造函数的接入点,无法让我们创建更细粒度的通知[/color][/b],若然读者有需要更精确的AOP,请考虑使用AspectJ或JBoss的AOP实现 [/size] [size=large] [b][color=red]SpringAOP使用(基于XML)[/color][/b] [/size] [code="java"] package com.accentrix.ray; import org.aspectj.lang.ProceedingJoinPoint; package com.accentrix.ray; import org.aspectj.lang.ProceedingJoinPoint; /* * 声明切面(Aspect) */ public class Advice { // 前置执行方法 public void beforeMethod() { System.out.println("method before execute"); } // 后置执行方法 public void afterMethod() { System.out.println("method after execute"); } // 抛出异常时方法 public void exceptionMethod() { System.out.println("method throw exception execute"); } // 环绕通知 public void aroundMethod(ProceedingJoinPoint joinPoint) { /* * 环绕通知就类似Filter,在一个方法中包含开始,执行,结束,抛出异常 , * 甚至可以不调用joinPoint的proceed方法执行真实逻辑 亦可以多次调用 * ,全由开发者业务逻辑决定 */ try { System.out.println("before"); joinPoint.proceed(); System.out.println("after"); } catch (Throwable e) { e.printStackTrace(); System.out.println("throw exception"); } } // 接受参数通知 public void hasParamterAfterMethod(String name) { System.out.println("before method " + name); } } [/code] [code="xml"] [/code] [size=large] [b][color=red] SpringAOP基于Annotation方式[/color][/b] [/size] [code="xml"] [/code] [code="java"] package com.accentrix.ray; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class AnnotationAdvice { // 声明切入点 @Pointcut("execution(* com.accentrix.ray.service.*.*(..))") public void myPointcut() { } // 可声明多个切入点 @Pointcut("execution(* com.accentrix.ray.service.*.*(String) and args(name)") public void hasParamterPointcut(String name) { } // 注解形式前置通知 @Before("myPointcut()") public void beforeMethod() { System.out.println("before method execute"); } // 注解形式后置通知 @After("myPointcut()") public void afterMethod() { System.out.println("after method execute"); } // 注解形式异常通知 @AfterThrowing("myPointcut()") public void exceptionMethod() { System.out.println("method throw exception execute"); } // 注解形式环绕通知 @Around("myPointcut()") public void aroundMethod(ProceedingJoinPoint joinPoint) { try { System.out.println("before"); joinPoint.proceed(); System.out.println("after"); } catch (Throwable e) { e.printStackTrace(); System.out.println("throw exception"); } } // 注解形式传递参数给通知 @Before("hasParamterPointcut(name)") public void hasParamterBeforeMethod(String name) { System.out.println("before method execute " + name); } } [/code] [size=large] 总结:[b][color=red]AOP对面向对象的士兵式编程提供了更好的补充[/color][/b],亦在广大的开源框架有显著的效果,例如Spring Security就以AOP为核心,在业务程序开发当中AOP亦到处存在,例如拦截并缓存关于菜单和数据字典的数据,例如声明式事务,而Annotation的出现更加为快速便捷开发提供了很好的支持,从上面两种方式对比就得出Annotation的声明更加简洁方便,[b][color=red]所以在业务当中有需要使用自定义的AOP建议采用Annotation的方式,但XML文件的方式亦必须掌握,因为我们时常需要使用到其他框架提供的切面[/color][/b] [/size]
个人觉得只是在满足一种普遍的需求,没有任何新增的思想,只是在与spring的依赖注入结合后实现起来非常方便,看起来好像是新的。如果没有spring,大家也会用代理自己实现一个。
AOP是一种思想,spring实现该思想的主要技术是动态代理。
只是代理模式的应用,谈不上补充面向对象的开发方式。