- 浏览: 216649 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
zi_wu_xian:
顶5楼,POI操作word和excel还是太复杂了,并且针对d ...
使用POI操作Excel和Word -
贝塔ZQ:
poi操作word、excel代码好多啊,用插件试试吧,网上不 ...
使用POI操作Excel和Word -
wap816:
@CacheEvict(value = ...
SpringEL详解及应用 -
string2020:
List<Map<String,Map<St ...
开源工具 — Apache Commons Collections -
uniqueX:
mark!
并发编程 — 判断线程安全
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); } }
<!-- 声明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 />
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文件的方式亦必须掌握,因为我们时常需要使用到其他框架提供的切面
评论
3 楼
hyj1254
2014-03-04
freezingsky 写道
AOP是一种思想,spring实现该思想的主要技术是动态代理。
个人觉得只是在满足一种普遍的需求,没有任何新增的思想,只是在与spring的依赖注入结合后实现起来非常方便,看起来好像是新的。如果没有spring,大家也会用代理自己实现一个。
2 楼
freezingsky
2014-03-03
AOP是一种思想,spring实现该思想的主要技术是动态代理。
1 楼
hyj1254
2014-03-03
只是代理模式的应用,谈不上补充面向对象的开发方式。
发表评论
-
并发编程 — 实现线程安全
2015-08-12 09:03 2261并发编程系列文章: 初解线程池:http://r ... -
并发编程 — 优化小技巧
2015-08-10 23:54 0并发编程系列文章: 初解线程池:http://r ... -
并发编程 — 判断线程安全
2015-08-05 10:11 5454并发编程系列文章: ... -
并发编程 — volatile
2015-07-30 10:11 2140并发编程系列文章: 初解线程池:http://r ... -
数据库储存不确定实体
2014-07-08 09:52 2223相信在项目开发当中都曾经遇到过,有某些要储蓄到数 ... -
同步操作降低效率解惑
2014-06-30 10:20 3244相信在读者刚接触Java的时候,都曾经学习到线程 ... -
并发编程 — 并发数据结构
2014-06-24 09:58 5504并发编程系列文章: 初解线程池:http://r ... -
并发编程 — 并发数据类型
2014-06-16 10:03 5396并发编程系列文章: 初解线程池:http://r ... -
并发编程 — 详解线程池
2014-06-03 09:51 5959本文章需要对JDK5 Executor框架有所了解,请读者先 ... -
并发编程 — 初解线程池
2014-05-28 09:57 7492并发编程系列文章: 初解线程池:http://r ... -
线程池详解
2014-05-26 18:20 0本文章需要对JDK5 Executor框架有所了 ... -
高级字节码生成工具 — Javassist
2014-03-11 09:57 6352AOP系列文章: Spring AOP:http ... -
实现AOP — CGLIB
2014-03-06 09:50 3920AOP系列文章: Sp ... -
cglib
2014-01-11 19:11 0什么是CGLIB? CGLIB是一个强大的高性能 ... -
SpringEL详解及应用
2013-09-22 07:07 10333什么是SpringEL? Spring3中引入了 ... -
Java高质量代码之 — 杂
2013-09-16 09:53 4686前言:由于上一个星期工作繁忙,利用上下班和晚上睡前空余的时间 ... -
Java高质量代码之 — 异常
2013-09-09 09:35 6644前言:由于上一个星期工作繁忙,利用上下班和晚上睡前空余的时间 ... -
Java高质量代码之 — 泛型与反射
2013-08-29 09:36 18012前言:由于上一个星期工作繁忙,利用上下班和晚上睡前空余的时间 ... -
Java高质量代码之 — 枚举与注解
2013-08-27 09:59 15716前言:由于上一个星期工作繁忙,利用上下班和晚上睡前空余的时间 ... -
Java高质量代码之 — 数组与集合(2)
2013-08-23 17:24 2844前言:由于上一个星期工作繁忙,利用上下班和晚上睡前空余的时间 ...
相关推荐
使用Spring的注解方式实现AOP的细节
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
Spring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOPSpring通过注解实现AOP,Spring通过注解实现AOP,Spring通过注解实现AOP
使用Spring配置文件实现AOP
Spring aop Spring aop
AOP的意思就是面向切面编程。本文主要是通过梳理JDK中自带的反射机制,实现 AOP动态代理模式,这也是Spring AOP 的实现原理
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-...
Spring通过XML实现AOP,Spring通过XML实现AOP,Spring通过XML实现AOP
springAop与spring定时器
Spring中实现AOP有很多方式,这里总结了四种,有基于代理的实现方式,也有基于AspectJ 的注解方式,还有基于AOP标签的配置
NULL 博文链接:https://zhang-yingjie-qq-com.iteye.com/blog/319927
里面包括4个例子:(1)Spring实现AOP方式之一:基于XML配置的Spring AOP (2)Spring实现AOP方式之二:使用注解配置 Spring AOP (3)Spring AOP : AspectJ Pointcut 切点 (4)Spring AOP : Advice 声明 (通知注解)
在Spring1.2或之前的版本中,实现AOP的传统方式就是通过实现Spring的AOP API来定义Advice,并设置代理对象。Spring根据Adivce加入到业务流程的时机的不同,提供了四种不同的Advice:Before Advice、After Advice、...
一个基于配置文件的Spring AOP的实现。实现了前置通知,后置通知,以及拦截器的功能,配置中有详细的注释。
Spring实现AOP以及AOP的定义Spring实现AOP以及AOP的定义
Spring mvc mybatis plus 实现AOP 切面日志系统,带有数据库。可以自行拓展
Spring实现AOP的4种方式
JavaEE spring自动实现AOP代理
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。