1. 配置切面

因为切面Bean可以当成一个普通的SpringBean来配置,所以完全可以为该切面Bean配置依赖注入。当切面Bean定义完成后,通过<aop:aspect…/>元素中使用ref属性来引用该Bean,就可以将该Bean转换成一个切面Bean了。配置<aop:aspect…/>元素时可以指定如下三个属性。

1)        id:定义该切面的标识名

2)        ref:用于将ref属性所引用的普通Bean转换为切面Bean

3)        order:指定该切面Bean的优先级。

如下的配置片段定义了一个切面:

<aop:config><!-- 将fourAdviceBean转换成切面Bean切面Bean的新名称为:fourAdviceAspect指定该切面的优先级为2 --><aop:aspect id="fourAdviceAspect" ref="fourAdviceBean"order="2">……..</aop:aspect></aop:config><bean id=” fourAdviceAspect” class=”com.owen. fourAdviceAspect”/>

2. 配置增强处理

使用XML配置增强处理分别依赖于如下几个元素:

1)        <aop:before…/>:配置Before增强处理

2)        <aop:after…/>:配置After增强处理

3)        <aop:after-returning../>:配置AfterReturning增强

4)        <aop:after-throwing…/>:配置AfterThrowing增强

5)        <aop:around../>配置Around增强处理

上面这些元素都不支持使用子元素,但通常可指定如下的属性:

1)        pointcut:该属性指定一个切入元素表达式,Spring将在匹配表达式的连接点时织入该增强处理。

2)        method:该属性指定一个方法名,指定将切面Bean的该方法转换为增强处理。

3)        pointcut-ref:该属性指定一个已经存在的切点名称,通常pointcut和pointcut-ref两个属性只需使用其中之一。

4)        throwing:该属性只对<after-throwing../>元素有效,用于指定一个形参名,AfterThrowing增强处理方法通过该形参访问目标方法所抛出的异常。

5)        returning:该属性只对<after-returning…/>元素有效,用于指定一个形参名,AfterReturning增强处理方法可通过该形参访问方法的返回值。

3. 例子

1)        下面定义一个简单的切面类。

 public class FourAdviceTest
{public Object processTx(ProceedingJoinPoint jp)throws java.lang.Throwable{System.out.println("Around增强:执行目标方法之前,模拟开始事务...");// 访问执行目标方法的参数Object[] args = jp.getArgs();// 当执行目标方法的参数存在,// 且第一个参数是字符串参数if (args != null && args.length > 0&& args[0].getClass() == String.class){// 修改目标方法调用参数的第一个参数args[0] = "【增加的前缀】" + args[0];}//执行目标方法,并保存目标方法执行后的返回值Object rvt = jp.proceed(args);System.out.println("Around增强:执行目标方法之后,模拟结束事务...");// 如果rvt的类型是Integer,将rvt改为它的平方if(rvt != null && rvt instanceof Integer)rvt = (Integer)rvt * (Integer)rvt;return rvt;}public void authority(JoinPoint jp){System.out.println("②Before增强:模拟执行权限检查");// 返回被织入增强处理的目标方法System.out.println("②Before增强:被织入增强处理的目标方法为:"+ jp.getSignature().getName());// 访问执行目标方法的参数System.out.println("②Before增强:目标方法的参数为:"+ Arrays.toString(jp.getArgs()));// 访问被增强处理的目标对象System.out.println("②Before增强:被织入增强处理的目标对象为:"+ jp.getTarget());}public void log(JoinPoint jp , Object rvt){System.out.println("AfterReturning增强:获取目标方法返回值:"+ rvt);System.out.println("AfterReturning增强:模拟记录日志功能...");// 返回被织入增强处理的目标方法System.out.println("AfterReturning增强:被织入增强处理的目标方法为:"+ jp.getSignature().getName());// 访问执行目标方法的参数System.out.println("AfterReturning增强:目标方法的参数为:"+ Arrays.toString(jp.getArgs()));// 访问被增强处理的目标对象System.out.println("AfterReturning增强:被织入增强处理的目标对象为:"+ jp.getTarget());}public void release(JoinPoint jp){System.out.println("After增强:模拟方法结束后的释放资源...");// 返回被织入增强处理的目标方法System.out.println("After增强:被织入增强处理的目标方法为:"+ jp.getSignature().getName());// 访问执行目标方法的参数System.out.println("After增强:目标方法的参数为:"+ Arrays.toString(jp.getArgs()));// 访问被增强处理的目标对象System.out.println("After增强:被织入增强处理的目标对象为:"+ jp.getTarget());}
}

2)        除了上面的,再定义一个简单的切面类。

