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:公钥 ...
随机推荐
- hdu 3336 Count the string KMP+DP优化
Count the string Problem Description It is well known that AekdyCoin is good at string problems as w ...
- boost linux 下安装
1. 在boost 官网 http://www.boost.org/doc/libs/ 下载最新的boost 安装包 2. 解压至 /usr/local/ 目录下 3. cd /usr/local/b ...
- 关于《一步步学习ASP.NET MVC3》系列发布时间的说明
在写这个系列的时候,老魏也是下了很大的决心,因为平时基本上没有时间写文章,这回我要挑战我自己的意志力,决定要把这个系列写完整. 再次呢,老魏不能向大家保证什么时间结束,但基本上要保持一天一篇的进度,如 ...
- dvd的舞女
[题目描述] 众所周知,dvd是一个爱做梦的好孩子. 但是不知道为什么最近dvd总是梦到一群舞女 众所周知,dvd是一个爱琢磨的好孩子. 但是不知道为什么dvd最近一直想不明白为什么 终于dvd发现了 ...
- Bootstrap 分页功能
function bootstrappage() { var options = { currentPage: currentPage, totalPages: totalPages, size: ' ...
- 解决WP8应用里ListBox绑定数据变多导致越来越卡
ListBox控件绑定数据,当滑动到底部的时候加载数据到列表上,这样就会产生一个问题,当ListBox上面绑定的数据有几千条的时候,界面将会卡顿,我们可以通过在ListBox上只绑定指定数量的数据,其 ...
- centos 下 yum 安装 nginx 平滑切换安装到 Tengine
---恢复内容开始--- 据说淘宝的Tengine很牛X,所以我们今天也来玩玩,我们这里是某开放云的vps,现在已经安装好了nginx,现在我们要平滑切换到安装Tengine. 下载Tengine,解 ...
- 批处理:遍历输出指定后缀格式的文件名.bat
批处理:遍历输出指定后缀格式的文件名.bat @echo off type nul >C:\result.txt for /r "d:\我的文档\桌面\交接\webservice\We ...
- BT5升级MSF至Git更新的方法
由于Kali在虚拟机的运行效率实在让人不敢恭维,于是决心将BT5中的MSF进行升级,升级的主要目的是,BT5R3内置的MSF是用SVN进行更新, 但是新版本的MSF已经停止通过SVN更新,改用Git, ...
- hdu 3483 A Very Simple Problem
两种构造的方式都是正确的: 1. #include<cstdio> #include<cstring> #include<algorithm> #define ma ...