Spring笔记(四)SpingAOP
需要的Jar包(String3.2)
com.springsource.net.sf.cglib-2.2.0.jar // 作用于cglib方式的动态代理
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-3.2.0.RELEASE.jar
五种增强模式
1、前置增强【before】,在业务代码之前执行增强的服务代码
示例代码如下:
AopUtil.java
| 1 | // 服务类定义 |
| 2 | public class AopUtil { |
| 3 | // 增强函数 |
| 4 | public void showLog(){ |
| 5 | System.out.println("调用服务类功能成功!"); |
| 6 | } |
| 7 | } |
Bean.java
| 1 | // 业务类实现 |
| 2 | public class Bean { |
| 3 | // 具体业务功能实现 |
| 4 | public void callBean(){ |
| 5 | System.out.println("调用Bean业务类功能成功!"); |
| 6 | } |
| 7 | } |
配置文件 spring.xml
| 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <beans |
| 3 | xmlns="http://www.springframework.org/schema/beans" |
| 4 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 5 | xmlns:aop="http://www.springframework.org/schema/aop" |
| 6 | xsi:schemaLocation=" |
| 7 | http://www.springframework.org/schema/beans |
| 8 | http://www.springframework.org/schema/beans/spring-beans-2.5.xsd |
| 9 | |
| 10 | http://www.springframework.org/schema/aop |
| 11 | http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> |
| 12 | |
| 13 | <bean id="beanID" class="aop.xml.Bean" ></bean> |
| 14 | <bean id="utilID" class="aop.xml.AopUtil"></bean> |
| 15 | |
| 16 | <aop:config proxy-target-class="true"> |
| 17 | <aop:pointcut expression="execution(public * *.*(..))" id="pcID"/> |
| 18 | |
| 19 | <aop:aspect ref="utilID" > |
| 20 | <aop:before method="showLog" pointcut-ref="pcID" /> |
| 21 | </aop:aspect> |
| 22 | </aop:config> |
| 23 | |
| 24 | </beans> |
TestSpring.java
| 1 | // 程序入口 |
| 2 | public class TestSpring { |
| 3 | public static void main(String[] args) { |
| 4 | ApplicationContext ac = new ClassPathXmlApplicationContext( |
| 5 | new String[] { "aop/xml/spring.xml" }); |
| 6 | Bean bean = (Bean) ac.getBean("beanID"); |
| 7 | bean.callBean(); |
| 8 | } |
| 9 | } |
2、后置增强【after】,在业务代码之后执行执行增强的服务代码,与前置增强区别仅在于XML文件中对aop的配置
| 1 | <aop:config proxy-target-class="true"> |
| 2 | <aop:pointcut expression="execution(public * *.*(..))" id="pcID"/> |
| 3 | |
| 4 | <aop:aspect ref="utilID" > |
| 5 | <aop:after method="showLog" pointcut-ref="pcID" /> |
| 6 | </aop:aspect> |
| 7 | </aop:config> |
3、正常返回后增强【after-returning】,业务代码正确执行后才会执行服务代码,也就是说若业务代码如果有异常,服务代码就不会执行
| 1 | <aop:config proxy-target-class="true"> |
| 2 | <aop:pointcut expression="execution(public * *.*(..))" id="pcID"/> |
| 3 | |
| 4 | <aop:aspect ref="utilID" > |
| 5 | <aop:after-returning method="showLog" pointcut-ref="pcID" /> |
| 6 | </aop:aspect> |
| 7 | </aop:config> |
4、异常后增强【after-throwing】,与after-returning的执行过程相反
| 1 | <aop:config proxy-target-class="true"> |
| 2 | <aop:pointcut expression="execution(public * *.*(..))" id="pcID"/> |
| 3 | |
| 4 | <aop:aspect ref="utilID" > |
| 5 | <aop:after-throwing method="showLog" pointcut-ref="pcID" /> |
| 6 | </aop:aspect> |
| 7 | </aop:config> |
5、方法环绕增强【around】
| 1 | <aop:config proxy-target-class="true"> |
| 2 | <aop:pointcut expression="execution(public * *.callBean(..))" id="pcID"/> |
| 3 | |
| 4 | <aop:aspect ref="utilID" > |
| 5 | <aop:around method="writeLog" pointcut-ref="pcID" /> |
| 6 | </aop:aspect> |
| 7 | </aop:config> |
| 1 | // 服务类定义 |
| 2 | public class AopUtil { |
| 3 | |
| 4 | public void writeLog(ProceedingJoinPoint point) throws Throwable{ |
| 5 | System.out.println("调用服务类功能【writeLog】成功!"); |
| 6 | point.proceed(); |
| 7 | } |
| 8 | } |
方法环绕增强与前面四种增强的主要区别在于,业务代码调用的控制在服务类的增强函数中 point.proceed()
XML方式配置stringAop总结:
确定动态代理方式JDK还是CGILIB有proxy-target-class属性字段的值确定,false表示采用JDK自带的动态代理,true表示采用 CGLIB动态代理;确定切入点即业务类中实现的业务方法,由属性值aop:pointcut expression确定,id=”pcID”建立业务方法的ID值execution属性值格式:
execution(修饰符 返回值 包名.类名.方法名(参数) 异常),其中修饰符、包名、类名、异常可以省略也支持通配符*,返回类型、方法名、参数不能省略但支持通配符*;
业务方法与服务方法建立关系由aop:aspect配置确定,ref=”utilID”指定服务类对象,
<aop:增强方式 method=增强方法 pointcut-ref=业务方法ID值 />
注解方式实现stringAop示范例代码:
spring.xml
| 1 | <?xml version="1.0" encoding="UTF-8"?> |
| 2 | <beans |
| 3 | xmlns="http://www.springframework.org/schema/beans" |
| 4 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 5 | xmlns:aop="http://www.springframework.org/schema/aop" |
| 6 | xmlns:context="http://www.springframework.org/schema/context" |
| 7 | |
| 8 | xsi:schemaLocation=" |
| 9 | http://www.springframework.org/schema/beans |
| 10 | http://www.springframework.org/schema/beans/spring-beans-2.5.xsd |
| 11 | |
| 12 | http://www.springframework.org/schema/aop |
| 13 | http://www.springframework.org/schema/aop/spring-aop-3.0.xsd |
| 14 | |
| 15 | http://www.springframework.org/schema/context |
| 16 | http://www.springframework.org/schema/context/spring-context-3.0.xsd" > |
| 17 | |
| 18 | <!-- 打开注解开关, 开关打开后才能解析@符号表示的注解 --> |
| 19 | <context:annotation-config /> |
| 20 | <!-- 扫描指定包下的注解,也就是指定需解析注解的路径 --> |
| 21 | <context:component-scan base-package="annotation"></context:component-scan> |
| 22 | <!-- 告诉springAop,产生业务类的动态代理对象,并切入到对应服务类方法中 --> |
| 23 | <aop:aspectj-autoproxy/> |
| 24 | </beans> |
Bean.java
| 1 | // 业务类实现 |
| 2 | //@Component表示业务Bean实例化 |
| 3 | @Component(value="beanID") |
| 4 | public class Bean { |
| 5 | // 具体业务功能实现,通过@Pointcut注解注册切入点 |
| 6 | @Pointcut (value="execution(public * *.callBean(..))") |
| 7 | public void callBean(){ |
| 8 | System.out.println("调用Bean业务类功能成功!"); |
| 9 | } |
| 10 | } |
AopUtil.java
| 1 | // 服务类定义 |
| 2 | //@Aspect表示AopUtil为切面,@Component(value="aopUtilID")对AopUtil类进行实例化 |
| 3 | @Aspect |
| 4 | @Component(value="aopUtilID") |
| 5 | public class AopUtil { |
| 6 | // @Before确定为增强模式为前置增强,value的值表示增强的业务函数 |
| 7 | @Before (value="annotation.Bean.callBean()") |
| 8 | public void writeLog(){ |
| 9 | System.out.println("调用服务类功能【writeLog】成功!"); |
| 10 | } |
| 11 | } |
TestSpring.java // 程序入口
| 1 | public class TestSpring { |
| 2 | public static void main(String[] args) { |
| 3 | ApplicationContext ac = new ClassPathXmlApplicationContext( |
| 4 | new String[] { "annotation/spring.xml" }); |
| 5 | Bean bean = (Bean) ac.getBean("beanID"); |
| 6 | bean.callBean(); |
| 7 | } |
| 8 | } |
注解说明:
@Component(value=自定义ID名称)作用于需实例化的类,并将实例化后的类对象加入Bean工厂;
@Aspect 指定作用于springAop中的切面类;
@Before (value="业务方法名")指定springAop中增强模式及需要增强的业务方法,业务方法名需带包名;
@Pointcut (value="execution表达式)") 指定springAop切入点业务方法;
@Resource(name="自定义ID名称")在Bean工厂中查找对应的ID名称对象,并注入到修饰的方法中,例子如下:
| 1 | private Unit unit; |
| 2 | @Resource(name="unitID") |
| 3 | public void setUnit(Unit unit) { |
| 4 | this.unit = unit; |
| 5 | } |
Spring笔记(四)SpingAOP的更多相关文章
- Spring笔记四
Spring-04 1.Spring整合Junit ①导入依赖 <!-- junit --> <dependency> <groupId>junit</gro ...
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...
- Spring笔记(6) - Spring的BeanFactoryPostProcessor探究
一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...
- C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻
前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...
- 《MFC游戏开发》笔记四 键盘响应和鼠标响应:让人物动起来
本系列文章由七十一雾央编写,转载请注明出处. http://blog.csdn.net/u011371356/article/details/9327377 作者:七十一雾央 新浪微博:http:// ...
- IOS学习笔记(四)之UITextField和UITextView控件学习
IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...
- java之jvm学习笔记四(安全管理器)
java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...
- Java学习笔记四---打包成双击可运行的jar文件
写笔记四前的脑回路是这样的: 前面的学习笔记二,提到3个环境变量,其中java_home好理解,就是jdk安装路径:classpath指向类文件的搜索路径:path指向可执行程序的搜索路径.这里的类文 ...
- Java加密与解密笔记(四) 高级应用
术语列表: CA:证书颁发认证机构(Certificate Authority) PEM:隐私增强邮件(Privacy Enhanced Mail),是OpenSSL使用的一种密钥文件. PKI:公钥 ...
随机推荐
- OpenCASCADE 基础
OpenCASCADE 基础 转载▼ 一直在用OCC作项目,但这方面的中文资料很少,看来OCC在中国还不是十分普及: 后来,项目中使用OCC和DirectX结合使用,取得了很好的效果: 随着OCC6. ...
- pycharm3.x 注册码
PyCharm 3.0 注册码 PyCharm3 序列号 License Key 用户名:yueting3527 注册码: ===== LICENSE BEGIN ===== 93347-120420 ...
- STL的pair学习, map学习
http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...
- C#微信登录-电脑版扫描二维码登录
像京东,一号店等网站都实现了用微信来登录的功能,就是用手机上的微信扫一扫网站上的二维码,微信上确认后,即可自动用微信的帐号登录网站. 一.创建网站应用 在微信开放平台创建一个网站应用 https:// ...
- 初识Tower Defense Toolkit
Tower Defense Toolkit 做塔防游戏的插件 主要层次如下图: 1GameControl _ _Game Control(Script) _ _ _Spawn Manager _ _ ...
- Python属性、方法和类管理系列之----属性初探
在学习dict的时候,肯定听过dict是Python中最重要的数据类型,但是不一定知道为什么.马上你就会明白原因了. Python中从模块.到函数.到类.到元类,其实主要管理方法就是靠一个一个的字典. ...
- 开发错误日志之FTP协议传输文件问题
从开发端用FTP协议向服务器(Linux系统)传输文件时,cat -A查询文件内容中行尾会有^M出现. 解决方案:改用SFTP协议上传文件.
- OpenStack项目列表
这个也是必须要熟悉的哟. ~~~~~~~~~~ OpenStack是一个美国国家航空航天局和Rackspace合作研发的,以Apache许可证授权,并且是一个自由软件和开放源代码项目.OpenStac ...
- php 数组指针相关函数current(),next(),prev(),end()
mixed current(array target_array) current()函数返回位于target_array数组当前指针位置的数组值.与next().prev().和end()函数不同, ...
- Codeforces Round #238 (Div. 1)
感觉这场题目有种似曾相识感觉,C题还没看,日后补上.一定要坚持做下去. A Unusual Product 题意: 给定一个n*n的01矩阵,3种操作, 1 i 将第i行翻转 2 i 将第i列翻转 3 ...