关于SpringAOP的XML方式的配置
AOP(XML)【理解】【应用】【重点】
1.AOP基础实例
A.导入jar包
核心包(4个) 日志(2个) AOP(4个)
Spring进行AOP开发(1个)(3.2资源包)
spring-aop-3.2.0.RELEASE.jar
Spring整合AspectJ框架(3.2资源包)
spring-aspects-3.2.0.RELEASE.jar
AOP联盟规范(1个) (3.0.2依赖包)
com.springsource.org.aopalliance-1.0.0.jar
aspectJ支持(1个) (3.0.2依赖包)
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
B.制作目标对象类(配置成Bean)
public class UserImpl {
//当前类的三个方法中具有共性功能System.out.println("共性功能1"); 抽取出来
public void add(){
//共性功能1被抽取走了,放入到了MyAdvice类中的functionOne方法里
System.out.println("共性功能2");
System.out.println("user add....");
}
}
注意:共性功能被抽取后,不是在原始位置进行调用,而是将共性功能删除
C.将抽取的功能制作成通知(配置成Bean)
public class MyAdvice {
//被抽取的共性功能1
public void functionOne(){
System.out.println("共性功能1");
}
}
说明:被抽取的共性功能制作成独立的类中方法。由于要将两个对象中的内容融合,Spring在控
制其融合的过程必须控制两个对象,因此需要分别将两个类配置为Spring管理的Bean
D.开启AOP空间的支持
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
">
E.配置AOP的切面
<!-- 配置AOP -->
<aop:config>
<!-- 配置切面,指定切面类的Bean:配置切入点与通知之间的关系 -->
<!-- ref:配置切面对应的Bean -->
<aop:aspect ref="myAdvice">
<!-- 声明通知的类别是何种类型 前置通知-->
<!-- 配置前置通知 -->
<!-- aop:before表示在原始方法执行前追加功能 -->
<!-- pointcut:配置Spring监控的方法切入点 -->
<!-- method:配置的切面类中对应的共性功能-方法名称 -->
<aop:before pointcut="execution(* cn.itcast.aop.one.UserImpl.add())" method="functionOne"/>
</aop:aspect>
</aop:config>
F.制作Spring格式的客户端程序测试
ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);
UserDao dao = (UserDao)ctx.getBean(“userDao”);
dao.add();
2.切入点表达式
格式:execution(切入点表达式)
execution([方法的访问控制修饰符] 方法的返回值 包名.类名/接口名.方法名(参数))
注意:方法的访问控制修饰符可以省略
范例:
public void cn.itcast.UserDAO.add() 公共的cn.itcat包中的UserDAO类的无参数无返回值的add方法
void cn.itcast.UserDAO.add() 公共的cn.itcat包中的UserDAO类的无参数无返回值的add方法
方法的返回值可以写具体的返回值,或者写*表示任意返回值
void cn.itcast.UserDAO.add() 公共的cn.itcat包中的UserDAO类的无参数无返回值的add方法
* cn.itcast.UserDAO.add() 公共的cn.itcat包中的UserDAO类的无参数不限定返回值的add方法
包名,方法名
* cn.itcast.*.dao.UserDAO.add() cn包下的itcast包下的任意包下的dao包下的…..
* cn.itcast..dao.UserDAO.add() cn包下的itcast包下的任意层包下的dao包下的…..
* *..*.*() 任意包下的任意类中的任意方法
参数
add() 无参数
add(*) 一个参数
add(int) 一个int型参数
add(*,*) 两个参数
add(*,int) 两个参数,第一个任意,第二个int
add(..) 任意参数
add(*,..) 至少一个参数
特殊格式:
* cn.itcast.UserDAO.add(..) && args(int,int) 错误
* cn.itcast.UserDAO.add(..) && args(int,int) 正确
* cn.itcast.UserDAO.add(int,int) && args(a,b) 不常用,正确
* cn.itcast.UserDAO.add(int,int) && args(b,a) 不常用,正确
3.切入点配置方式
切入点配置时,可以设置切面间共享切入点,也可以设置切面内共享切入点,还可以设置局部切入点
格式一:配置在通知类别后面
<aop:before pointcut="execution(public void *..*.*Impl.a*())" ….
格式二:配置在某个切面中,在当前切面范围内共享
<aop:aspect ref="myAdvice">
<!-- 配置同一个切面中使用的公共切入点 -->
<aop:pointcut expression="execution(public void *..*.*Impl.a*())" id="pt2"/>
<aop:before pointcut-ref="pt2" …..
格式三:配置在AOP总配置中,在全部范围内共享
<aop:config>
<aop:pointcut expression="execution(public void *..*.*Impl.a*())" id="pt1"/>
<aop:aspect ref="myAdvice">
<aop:before pointcut-ref="pt1"……
</aop:aspect>
</aop:config>
范例:
<aop:config>
<!-- 所有切面共享的切入点 -->
<aop:pointcut expression="execution(* cn.itcast..*Tar*.*())" id="pt"/>
<aop:aspect ref="myAdvice">
<!-- 在一个切面内共享的切入点:配置独立的切入点表达式对象 -->
<aop:pointcut expression="execution(* cn.itcast..*Tar*.*(..))" id="pt1"/>
<aop:before pointcut-ref="pt" method="fn"/>
<aop:before pointcut="execution(* *..*.*(..))" method="fn"/>
</aop:aspect>
<aop:aspect ref="myAdvice">
<!-- 配置独立的切入点表达式对象 -->
<aop:pointcut expression="execution(* cn.itcast..*Tar*.*(..))" id="pt2"/>
<aop:before pointcut-ref="pt" method="fn"/>
<aop:before pointcut-ref="pt" method="fn"/>
</aop:aspect>
</aop:config>
4.通知类别
通知共有五种类别
before:在原始操作前运行
after: 在原始操作后运行,无论方法是否抛出异常
afterReturning:在原始操作后运行,只有方法正常结束才运行,抛出异常则不运行
afterThrowing:在原始操作中如果抛出异常,运行
around: 在原始操作前后运行,通过ProceedingJoinPoint对象调用procee()方法完成对原始操作的调用
//环绕通知
public void around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("around before......");
//调用原始方法
pjp.proceed();
System.out.println("around after......");
}
通知的配置格式
<aop:before pointcut-ref="pt2" method="before"/>
<aop:after pointcut-ref="pt2" method="after"/>
<aop:after-returning pointcut-ref="pt2" method="afterReturning"/>
<aop:after-throwing pointcut-ref="pt2" method="afterThrowing"/>
<aop:around pointcut-ref="pt2" method="around"/>
5.通知顺序
在切入点之前运行的通知
执行允许与配置顺序有关,在上面的先运行
在切入点之后运行的通知
在同一个切面中
执行允许与配置顺序有关,在上面的先运行
在不同的切面中
执行允许与配置顺序有关,与切面的配置顺序相反
总结:不同通知类型执行的顺序以配置顺序为准
6.获取通知参数
为环绕通知之外的通知方法定义形参JoinPoint,该参数必须是通知方法的第一个参数
获取参数:Obejct[] args = jp.getArgs();
范例:
public void before(JoinPoint jp){
Object[] objs = jp.getArgs();
System.out.println("before......"+objs[0]+","+objs[1]);
}
为环绕通知方法定义形参ProceedingJoinPoint对象
获取参数:Obejct[] args = pjp.getArgs();
7.获取通知返回值
afterReturning 与 around可以获取方法的返回值
A.around通知获取返回值
ProceedingJoinPoint对象执行调用原始操作的返回值就是原始方法的运行返回值
Object res = pt.proceed(args);
注意:如果原始方法返回值为void类型,则around方法返回值设置为Object
如果原始方法返回值为非void类型,则around方法内必须将原始方法调用的结果返回
原始方法返回值为void类型的,通知内获取的返回值统一为null
public Object around(ProceedingJoinPoint pjp) throws Throwable{
Object res = pjp.proceed(args);
return res;
}
B.afterReturning通知获取返回值
在通知方法的参数中,声明一个Object类型的参数,用于保存方法的返回值
public void afterReturning(JoinPoint jp,Object abc){
System.out.println("afterReturning......"+ abc);
}
在配置文件中,为afterReturning声明保存返回值的变量名
<aop:after-returning method="afterReturning" returning="abc"/>
8.获取通知异常对象
异常对象的获取方式与返回值很相似,声明变量,在配置中声明保存异常对象的变量名
<aop:after-throwing pointcut-ref="pt" method="afterThrowing" throwing="e"/>
public void afterThrowing (Throwable e){
System.out.println("afterThrowing......."+ e);
}
关于SpringAOP的XML方式的配置的更多相关文章
- springmvc基础篇—通过注解的方式去配置项目
学习了通过xml方式去配置项目后,当然要掌握更简单更灵活的注解方式哟,这是官方推荐使用的方式. 一.修改配置文件,建议大家直接使用我的配置文件 <?xml version="1.0&q ...
- Spring中的AOP注解方式和XML方式
应掌握内容:1. AOP的全名2. AOP的实现原理[静态代理和动态代理]3. 注解方式的配置4. 通知类型 A. 每种通知的特点和使用方式 B. 获取各种数据,方便日后操作5. 执行表 ...
- Spring-注入方式(基于xml方式)
1.基于xml方式创建对象 <!--配置User类对象的创建 --> <bean id="user" class="com.at.spring5.Use ...
- 转-springAOP基于XML配置文件方式
springAOP基于XML配置文件方式 时间 2014-03-28 20:11:12 CSDN博客 原文 http://blog.csdn.net/yantingmei/article/deta ...
- SpringMVC(十六):如何使用编程方式替代/WEB-INF/web.xml中的配置信息
在构建springmvc+mybatis项目时,更常用的方式是采用web.xml来配置,而且一般情况下会在web.xml中使用ContextLoaderListener加载applicationCon ...
- springAop:Aop(Xml)配置,Aop注解配置,spring_Aop综合案例,Aop底层原理分析
知识点梳理 课堂讲义 0)回顾Spring体系结构 Spring的两个核心:IoC和AOP 1)AOP简介 1.1)OOP开发思路 OOP规定程序开发以类为模型,一切围绕对象进行,OOP中完成某个任务 ...
- spring aop注解方式与xml方式配置
注解方式 applicationContext.xml 加入下面配置 <!--Spring Aop 启用自动代理注解 --> <aop:aspectj-autoproxy proxy ...
- 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)
Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式 √ id:标识容器中的bean.id唯一. √ cl ...
- spring学习笔记 星球日one - xml方式配置bean
ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...
随机推荐
- JDBCTemplate.java
package com.pk.xjgs.util; import java.sql.Connection; import java.sql.SQLException; import java.util ...
- Python 同时for遍历多个列表
a = range(3) b = range(3) [ (x, y) for x, y in zip(a, b) ] 结果: [ (0,0), (1,1), (2,2) ] 当然,如上可以推广到多个列 ...
- 【转载】extern "C"的用法解析(原博主就是抄百度百科的,不如另外一篇好)
[说明]文章转载自Rollen Holt 的文章 http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html --------- ...
- PetShop学习第四天
ASP.NET缓存 1.页输出缓存分为整页缓存和部分页缓存.我们可以通过@OutputCache指令来完成对Web页面的输出缓存.
- Django中的Model(操作表)
Model 操作表 一.基本操作 # 增 models.Tb1.objects.create(c1='xx', c2='oo') #增加一条数据,可以接受字典类型数据 **kwargs obj = m ...
- SQL2008-不同数据库之间的触发器
create trigger tr_update_Table_1 on rwqd FOR UPDATE As update dataabc.dbo.Table_1 set ...
- 推荐《C Primer Plus(第五版)中文版》【worldsing笔记】
老外写的C书,看了你会有一种哇塞的感觉,这里提供PDF扫描版的下在,包含数内的例程,请大家支持原版!! C Primer Plus(第五版)中文版.pdf 下载地址:http://pan.bai ...
- 使用Redis bitmaps进行快速、简单、实时统计
原文:Fast, easy, realtime metrics using Redis bitmaps (http://blog.getspool.com/2011/11/29/fast-easy-r ...
- 我的JS 类 写法
长这样! var p,p1; //构造函数 function Person(name) { this.name = name; } //原型对象 var proto = { getName : fun ...
- 配置MySQL主从双向同步
原文地址:http://www.cnblogs.com/zhongshengzhen/ 原主数据库:192.168.137.33 原从数据库:192.168.137.197 需要先阅读并操作:ht ...