spring AOP应用
转自:http://wb284551926.iteye.com/blog/1887650
最近新项目要启动,在搭建项目基础架构的时候,想要加入日志功能和执行性能监控的功能,想了很多的想法,最后还是想到用spring的aop功能,一是不需要写那么多的重复代码,二是灵活好用,好了现在就讲讲这个aop功能的使用。(需要的jar包:spring.jar、asm-2.2.3.jar、asm-commons-2.2.3.jar、aopalliance.jar、aspectjweaver.jar、cglib-nodep-2.1_3.jar,可以忽略版本号)
1.建立一个切入类,包括doBefore、doAround、doAfter、doThrowing等方法。
- /*****************************************
- * @类名:com.util.TestAspect
- * @作者:王博
- * @时间:Jun 15, 2013
- * @版本:1.0
- * @描述:
- * @修改人员:
- * @修改时间:
- * @修改说明:
- *****************************************/
- package com.util;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- public class TestAspect {
- public void doAfter(JoinPoint jp) {
- System.out.println("log Ending method: "
- + jp.getTarget().getClass().getName() + "."
- + jp.getSignature().getName());
- }
- public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
- long time = System.currentTimeMillis();
- Object retVal = pjp.proceed();
- time = System.currentTimeMillis() - time;
- System.out.println("process time: " + time + " ms");
- return retVal;
- }
- public void doBefore(JoinPoint jp) {
- System.out.println("log Begining method: "
- + jp.getTarget().getClass().getName() + "."
- + jp.getSignature().getName());
- }
- public void doThrowing(JoinPoint jp, Throwable ex) {
- System.out.println("method " + jp.getTarget().getClass().getName()
- + "." + jp.getSignature().getName() + " throw exception");
- System.out.println(ex.getMessage());
- }
- }
2.在spring的配置文件中加入aop配置,proxy-target-class="true"红色部分*注意*,不加入这个,action是拦截不到的,切记
com.neau.project.*.*.*.*(..))" 这里主要注意的是
例如你的包结构是com.xxx.action.actionclass 你需要这样写才能拦截到 com.xxx.action.actionclass.*
最后的.*是代表actionclass中的方法。下面有这个配置的具体说明
如果要放行一些方法,比如get/set方法可以用 and 关键字,例如
execution(* com.neau.project.*.*.*.*(..)) and !execution(* com.neau.project.*.*.*.get*(..)) and !execution(* com.neau.project.*.*.*.set*(..))
- <aop:config <span style="color: #ff0000;">proxy-target-class="true"> </span>
- <aop:aspect id="TestAspect" ref="aspectBean">
- <!--配置com.neau.project.*.*.*.*包下所有类或接口的所有方法-->
- <aop:pointcut id="AOPService"
- expression="execution(* <span style="color: #ff0000;">com.neau.project.*.*.*.*</span>(..))" /> <aop:before pointcut-ref="AOPService" method="doBefore"/>
- <aop:after pointcut-ref="AOPService" method="doAfter"/>
- <aop:around pointcut-ref="AOPService" method="doAround"/>
- <aop:after-throwing pointcut-ref="AOPService" method="doThrowing" throwing="ex"/>
- </aop:aspect>
- </aop:config>
- <bean id="aspectBean" class="com.util.TestAspect" />
3.启动项目,执行
访问action
- log Begining method: com.neau.project.logon.action.LogonAction.test
- 2013-06-16 15:31:56,737 - {pstm-100001} Executing Statement: select count(*) from rdtteam t
- 2013-06-16 15:31:56,737 - {pstm-100001} Parameters: []
- 2013-06-16 15:31:56,737 - {pstm-100001} Types: []
- 2013-06-16 15:31:56,749 - {pstm-100004} Executing Statement: select t.* from rdtteam t limit ?,?
- 2013-06-16 15:31:56,749 - {pstm-100004} Parameters: [0, 20]
- 2013-06-16 15:31:56,749 - {pstm-100004} Types: [java.lang.Integer, java.lang.Integer]
- log Ending method: com.neau.project.logon.action.LogonAction.test
- process time: 261 ms
- log Begining method: com.neau.project.logon.action.LogonAction.getRdtTeamPage
- log Ending method: com.neau.project.logon.action.LogonAction.getRdtTeamPage
- process time: 0 ms
- log Begining method: com.neau.project.logon.action.LogonAction.get_rdtTeamList
- log Ending method: com.neau.project.logon.action.LogonAction.get_rdtTeamList
- process time: 0 ms
aop就这样实现了拦截action,简单吧,简单的工作实现大功能是我等苦逼开发者的目标。
偷点别的写好的吧。
《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:
- 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
- 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
- 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
- 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
- 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
- AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将
<aop:config>的proxy-target-class属性设为true
通知(Advice)类型
- 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
- 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
- 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
- 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
- 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。
切入点表达式
- 通常情况下,表达式中使用”execution“就可以满足大部分的要求。表达式格式如下:
- execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
modifiers-pattern:方法的操作权限
ret-type-pattern:返回值
declaring-type-pattern:方法所在的包
name-pattern:方法名
parm-pattern:参数名
throws-pattern:异常
其中,除ret-type-pattern和name-pattern之外,其他都是可选的。上例中,execution(* com.spring.service.*.*(..))表示com.spring.service包下,返回值为任意类型;方法名任意;参数不作限制的所有方法。
- 通知参数
可以通过args来绑定参数,这样就可以在通知(Advice)中访问具体参数了。例如,<aop:aspect>配置如下
- <aop:config>
- <aop:aspect id="TestAspect" ref="aspectBean">
- <aop:pointcut id="businessService"
- expression="execution(* com.spring.service.*.*(String,..)) and args(msg,..)" />
- <aop:after pointcut-ref="businessService" method="doAfter"/>
- </aop:aspect>
- </aop:config>
TestAspect的doAfter方法中就可以访问msg参数,但这样以来AService中的barA()和BServiceImpl中的barB()就不再是连接点,因为execution(* com.spring.service.*.*(String,..))只配置第一个参数为String类型的方法。其中,doAfter方法定义如下:
- public void doAfter(JoinPoint jp,String msg)
- 访问当前的连接点
任何通知(Advice)方法可以将第一个参数定义为 org.aspectj.lang.JoinPoint 类型。JoinPoint 接口提供了一系列有用的方法, 比如 getArgs() (返回方法参数)、getThis() (返回代理对象)、getTarget() (返回目标)、getSignature() (返回正在被通知的方法相关信息)和 toString()(打印出正在被通知的方法的有用信息。
warning no match for this type name: com.zrm.service [Xlint:invalidAbsoluteTypeName]
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="gkServicePct" expression="execution(* com.zrm.service.*(..))" />
<aop:advisor pointcut-ref="gkServicePct" advice-ref="gkTxAdvice" order="0" />
</aop:config>
出现warning no match for this type name: com.zrm.service [Xlint:invalidAbsoluteTypeName]错误
配置事务时,一定注意expression="execution(* com.zrm.service.*(..))" 应该为
expression="execution(* com.zrm.service.*.*(..))" ,这样,切点才定位到方法上了。
感谢:http://pandonix.iteye.com/blog/336873
感谢:http://hi.baidu.com/gglzf4/item/a58624e2f962d113585dd832
感谢:http://bbs.csdn.net/topics/350180595 11楼的同志
感谢:http://blog.sina.com.cn/s/blog_5f53615f0101ijdl.html
看了那么童鞋的经验让我少走了不少弯路,看来我就是吃百家饭长大孩纸的命,伤不起啊。苦逼的开发者伤不起啊。
spring AOP应用的更多相关文章
- 学习AOP之深入一点Spring Aop
上一篇<学习AOP之认识一下SpringAOP>中大体的了解了代理.动态代理及SpringAop的知识.因为写的篇幅长了点所以还是再写一篇吧.接下来开始深入一点Spring aop的一些实 ...
- 学习AOP之认识一下Spring AOP
心碎之事 要说知道AOP这个词倒是很久很久以前了,但是直到今天我也不敢说非常的理解它,其中的各种概念即抽象又太拗口. 在几次面试中都被问及AOP,但是真的没有答上来,或者都在面上,这给面试官的感觉就是 ...
- spring aop
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),它利用一种称为“横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将 ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- 基于Spring AOP的JDK动态代理和CGLIB代理
一.AOP的概念 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的 ...
- Spring AOP详解
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- Spring AOP实例——异常处理和记录程序执行时间
实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...
- 从零开始学 Java - Spring AOP 实现用户权限验证
每个项目都会有权限管理系统 无论你是一个简单的企业站,还是一个复杂到爆的平台级项目,都会涉及到用户登录.权限管理这些必不可少的业务逻辑.有人说,企业站需要什么权限管理阿?那行吧,你那可能叫静态页面,就 ...
- 从零开始学 Java - Spring AOP 实现主从读写分离
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
- 从零开始学 Java - Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
随机推荐
- iOS基础-NSString及NSMutableString剖析
一.NSString头文件 NSString : NSObject 实现协议: NSCopying/NSMutableCopying/NSSecureCoding 类别: //扩展类别 NSStrin ...
- (转)提高mysql千万级大数据SQL查询优化30条经验(Mysql索引优化注意)
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- svn各个图标代表什么意思
最近参与公司项目开发要使用SVN,下面随笔记下在使用SVN中常见的图标各代表什么意思 灰色向右箭头:本地修改过 ,本地代码没有及时上库.灰色向右且中间有个加号的箭头:本地比SVN上多出的文件灰色向右且 ...
- Tomcat 设置
bin/catalina.bat--增加内存 set JAVA_OPTS=...后面加上 set JAVA_OPTS=-Xmx1024M -Xms512M -XX:MaxPermSize=256m c ...
- JavaWeb---设置content-disposition响应头,让浏览器下载文件
package com.zyz; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServlet ...
- C语言调用curl库抓取网页图片(转)
思路是先用curl抓取网页源码,然后以关键字寻找出图片网址. 范例: #include <stdio.h> #include <stdlib.h> #include < ...
- selenium第一课(selenium+java+testNG+maven)
selenium介绍和环境搭建 一.简单介绍 1.selenium:Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包 ...
- PHP移动文件指针ftell()、fseek()、rewind()总结
在对文件进行读写过程中,有时需要在文件中跳转.同不同位置读取,以及将数据写入到不同的位置.例如,使用文件模拟数据库保存数据,就需要移动文件指针.指针的位置是以从文件头开始的字节数度量的,默认以不同模式 ...
- Cassandra-几个基本测试常识
一 使用ycsb进行装载,每次装载都不会删除以前装载过的. 因此如果想在空数据库中装载,需要先情况数据表. drop命令删除整个表,因此需要重建标头. truncate命令删除表的所有行,只留下表头, ...
- Linux下使用Eclipse开发Hadoop应用程序
在前面一篇文章中介绍了如果在完全分布式的环境下搭建Hadoop0.20.2,现在就再利用这个环境完成开发. 首先用hadoop这个用户登录linux系统(hadoop用户在前面一篇文章中创建的),然后 ...