Spring Aop实例之xml配置
AOP的配置方式有2种方式:xml配置和AspectJ注解方式。今天我们就来实践一下xml配置方式。
我采用的jdk代理,所以首先将接口和实现类代码附上
- package com.tgb.aop;
- public interface UserManager {
- public String findUserById(int userId);
- }
- package com.tgb.aop;
- public class UserManagerImpl implements UserManager {
- public String findUserById(int userId) {
- System.out.println("---------UserManagerImpl.findUserById()--------");
- if (userId <= 0) {
- throw new IllegalArgumentException("该用户不存在!");
- }
- return "张三";
- }
- }
单独写一个Advice通知类进行测试。这个通知类可以换成安全性检测、日志管理等等。
- package com.tgb.aop;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.ProceedingJoinPoint;
- /**
- * Advice通知类
- * 测试after,before,around,throwing,returning Advice.
- * @author Admin
- *
- */
- public class XMLAdvice {
- /**
- * 在核心业务执行前执行,不能阻止核心业务的调用。
- * @param joinPoint
- */
- private void doBefore(JoinPoint joinPoint) {
- System.out.println("-----doBefore().invoke-----");
- System.out.println(" 此处意在执行核心业务逻辑前,做一些安全性的判断等等");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of doBefore()------");
- }
- /**
- * 手动控制调用核心业务逻辑,以及调用前和调用后的处理,
- *
- * 注意:当核心业务抛异常后,立即退出,转向After Advice
- * 执行完毕After Advice,再转到Throwing Advice
- * @param pjp
- * @return
- * @throws Throwable
- */
- private Object doAround(ProceedingJoinPoint pjp) throws Throwable {
- System.out.println("-----doAround().invoke-----");
- System.out.println(" 此处可以做类似于Before Advice的事情");
- //调用核心逻辑
- Object retVal = pjp.proceed();
- System.out.println(" 此处可以做类似于After Advice的事情");
- System.out.println("-----End of doAround()------");
- return retVal;
- }
- /**
- * 核心业务逻辑退出后(包括正常执行结束和异常退出),执行此Advice
- * @param joinPoint
- */
- private void doAfter(JoinPoint joinPoint) {
- System.out.println("-----doAfter().invoke-----");
- System.out.println(" 此处意在执行核心业务逻辑之后,做一些日志记录操作等等");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of doAfter()------");
- }
- /**
- * 核心业务逻辑调用正常退出后,不管是否有返回值,正常退出后,均执行此Advice
- * @param joinPoint
- */
- private void doReturn(JoinPoint joinPoint) {
- System.out.println("-----doReturn().invoke-----");
- System.out.println(" 此处可以对返回值做进一步处理");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of doReturn()------");
- }
- /**
- * 核心业务逻辑调用异常退出后,执行此Advice,处理错误信息
- * @param joinPoint
- * @param ex
- */
- private void doThrowing(JoinPoint joinPoint,Throwable ex) {
- System.out.println("-----doThrowing().invoke-----");
- System.out.println(" 错误信息:"+ex.getMessage());
- System.out.println(" 此处意在执行核心业务逻辑出错时,捕获异常,并可做一些日志记录操作等等");
- System.out.println(" 可通过joinPoint来获取所需要的内容");
- System.out.println("-----End of doThrowing()------");
- }
- }
只有Advice还不行,还需要在application-config.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"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <bean id="userManager" class="com.tgb.aop.UserManagerImpl"/>
- <!--<bean id="aspcejHandler" class="com.tgb.aop.AspceJAdvice"/>-->
- <bean id="xmlHandler" class="com.tgb.aop.XMLAdvice" />
- <aop:config>
- <aop:aspect id="aspect" ref="xmlHandler">
- <aop:pointcut id="pointUserMgr" expression="execution(* com.tgb.aop.*.find*(..))"/>
- <aop:before method="doBefore" pointcut-ref="pointUserMgr"/>
- <aop:after method="doAfter" pointcut-ref="pointUserMgr"/>
- <aop:around method="doAround" pointcut-ref="pointUserMgr"/>
- <aop:after-returning method="doReturn" pointcut-ref="pointUserMgr"/>
- <aop:after-throwing method="doThrowing" throwing="ex" pointcut-ref="pointUserMgr"/>
- </aop:aspect>
- </aop:config>
- </beans>
编一个客户端类进行测试一下:
- package com.tgb.aop;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Client {
- public static void main(String[] args) {
- BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
- UserManager userManager = (UserManager)factory.getBean("userManager");
- //可以查找张三
- userManager.findUserById(1);
- System.out.println("=====我==是==分==割==线=====");
- try {
- // 查不到数据,会抛异常,异常会被AfterThrowingAdvice捕获
- userManager.findUserById(0);
- } catch (IllegalArgumentException e) {
- }
- }
- }
结果如图:
值得注意的是Around与Before和After的执行顺序。3者的执行顺序取决于在xml中的配置顺序。图中标记了3块,分别对应Before,Around,After。其中②中包含有③。这是因为aop:after配置到了aop:around的前面,如果2者调换一下位置,这三块就会分开独立显示。如果配置顺序是aop:after -> aop:around ->aop:before,那么①和③都会包含在②中。这种情况的产生是由于Around的特殊性,它可以做类似于Before和After的操作。当安全性的判断不通过时,可以阻止核心业务逻辑的调用,这是Before做不到的。
使用xml可以对aop进行集中配置。很方便而简单。可以对所有的aop进行配置,当然也可以分开到单独的xml中进行配置。当需求变动时,不用修改代码,只要重新配置aop,就可以完成修改操作。
Spring Aop实例之xml配置的更多相关文章
- spring aop自动代理xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置
用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...
- Spring装配Bean---使用xml配置
声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...
- Spring AOP 注解和xml实现 --转载
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. ...
- Spring的配置文件ApplicationContext.xml配置头文件解析
Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...
- 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP
上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...
- Spring学习十四----------Spring AOP实例
© 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...
- (一)spring aop的两种配置方式。
sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...
- Spring AOP实例——异常处理和记录程序执行时间
实例简介: 这个实例主要用于在一个系统的所有方法执行过程中出线异常时,把异常信息都记录下来,另外记录每个方法的执行时间. 用两个业务逻辑来说明上述功能,这两个业务逻辑首先使用Spring AOP的自动 ...
随机推荐
- 关于TreeSet倒序排列和自定义排列
本文部分转自:http://blog.csdn.net/kaituozhe345/article/details/6842945 1.TreeSet的自然排序. TreeSet存储对象的时候, 可以排 ...
- [改善Java代码] 推荐使用序列化实现对象的拷贝
建议44: 推荐使用序列化实现对象的拷贝 上一个建议说了对象的浅拷贝问题,实现Cloneable接口就具备了拷贝能力,那我们来思考这样一个问题:如果一个项目中有大量的对象是通过拷贝生成的,那我们该如何 ...
- 关于JDK中正则表达式
正则表达式的构造摘要 构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn ...
- poj 1848 树形dp
思路:表示我很弱,这个想不出dp方程,参考网上代码 #include<iostream> #include<algorithm> #include<cstring> ...
- Sublime text3 安装
Sublime是一款跨平台的前端开发神器,国外的一款共享软件,虽然是未注册的但不影响使用. 一.下载最新版的安装包 官网地址:http://www.sublimetext.com/3 --portab ...
- base.AutoScaleMode = AutoScaleMode.Font; 方法“InitializeComponent”内的代码由设计器生成,不应手动修改。请移除任何更改,然后尝试重新打开设计器”。
http://www.taohuiduo.com 反编译后的工程文件用VS2010打开后,在打开窗体时会出现一系列错误提示: 第一种情况: “设计器无法处理第 152 行的代码: base.AutoS ...
- rowid
rowid:select t.*,t.rowid from test t; -- AACeJKAAIAAAA4XAAA 注:有个很二需求,通过一个存储过程插入的数据必须是放在一块的,不能分散开存储,考 ...
- (转)理解SQL SERVER中的分区表
简介 分区表是在SQL SERVER2005之后的版本引入的特性.这个特性允许把逻辑上的一个表在物理上分为很多部分.而对于SQL SERVER2005之前版本,所谓的分区表仅仅是分布式视图,也就是多个 ...
- Linux下安装和配置java开发环境(jdk/eclipse/tomcat)
第一步 安装jdk 1.进入usr目录 cd /usr 2.在usr目录下建立java安装目录 mkdir java 3.将jdk-6u24-linux-i586.bin拷贝到java目录下 cp j ...
- 【墙裂推荐】大学生如何学习WEB开发
每天网络上有上万条Web招聘职位,招聘要求很简单: 会JavaScript,会CSS,能开发网页,能设计网页. 但我们真正面试时才发现:都是些很小很小的知识点! 我们没有实践过,没有碰到过,头脑一片茫 ...