关于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 ...
随机推荐
- App是什么,可以分为几类?及其相关解释。
App,是应用程序,Application的缩写,事实上,严格说来,目前市面上的APP大致可分为以下十类,即移动UGC,移动搜索,移动浏览,移动支付,移动广告,移动即时信息,SNS,LBS,AR以及 ...
- NOIP 2015 神奇的幻方
模拟,注意为偶数的情况 #include<cstdio> #include<cstring> #include<cstdlib> #include<iostr ...
- codeforce 600A - Extract Numbers
学习string #include <bits/stdc++.h> #define eps 1e-8 #define M_PI 3.141592653589793 ; using name ...
- PHP编码规范整理,很全很实用(图文版)
有一个组织叫做“php互操作性框架制定小组”,这个小组的主要目的是制定各种PHP编码规范的,下面就是我根据小组提供的建议整理的一些常用的编码规范. PSR-1: 1.PHP代码文件必须以<?ph ...
- Docker系列(七)Shipyard安装及介绍
Shipyard 是一个基于 Web 的 Docker 管理工具,支持多 host,可以把多个 Docker host 上的 containers 统一管理:可以查看 images,甚至 build ...
- HW7.9
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- 使用Mono Cecil 动态获取运行时数据 (Atribute形式 进行注入 用于写Log) [此文报考 xxx is declared in another module and needs to be imported的解决方法]-摘自网络
目录 一:普通写法 二:注入定义 三:Weave函数 四:参数构造 五:业务编写 六:注入调用 7. 怎么调用别的程序集的方法示例 8. [is declared in another module ...
- DELPHI 数学函数+字符处理函数
System单元 Trunc(1234.5678);{1234} Trunc(-1234.5678);{-1234} Round(1234.5678);{1235} Round(-1234.5678) ...
- MAX16054
MAX16054是带有单个开关去抖以及内部闭锁电路的按键通/断控制器,可接受机械开关产生的嘈杂输入,并经过一个有工厂设置的延迟时间后产生干净的数字锁存输出. 开关通.断期间,MAX16054无接触抖动 ...
- pip error: command 'gcc' failed with exit status 1
SWIG/_m2crypto_wrap.c:127:20: 致命错误:Python.h:没有那个文件或目录 #include <Python.h> ...