public class SecondAdviceTest
{// 定义Before增强处理public void authority(String aa){System.out.println("目标方法的参数为:" + aa);System.out.println("①号Before增强:模拟执行权限检查");}
}

3)        在Spring配置文件中配置。

 <?xml version="1.0" encoding="GBK"?>
<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-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><aop:config><!-- 将fourAdviceBean转换成切面Bean切面Bean的新名称为:fourAdviceAspect指定该切面的优先级为2 --><aop:aspect id="fourAdviceAspect" ref="fourAdviceBean"order="2"><!-- 定义一个After增强处理,直接指定切入点表达式以切面Bean中的release()方法作为增强处理方法 --><aop:after pointcut="execution(* com.owenapp.service.impl.*.*(..))" method="release"/><!-- 定义一个Before增强处理,直接指定切入点表达式以切面Bean中的authority()方法作为增强处理方法 --><aop:before pointcut="execution(* com.owenapp.service.impl.*.*(..))" method="authority"/><!-- 定义一个AfterReturning增强处理,直接指定切入点表达式以切面Bean中的log()方法作为增强处理方法 --><aop:after-returning pointcut="execution(* com.owenapp.service.impl.*.*(..))" method="log" returning="rvt"/><!-- 定义一个Around增强处理,直接指定切入点表达式以切面Bean中的processTx()方法作为增强处理方法 --><aop:around pointcut="execution(* com.owenapp.service.impl.*.*(..))" method="processTx"/></aop:aspect><!-- 将secondAdviceBean转换成切面Bean切面Bean的新名称为:secondAdviceAspect指定该切面的优先级为1,该切面里的增强处理将被优先织入 --><aop:aspect id="secondAdviceAspect" ref="secondAdviceBean"order="1"><!-- 定义一个Before增强处理,直接指定切入点表达式以切面Bean中的authority()方法作为增强处理方法 且该参数必须为String类型(由authority方法声明中msg参数的类型决定) --><aop:before pointcut="execution(* com.owenapp.service.impl.*.*(..)) and args(aa)" method="authority"/></aop:aspect></aop:config><!-- 定义一个普通Bean实例,该Bean实例将被作为Aspect Bean --><bean id="fourAdviceBean"class="com.owenapp.aspect.FourAdviceTest"/><!-- 再定义一个普通Bean实例,该Bean实例将被作为Aspect Bean --><bean id="secondAdviceBean"class="com.owenapp.aspect.SecondAdviceTest"/><bean id="hello" class="com.owenapp.service.impl.HelloImpl"/><bean id="world" class="com.owenapp.service.impl.WorldImpl"/>
</beans>

完成上面的定义后,运行上面的示例程序,将看到使用XML配置文件来管理切面、增强处理的效果。

4. 配置切入点

配置切入点,我们需要配置<aop:pointcut…/>元素,这里需要指定下面的属性:

1)        id:指定该切点的标识名。

2)        exception:指定该切点关联的切入点表达式。

下面笔者定义一个AfterThrowing增强处理,包含该增强处理的切面类如下:

public class RepairAspect
{// 定义一个普通方法作为Advice方法// 形参ex用于访问目标方法中抛出的异常public void doRecoveryActions(Throwable ex){System.out.println("目标方法中抛出的异常:" + ex);System.out.println("模拟Advice对异常的修复...");}
}

下面的配置文件将负责配置该Bean实例,并将该Bean实例转换成切面Bean。

<?xml version="1.0" encoding="GBK"?>
<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-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><aop:config><!-- 定义一个切入点:myPointcut通过expression指定它对应的切入点表达式 --><aop:pointcut id="myPointcut" expression="execution(* com.owen.app.service.impl.*.*(..))"/><aop:aspect id="afterThrowingAdviceAspect"ref="afterThrowingAdviceBean"><!-- 定义一个AfterThrowing增强处理,指定切入点以切面Bean中的doRecoveryActions()方法作为增强处理方法 --><aop:after-throwing pointcut-ref="myPointcut" method="doRecoveryActions" throwing="ex"/></aop:aspect></aop:config><!-- 定义一个普通Bean实例,该Bean实例将被作为Aspect Bean --><bean id="afterThrowingAdviceBean"class="com.owen.app.aspect.RepairAspect"/><bean id="hello" class="com.owen.app.service.impl.HelloImpl"/><bean id="world" class="com.owen.app.service.impl.WorldImpl"/>
</beans>

上面配置了一个myPontcut,这校准其他切面Bean就可多次复用该切点了。上面的配置文件在配置<aop:pointcut../>元素时,使用point-ref引用了一个已有的切入点。

