- 浏览: 984819 次
- 性别:
- 来自: 杭州
文章分类
- 全部博客 (826)
- 硬件 (8)
- 软件 (24)
- 软件工程 (34)
- JAVA (229)
- C/C++/C# (77)
- JavaScript (8)
- PHP (1)
- Ruby (3)
- MySQL (14)
- 数据库 (19)
- 心情记事 (12)
- 团队管理 (19)
- Hadoop (1)
- spring (22)
- mybatis(ibatis) (7)
- tomcat (16)
- velocity (0)
- 系统架构 (6)
- JMX (8)
- proxool (1)
- 开发工具 (16)
- python (10)
- JVM (27)
- servlet (5)
- JMS (26)
- ant (2)
- 设计模式 (5)
- 智力题 (2)
- 面试题收集 (1)
- 孙子兵法 (16)
- 测试 (1)
- 数据结构 (7)
- 算法 (22)
- Android (11)
- 汽车驾驶 (1)
- lucene (1)
- memcache (12)
- 技术架构 (7)
- OTP-Erlang (7)
- memcached (17)
- redis (20)
- 浏览器插件 (3)
- sqlite (3)
- Heritrix (9)
- Java线程 (1)
- scala (0)
- Mina (6)
- 汇编 (2)
- Netty (15)
- libevent (0)
- CentOS (12)
- mongod (5)
- mac os (0)
最新评论
-
kingasdfg:
你这里面存在一个错误添加多个任务 应该是这样的 /** * ...
Quartz的任务的临时启动和暂停和恢复【转】 -
kyzeng:
纠正一个错误,long型对应的符号是J,不是L。
Jni中C++和Java的参数传递 -
zhaohaolin:
抱歉,兄弟,只是留下作记录,方便学习,如果觉得资料不好,可以到 ...
netty的个人使用心得【转】 -
cccoooccooco:
谢谢!自己一直以为虚机得使用网线才可以与主机连接呢。。
主机网卡无网线连接与虚拟机通信 -
yuqilin001:
要转别人的东西,请转清楚点嘛,少了这么多类,误人子弟
netty的个人使用心得【转】
-
AOP是一种不同于OOP(面向对象编程)的编程模式,它不是OOP的替代,而是对OOP的一种有益补充。
AOP的理解
1、AOP的概述
AOP是一种不同于OOP(面向对象编程)的编程模式,它不是OOP的替代,而是对OOP的一种有益补充。
2、spring AOP的原理
3、spring AOP的实现
在spring2.5中,常用的AOP实现方式有两种。第一种是基于xml配置文件方式的实现,第二种是基于注解方式的实现。
接下来,以具体的是理智讲解这两种方式的使用。
Java代码
package com.zxf.service; /** * 业务逻辑接口 * @author z_xiaofei168 */ public interface AccountService { public void save(String loginname, String password); } 它的实现类 package com.zxf.service; import com.zxf.dao.AccountDao; /** * AccountService的实现类 * @author z_xiaofei168 */ public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public AccountServiceImpl() {} /** 带参数的构造方法 */ public AccountServiceImpl(AccountDao accountDao){ this .accountDao = accountDao; } public void save(String loginname, String password) { accountDao.save(loginname, password); throw new RuntimeException( "故意抛出一个异常。。。。" ); } /** set方法 */ public void setAccountDao(AccountDao accountDao) { this .accountDao = accountDao; } }
对于业务系统来说,AccountServiceImpl类就是目标实现类,它的业务方法,如save()方法的前后或代码会出现异常的地方都是AOP的连接点。
下面是日志服务类的代码:
Java代码
package com.zxf.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; /** * 日志切面类 * @author z_xiaofei168 */ public class LogAspect { //任何通知方法都可以将第一个参数定义为 org.aspectj.lang.JoinPoint类型 public void before(JoinPoint call) { //获取目标对象对应的类名 String className = call.getTarget().getClass().getName(); //获取目标对象上正在执行的方法名 String methodName = call.getSignature().getName(); System.out.println( "前置通知:" + className + "类的" + methodName + "方法开始了" ); } public void afterReturn() { System.out.println( "后置通知:方法正常结束了" ); } public void after(){ System.out.println( "最终通知:不管方法有没有正常执行完成,一定会返回的" ); } public void afterThrowing() { System.out.println( "异常抛出后通知:方法执行时出异常了" ); } //用来做环绕通知的方法可以第一个参数定义为org.aspectj.lang.ProceedingJoinPoint类型 public Object doAround(ProceedingJoinPoint call) throws Throwable { Object result = null ; this .before(call); //相当于前置通知 try { result = call.proceed(); this .afterReturn(); //相当于后置通知 } catch (Throwable e) { this .afterThrowing(); //相当于异常抛出后通知 throw e; } finally { this .after(); //相当于最终通知 } return result; } }
这个类属于业务服务类,如果用AOP的术语来说,它就是一个切面类,它定义了许多通知。Before()、afterReturn()、after()和afterThrowing()这些方法都是通知。
<1>.基于xml配置文件的AOP实现
这种方式在实现AOP时,有4个步骤。
Xml代码
<xml version = "1.0" encoding = "UTF-8" ?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd> <bean id = "accountDaoImpl" class = "com.zxf.dao.AccountDaoImpl" /> <bean id = "accountService" class = "com.zxf.service.AccountServiceImpl"> <property name = " accountDaoImpl " ref = " accountDaoImpl " /> </bean> <bean id = "logAspectBean" class = "com.zxf.aspect.LogAspect" /> <aop:config> <aop:aspect id = "logAspect" ref = "logAspectBean"> <aop:pointcut id = "allMethod" expression = "execution(* com.zxf.service.*.*(..))" /> <aop:before method = "before" pointcut-ref = "allMethod" /> <aop:after-returning method = "afterReturn" pointcut-ref = "allMethod" /> <aop:after method = "after" pointcut-ref = "allMethod" /> <aop:after-throwing method = "afterThrowing" pointcut-ref = "allMethod" /> </aop:aspect> </aop:config> </beans>
上述配置针对切入点应用了前置、后置、最终,以及抛出异常后通知。这样在测试执行AccountServiceImpl类的save()方法时,控制台会有如下结果输出。
前置通知:com.zxf.service.AccountServiceImpl类的save方法开始了。
针对MySQL的AccountDao实现中的save()方法。
后置通知:方法正常结束了。
最终通知:不管方法有没有正常执行完成,一定会返回的。
<2>基于注解的AOP的实现
首先创建一个用来作为切面的类LogAnnotationAspect,同时把这个类配置在spring的配置文件中。
在spring2.0以后引入了JDK5.0的注解Annotation的支持,提供了对AspectJ基于注解的切面的支持,从而 更进一步地简化AOP的配置。具体的步骤有两步。
Spring的配置文件是如下的配置:
Xml代码
<xml version = "1.0" encoding = "UTF-8" ?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd> <bean id = "accountDao" class = "com.zxf.dao.AccountDaoImpl" /> <bean id = "accountService" class = "com.zxf.service.AccountServiceImpl"> <property name = "accountDao" ref = "accountDao" /> </bean> <bean id = "logAspectBean" class = "com.zxf.aspect.LogAnnotationAspect" /> <aop:aspectj-autoproxy /> </beans>
这是那个切面的类LogAnnotationAspect
Java代码
package com.zxf.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; /** * 日志切面类 */ @Aspect //定义切面类 public class LogAnnotationAspect { @SuppressWarnings ( "unused" ) //定义切入点 @Pointcut ( "execution(* com.zxf.service.*.*(..))" ) private void allMethod(){} //针对指定的切入点表达式选择的切入点应用前置通知 @Before ( "execution(* com. zxf.service.*.*(..))" ) public void before(JoinPoint call) { String className = call.getTarget().getClass().getName(); String methodName = call.getSignature().getName(); System.out.println( "【注解-前置通知】:" + className + "类的" + methodName + "方法开始了" ); } //访问命名切入点来应用后置通知 @AfterReturning ( "allMethod()" ) public void afterReturn() { System.out.println( "【注解-后置通知】:方法正常结束了" ); } //应用最终通知 @After ( "allMethod()" ) public void after(){ System.out.println( "【注解-最终通知】:不管方法有没有正常执行完成," + "一定会返回的" ); } //应用异常抛出后通知 @AfterThrowing ( "allMethod()" ) public void afterThrowing() { System.out.println( "【注解-异常抛出后通知】:方法执行时出异常了" ); } //应用周围通知 //@Around("allMethod()") public Object doAround(ProceedingJoinPoint call) throws Throwable{ Object result = null ; this .before(call); //相当于前置通知 try { result = call.proceed(); this .afterReturn(); //相当于后置通知 } catch (Throwable e) { this .afterThrowing(); //相当于异常抛出后通知 throw e; } finally { this .after(); //相当于最终通知 } return result; } }
备注:输出结果和前面的一样。
【编辑推荐】
- Spring Hibernate简单讨论
- OSGi与Spring:设置Spring DM开发环境
- 使用Spring DM创建Hello World,以及OSGi服务
- Spring MVC总结:善用注解,生活更轻松
- 概括spring hibernate集成
发表评论
-
调试jdk中的源码,查看jdk局部变量
2013-06-15 23:30 1011调试jdk中的源码,查看jdk局部变量 2012-04 ... -
Eclipse快捷键 10个最有用的快捷键<转>
2013-04-11 23:28 1009Eclipse中10个最有用的快捷键组合 一个Eclip ... -
Lucene 3.6 中文分词、分页查询、高亮显示等
2012-12-09 23:35 17691、准备工作 下载lucene 3.6.1 : htt ... -
Maven实战(九)——打包的技巧(转)
2012-10-12 00:41 897“打包“这个词听起 ... -
基于Maven的web工程如何配置嵌入式Jetty Server开发调试环境(转)
2012-10-12 00:28 8681、首先在web工程的POM文件里添加依赖jar包如下: ... -
轻轻松松学Solr(1)--概述及安装[转]
2012-09-18 14:59 947概述 这段时间对企 ... -
分析Netty工作流程[转]
2012-09-04 19:02 845下面以Netty中Echo的例 ... -
让eclipse在ubuntu下面好看一点
2012-03-27 10:17 881<p> </p> <h1 cla ... -
zookeeper安装和应用场合(名字,配置,锁,队列,集群管理)[转]
2012-01-12 17:59 1613安装和配置详解 本文 ... -
Jakarta-Common-BeanUtils使用笔记[转]
2012-01-10 14:13 1119Jakarta-Common-BeanUtils ... -
一个关于Java Thread wait(),notify()的实用例【转】
2012-01-07 16:05 982///// // ProducerConsume ... -
Java基础:Java中的 assert 关键字解析【转】
2012-01-06 19:50 972J2SE 1.4在语言上提供了 ... -
一篇不错的讲解Java异常的文章(转载)----感觉很不错,读了以后很有启发[转]
2012-01-06 15:02 1215六种异常处理的陋习 ... -
如何解决HP QC(Quality Center)在Windows 7下不能工作的问题
2011-12-26 10:48 1537HP QC(Quantity Center) 是一款不错的测 ... -
JAVA读写文件,中文乱码 【转】
2011-12-19 23:43 2071最近在做HTML静态生成,需要从硬盘上把模版文件的内容读出来。 ... -
Java 6 JVM参数选项大全(中文版)【转】
2011-12-19 19:51 925Java 6 JVM参数选项大全(中文版) 作者 ... -
使用assembly plugin实现自定义打包【转】
2011-12-13 01:58 923在上一篇文章中,讨论到在对maven的机制不熟悉的情况下,为了 ... -
使用maven ant task实现非标准打包[转]
2011-12-13 01:56 1003maven很强大,但是总有些事情干起来不是得心应手,没有使用a ... -
Java日期转换SimpleDateFormat格式大全【转】
2011-12-08 20:22 126324小时制时间 显示: public clas ... -
使用Spring的表单标签库
2011-11-22 20:08 103013.9. 使用Spring的 ...
相关推荐
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
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-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...
赠送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;...
赠送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;...
赠送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...
aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar
spring-aop-3.2.5.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...
赠送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....
赠送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;...
spring-aop-4.0.4.RELEASE 的jar包,亲测可用。。。。
spring-**core**-4.3.6.RELEASE.jar :包含spring框架基本的核心工具类,spring其他组件都要用到这个包里的类,其他组件的基本核心 spring-**beans**-4.3.6.RELEASE.jar:所有应用都要用到的jar包,它包含访问配置...
spring-aop-5.0.4.RELEASE.jar。
spring-aop-2.0.8.jar
spring-aop-3.2.0.RELEASE.jar,一个Spring中AOP的jar包
赠送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;...
赠送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-...
赠送jar包:spring-aop-5.3.15.jar; 赠送原API文档:spring-aop-5.3.15-javadoc.jar; 赠送源代码:spring-aop-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.15.pom; 包含翻译后的API文档:spring...