AOP系列文章:
Spring AOP:http://ray-yui.iteye.com/blog/2024759
CGLIB:http://ray-yui.iteye.com/blog/2026426
Javassist:http://ray-yui.iteye.com/blog/2029261
什么是AOP?
AOP是对传统面向对象开发的一种有效的补充,在AOP中概念非常多,请容许笔者省略数千字的概念,只举出一个示例,面向对象中的类就好比是士兵,我们编写每个类就等同于为每个士兵增加装备和训练士兵的体能,从而达到可以上战场打仗的目的,然而有一天AOP这个魔法师出现了,打开了一个魔法门,告诉每个士兵(类),不用再每个士兵单独这样训练了(编码),从我这个魔法门(切面)走过就能增强士兵自身的能力,而这种方式,就是AOP,面向切面编程
AOP的实现方式:
Spring中有AOP的实现,但Spring的AOP更多是借鉴了AspectJ的方式,SpringAOP是基于动态代理的,所以只是方法级别的连接点模型,无法做到例如字段或构造函数的接入点,无法让我们创建更细粒度的通知,若然读者有需要更精确的AOP,请考虑使用AspectJ或JBoss的AOP实现
SpringAOP使用(基于XML)
- 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);
- }
- }
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); } }
- <!-- 声明Bean -->
- <bean id="advice" class="com.accentrix.ray.Advice" />
- <!-- 配置AOP -->
- <aop:config>
- <!-- 引用注册的Bean ID -->
- <aop:aspect ref="advice">
- <!-- 声明切入点,过滤service下的所有类型和所有方法,不限制参数和返回类型 -->
- <aop:pointcut expression="execution(* com.accentrix.ray.service.*.*(..))"
- id="servicePoincut" />
- <!-- 引用servicePoincut的切入点,调用advice的beforeMethod方法进行前置处理 -->
- <aop:before pointcut-ref="servicePoincut" method="beforeMethod" />
- <!-- 引用servicePoincut的切入点,调用advice的afterMethod方法进行后置处理 -->
- <aop:after pointcut-ref="servicePoincut" method="afterMethod" />
- <!-- 引用servicePoincut的切入点,调用advice的exceptionMethod方法进行异常处理 -->
- <aop:after-throwing pointcut-ref="servicePoincut"
- method="exceptionMethod" />
- <!-- 引用servicePoincut的切入点,调用advice的aroundMethod方法进行环绕处理 -->
- <aop:around pointcut-ref="servicePoincut" method="aroundMethod" />
- <!-- 使用如下pointcut即可为advice传递调用真实业务时传递的参数 -->
- <aop:after
- pointcut="execution(* com.accentrix.ray.service.*.*(String)) and args(name)"
- method="hasParamterAfterMethod" />
- </aop:aspect>
- </aop:config>
<!-- 声明Bean --> <bean id="advice" class="com.accentrix.ray.Advice" /> <!-- 配置AOP --> <aop:config> <!-- 引用注册的Bean ID --> <aop:aspect ref="advice"> <!-- 声明切入点,过滤service下的所有类型和所有方法,不限制参数和返回类型 --> <aop:pointcut expression="execution(* com.accentrix.ray.service.*.*(..))" id="servicePoincut" /> <!-- 引用servicePoincut的切入点,调用advice的beforeMethod方法进行前置处理 --> <aop:before pointcut-ref="servicePoincut" method="beforeMethod" /> <!-- 引用servicePoincut的切入点,调用advice的afterMethod方法进行后置处理 --> <aop:after pointcut-ref="servicePoincut" method="afterMethod" /> <!-- 引用servicePoincut的切入点,调用advice的exceptionMethod方法进行异常处理 --> <aop:after-throwing pointcut-ref="servicePoincut" method="exceptionMethod" /> <!-- 引用servicePoincut的切入点,调用advice的aroundMethod方法进行环绕处理 --> <aop:around pointcut-ref="servicePoincut" method="aroundMethod" /> <!-- 使用如下pointcut即可为advice传递调用真实业务时传递的参数 --> <aop:after pointcut="execution(* com.accentrix.ray.service.*.*(String)) and args(name)" method="hasParamterAfterMethod" /> </aop:aspect> </aop:config>
SpringAOP基于Annotation方式
- <!-- 需要在applicationContext.xml中增加如下配置 -->
- <aop:aspectj-autoproxy />
<!-- 需要在applicationContext.xml中增加如下配置 --> <aop:aspectj-autoproxy />
- 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);
- }
- }
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); } }
总结:AOP对面向对象的士兵式编程提供了更好的补充,亦在广大的开源框架有显著的效果,例如Spring Security就以AOP为核心,在业务程序开发当中AOP亦到处存在,例如拦截并缓存关于菜单和数据字典的数据,例如声明式事务,而Annotation的出现更加为快速便捷开发提供了很好的支持,从上面两种方式对比就得出Annotation的声明更加简洁方便,所以在业务当中有需要使用自定义的AOP建议采用Annotation的方式,但XML文件的方式亦必须掌握,因为我们时常需要使用到其他框架提供的切面
相关推荐
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
"spring-aop-jar"这个主题涉及到Spring框架中的核心组件之一——Spring AOP。这里我们将深入探讨Spring AOP、相关jar文件以及它们在实际开发中的作用。 首先,我们来看一下提供的文件: 1. aopalliance.jar:这是一...
赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...
spring-aop-5.3.22.jar Spring AOP provides an Alliance-compliant aspect-oriented programming implementation allowing you to define method interceptors and pointcuts to cleanly decouple code that ...
赠送jar包:spring-aop-5.3.10.jar; 赠送原API文档:spring-aop-5.3.10-javadoc.jar; 赠送源代码:spring-aop-5.3.10-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.10.pom; 包含翻译后的API文档:spring...
spring-aop-5.2.0.RELEASE
spring-aop-3.2.0.RELEASE.jar,一个Spring中AOP的jar包
赠送jar包:spring-aop-5.2.15.RELEASE.jar; 赠送原API文档:spring-aop-5.2.15.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.15.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.15.RELEASE....
spring-aop-3.2.5.RELEASE.jar
spring-aop-4.0.4.RELEASE 的jar包,亲测可用。。。。
赠送jar包:spring-aop-5.1.3.RELEASE.jar; 赠送原API文档:spring-aop-5.1.3.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.1.3.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.1.3.RELEASE.pom;...
赠送jar包:spring-aop-4.3.20.RELEASE.jar; 赠送原API文档:spring-aop-4.3.20.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.3.20.RELEASE-sources.jar; 包含翻译后的API文档:spring-aop-4.3.20.RELEASE-...
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
赠送jar包:spring-aop-4.2.2.RELEASE.jar; 赠送原API文档:spring-aop-4.2.2.RELEASE-javadoc.jar; 赠送源代码:spring-aop-4.2.2.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-4.2.2.RELEASE.pom;...
spring-aop-5.0.4.RELEASE.jar。
赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...
spring-aop-6.0.2.jar
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....