Spring AOP之XML配置相关推荐

  1. Struts2+spring+jdbc 以xml配置形式整合

    今天做作业,练习一下Struts2+spring+jdbc 以xml配置形式整合 整合步骤: 工程结构图: 重要配置文件 web.xml <?xml version="1.0" ...

  2. Spring JDBC-使用XML配置声明式事务

    系列 概述 基于aop/tx命名空间的配置 示例 tx:method元素属性 系列 Spring对事务管理的支持概述以及 编程式的事务管理 Spring JDBC-使用XML配置声明式事务 Sprin ...

  3. springboot项目中的注解 启动项目的方式 解决spring的bean.xml配置不生效 spring的基础JDBC配置

    依赖 创建一个 Spring Boot 工程时,可以继承自一个 spring-boot-starter-parent ,也可以不继承 先来看 parent 的基本功能有哪些? 定义了 Java 编译版 ...

  4. Java框架篇---spring aop两种配置方式

    Java框架篇---spring aop两种配置方式 第一种:注解配置AOP 注解配置AOP(使用 AspectJ 类库实现的),大致分为三步:  1. 使用注解@Aspect来定义一个切面,在切面中 ...

  5. Spring框架中XML配置特殊属性注入

    Spring框架中XML配置特殊属性注入 前言 创建测试类 其他类型属性 前言 Spring框架中,在通过set方式进行属性注入时,可能会遇到某些特殊字符的注入,例如:null和某些标签字符" ...

  6. spring事务管理-xml配置aop事务(重点)

    刚才咱们是使用了模板操作咱们事务,当然使用模板操作比较low,还得写代码,每个方法都写太费劲了,首先把之前写的先注释掉,把这个transfer这个代码直接复制一份,然后底下展一份,留着注释掉就行了,上 ...

  7. @bean注解和@component注解的区别_阿里面试题一:spring里面使用xml配置和注解配置区别...

    阿里是众多程序员梦寐以求的地方,今天我们分享阿里的一个真实面试题,看似简单,回答起来确实没有头绪?下面我们分几点给大家说下答案. 适用场景 Xml配置场景: 1.Bean实现类来源于第三方类库,如Da ...

  8. Spring AOP(三)--XML方式实现

    本文介绍通过XML方式实现Spring AOP,在上一篇中已经介绍了通过注解+java配置的方式,这篇文章主要是看XML中怎么配置,直接上代码了: 一.创建一个连接点 1⃣️定义接口 注意⚠️:可以定 ...

  9. spring AOP实现——xml方法

    上一文中 讲了Annotation如何配置AOP,地址如下:http://5148737.blog.51cto.com/5138737/1428048 使用同样的bean,用xml来实现一下: Hel ...

  10. Spring MVC 无XML配置入门示例

    Spring MVC 无XML(纯 Java)配置入门示例 本示例是从<Spring in Action, Fourth Edition>一书而来,涉及的是书中5.1节部分内容,书中其实说 ...

最新文章

  1. Android TextView 常用技巧
  2. Java三维数组的使用
  3. spring boot 配置文件加密数据库用户名/密码
  4. 从框架源码中学习创建型设计模式
  5. Linux中SDIO命令,Linux MMC/SD/SDIO体系结构
  6. Go学习笔记(二)Go语言结构
  7. 查看pod网络范围_K8S Pod 内抓包快速定位网络问题
  8. Windows Azure 云计算服务: 怎样按需选择并计算它将花费多少钱呢?
  9. FastDFS入门一篇就够
  10. 导出苹果安装包IPA的几种方法
  11. 蒙古文输入法linux版,蒙古文输入法下载 德力海蒙古文输入法 V2.1.3 官方安装版(附使用手册) 下载-脚本之家...
  12. 从福彩官网抓取 双色球历史数据
  13. 苦禅箜mm让我帮她做的作业
  14. 转载:WVGA,QVGA,VGA,HVGA,WQVGA是什么意思?如何区别?
  15. 关于网络知识一些笔记(个人2022.06)
  16. mysql的or能去重吗_mysql查询数据去重
  17. 根据文件名修改文件(创业天使-xxx 120101_超清.mp4 -- 120101-创业天使-xxx.mp4)
  18. BootStrap-Table主子表
  19. dell服务器卡在启动界面_dell服务器系统开机提示错误解决方法
  20. wafer robot-晶圆机器人(防水型)

热门文章

  1. traceroute tracert
  2. web安全day47:口令、字典、crunch、cupp、hydra、MSF-psexec_psh的使用
  3. H3C nat转换实验
  4. NP、OSPF Stub区域
  5. 华为防火墙管理员角色和级别详解
  6. ansible 第一次练习
  7. Python学习之==接口开发
  8. 给我一个软件,我将操控一个机器人军团!
  9. 9-Shell的逻辑操作符知识介绍-实践及企业案例脚本剖析
  10. 解读2016之Golang篇:极速提升,逐步超越