热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

在XML中配置AOP

在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面:在XML文件中配置aop命名空间:<beansxmlnshttp:www.springfram

在Spring的aop命名空间中,提供了多个元素用来在XML中声明切面:
这里写图片描述

在XML文件中配置aop命名空间:

"http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cOntext="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">

一、声明前置和后置通知

1、定义一个普通的POJO作为通知(增强advice)

public class Audience {
public void silenceCellphone(){
System.out.println("please silence cell phone");
}

public void takeSeates(){
System.out.println("please take Seate");
}

public void applause(){
System.out.println("CLAP CLAP CLAP");
}

public void demandRefund(){
System.out.println("Refund refund");
}
}

2、在XML中声明前置通知和后置通知


<bean id="audience" class="org.aop.Audience"/>

<aop:config>

<aop:aspect ref="audience">
<aop:before pointcut="execution(* org.aop.Perform.play())" method="silenceCellphone"/>
<aop:before pointcut="execution(* org.aop.Perform.play())" method="takeSeates"/>
<aop:after method="applause" pointcut="execution(* org.aop.Perform.play())"/>
<aop:after-throwing method="demandRefund" pointcut="execution(* org.aop.Perform.play())"/>
aop:aspect>
aop:config>

在<\aop:config>元素内,可以声明一个或多个通知器、切面或切点。
对于上面重复的切点pointcut属性值,可以进行优化:

<aop:config>

<aop:aspect ref="audience">

<aop:pointcut id="point" expression="execution(* org.aop.Perform.play())"/>

<aop:before pointcut-ref="point" method="silenceCellphone"/>
<aop:before pointcut-ref="point" method="takeSeates"/>
<aop:after method="applause" pointcut-ref="point"/>
<aop:after-throwing method="demandRefund" pointcut-ref="point"/>
aop:aspect>
aop:config>

如果想让定义的切点可以在多个切面使用,可以把<\aop:pointcut>元素放在<\aop:config>元素的范围内。

3、目标对象

@Component
public class Perform implements Performence {
public void play(){
System.out.println("play what");
}
}

4、测试一下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locatiOns= {"classpath:spring-aop.xml"})
public class CDPConfigTest {

@Autowired
private Performence perform;

@Test
public void doP(){
perform.play();
}
}
//结果:
please silence cell phone
please take Seate
play what
CLAP CLAP CLAP

二、声明环绕通知
1、定义一个新的环绕通知

@Component
public class AroundAudience {
public void watchPerformance(ProceedingJoinPoint jp){
try{
System.out.println("Silencing cell phones");
System.out.println("Taking seats");
//执行被通知的方法
jp.proceed();
System.out.println("CLAP CLAP CALP");
}catch (Throwable e){
System.out.println("Demanding a refund");
}
}
}

2、在XML中声明环绕通知

 <aop:config>
<aop:aspect ref="aroundAudience">
<aop:pointcut id="aroundPoint" expression="execution(* org.aop.Perform.aroundPlay())"/>
<aop:around method="watchPerformance" pointcut-ref="aroundPoint"/>
aop:aspect>
aop:config>

3、测试一下

@Autowired
private Performence perform;
@Test
public void doP(){
//类型如果是接口,那方法一定要在接口中有定义
perform.aroundPlay();
}

三、为通知传递参数
传递到目标对象中的某个方法的参数,也可以传递到通知中。
通知中与目标对象方法中的参数名保持一致。并且,在通知中对参数的修改不会改变目标对象的方法参数
1、一个通知

public void countTrack(int Num){
Num++;
System.out.println("通知中 trackNum="+Num);
}

2、XML中配置

<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="trackplay" expression="execution(* org.aop.Perform.palyTrace(int)) and args(Num)"/>
<aop:before method="countTrack" pointcut-ref="trackplay"/>
aop:aspect>
aop:config>

3、目标对象

public void palyTrace(int Num){
System.out.println("目标对象 trackNum="+Num);
}

4、测试一下

@Test
public void doP(){
//类型如果是接口,那方法一定要在接口中有定义
perform.palyTrace(8);
}
//结果
通知中 trackNum=9
目标对象 trackNum=8

四、通过切面引入新的功能
1、新功能所在的接口(要引入的)和实现类

//新功能接口
public interface Encorable {
void performEncore();
}

//新更能实现类
@Component
public class defaultEncore implements Encorable {
public void performEncore(){
System.out.print("这是新引入的功能");
}
}

2、XML中的配置

  <aop:config>
<aop:aspect>
<aop:declare-parents types-matching="org.aop.Performence+"
implement-interface="org.aop.Encorable"
default-impl="org.aop.defaultEncore"/>

aop:aspect>
aop:config>
types-matching:Performence接口所实现的子类,也就是对应的目标切点类。
implement-interface:新加入的接口类。
default-impl:新加入的接口的默认实现类。

也可以修改为:

<aop:config>
<aop:aspect>
<aop:declare-parents types-matching="org.aop.Performence+"
implement-interface="org.aop.Encorable"
delegate-ref="defaultEncore"/>

aop:aspect>
aop:config>

<bean id="defaultEncore" class="org.aop.defaultEncore"/>

4、测试一下

@Autowired
private Performence perform;

@Test
public void doP(){
//就像perform实现了Encorable接口一样
Encorable perEncore=(Encorable)perform;
perEncore.performEncore();
}

推荐阅读
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
author-avatar
黑鸽子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有