这个是承接五的,这部分主要的内容是在XML中声明切面。

一、在XML中声明切面

让我们先看一下spring中的AOP配置元素有哪些:

AOP配置元素 用途
<aop:advisor> 定义AOP通知器
<aop:after> 定义AOP后置通知(不管被通知的方法是否成功)
<aop:after-returning> 定义AOP返回通知
<aop:around> 定义AOP环绕通知
<aop:aspect> 定义一个切面
<aop:aspectj-autoproxy> 启用@AspectJ注解驱动切面
<aop:before> 定义一个
<aop:config> 顶层的AOP配置元素,大多数的
<aop:declare-parents> 以透明的方式为被通知的对象引入额外的接口
<aop:pointcut> 定义一个切点
 //声明的Audience类
package concert;
public class Audience {
public void silenceCellPhones(){
System.out.println("Silence cell phones");
} public void takeSeats(){
System.out.println("Taking seats");
} public void applause(){
System.out.println("CLAP CLAP CLAP!!!");
} public void demadRefund(){
System.out.println("Demanding a refund");
}
}

1、声明前置和后置通知

通过XML方式将无注解的Audience声明为切面

  <aop:config>
//引用audience bean
<aop:aspect ref="audience">
<aop:before
pointcut="execution(** concert.Performance.perform(..))"
method="silenceCellPhone" /> <aop:before
pointcut="execution(** concert.Performance.perform(..))"
method="takeSeats" /> <aop:after-returning
pointcut="execution(** concert.Performance.perform(..))"
method="applause" /> <aop:after-throwing
pointcut="execution(** concert.Performance.perform(..))"
method="demandRefund" />
</aop:aspect>
</aop:config> //使用<aop:pointcut> 定义命名切点
<aop:config>
//引用audience bean
<aop:aspect ref="audience">
<aop:point
id="performance"
expression="execution(** concert.Performance.perform(..))"
/>
<aop:before
pointcut="performance"
method="silenceCellPhone" /> <aop:before
pointcut="performance"
method="takeSeats" /> <aop:after-returning
pointcut="performance"
method="applause" /> <aop:after-throwing
pointcut="performance"
method="demandRefund" />
</aop:aspect>
</aop:config>

2、声明环绕通知

 package concert;
import org.aspectj.lang.PreceedingJoinPoint; public class Audience{
public void watchPerformance(PreceedingJoinPoint jp){
try{
System.out.println("Sliencing cell phones");
System.out.println("Taking seats");
jp.proceed();
System.out.println("CLAP CLAP CLAP!!!");
}catch(Throwable e){
System.out.println("Demanding a refund");
}
}
} //xml中配置环绕通知
<aop:config>
<aop:aspect ref="audience">
<aop:pointcut id="performance" experssion="execution(** concert.Performance.perform(..))" />
<aop:around pointcut-ref="performance" method="watchPerformance" />
</aop:aspect>
</aop:config>

3、为通知传递参数

//使用参数化的通知来记录磁道播放的次数
package soundsystem;
import java.util.HashMap;
import java.util.Map; public class TrackCounter {
private Map<Integer,Integer> trackCounts = new HashMap<Integer,Integer>(); public void trackPlayed(int trackNumber) {} public void countTrack(int trackNumber){
int currentCount = getPlayCount(trackNumber);
trackCounts.put(trackNumber, currentCount +1);
} public int getPlayCount(int trackNumber){
return trackCounts.countainsKey(trackNumber) ? trackCounts.get(trackNumber) : 0;
}
} //在XML 中将TrackCounter配置为参数化的切面
<bean id="trackCounter" class="soundsystem.TrackCounter " />
<bean id="cd" class="soundsystem.BlackDisc" >
<property name="title" value="此时此刻" />
<property name="artist" value="许巍" />
<property name="tracks" >
<list>
<value>空谷幽兰</value>
<value>爱情</value>
<value>此时此刻</value>
<value>灵岩</value>
<value>救赎之旅</value>
<value>心愿</value>
<value>逍遥行</value>
<value>喜悦</value>
<value>世外桃源</value>
<value>出离</value>
</list>
</property>
</bean> <aop:config>
<aop:aspect ref="trackCounter">
<aop:pointcut id="trackPlayed" expression="execution(* soundsystem.CompactDisc.playerTrack(int)) and args(trackNumber)" />
<aop:before pointcut-ref="trackPlayed" method="countTrack" />
</aop:aspect>
</aop:config>

注意:我们使用了前面相同的aop命名空间XML元素,它们会将POJO声明为切面。唯一的区别是切点表达式中包含了一个参数,这个参数会传递到通知的方法中。

4、通过切面引入新功能

<aop:aspect>
<aop:declare-parents types-matching="concert.Performance" implement-interface="concert.Encoreable" default-impl="concert.DefaultEncoreable" />
</aop:aspect>

<aop:declare-parents>声明了此切面所通知的bean要在它的对象层次结构拥有新的父类型。具体到本例中,类型匹配Performance接口(由types-matching属性指定)的那些bean在父类结构中会增加Encoreable接口(由implement-interface属性指定)。

二、注入Aspect切面

当springAOP不能满足需求时,我们必须转向更为强大的AspectJ。这里主要注意的是如何使用spring为AspectJ 切面注入依赖。这里就不写了,我感觉一般也用不到,用到的时候在补充吧。。。

六、面向切面的spring(2)的更多相关文章

  1. Spring使用笔记(四) 面向切面的Spring

    面向切面的Spring 一.面向切面的概念 在软件开发中,散布于应用多处的功能被称为横切关注点(cross-cutting concern). 通常来讲这些横切关注带点从概念上来讲是与应用逻辑相分离的 ...

  2. Spring实战第四章学习笔记————面向切面的Spring

    Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...

  3. 五、面向切面的spring(1)

    spring的依赖注入看完了,接下来是spring中与DI一并重要的AOP了,开始吧,GO. 在软件开发中,散布于应用中多处的功能被称为横切发关注点,通常来讲,这些横切关注点从概念上市与应用的业务逻辑 ...

  4. Spring学习(四)--面向切面的Spring

    一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...

  5. 面向切面的Spring

    在软件开发中,发布于应用中多处的功能被称为横切关注点.通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(但往往直接嵌入到应用的业务逻辑之中).将横切关注点与业务逻辑相分离是AOP所要解决的. 一 ...

  6. Spring系列(四) 面向切面的Spring

    除了IOC外, AOP是Spring的另一个核心. Spring利用AOP解决应用横切关注点(cross-cutting concern)与业务逻辑的分离, 目的是解耦合. 横切关注点是指散布于代码多 ...

  7. 第04章-面向切面的Spring

    1. 什么是面向切面编程 AOP是什么 切面帮助我们模块化横切关注点. 横切关注点可被描述为影响应用[多处的]功能.如安全,应用许多方法会涉及安全规则. 继承与委托是最常见的实现重用 通用功能 的面向 ...

  8. Spring学习笔记(三):面向切面的Spring

    Spring之面向切面编程 一.理解何为面向切面编程 对于这个的理解,我觉得Spring实战中的例子讲得很明白: 假设我现在是一个小区用户,每个月小区都要收电费,这时候就会来人查看电表,算出来这个月电 ...

  9. Spring AOP 面向切面的Spring

    定义AOP术语 描述切面的常用术语有: 通知 (advice) 切点 (pointcut) 连接点 (joinpoint) 下图展示了这些概念是如何关联的 Spring 对AOP的支持 Spring提 ...

随机推荐

  1. x86 linux 裁剪过程中能正常跑起来的必要配置项

    A .选中Executable file formats/Emulations ---> Kernel support for ELFbinaries -----加载运行rootfs 中的程序. ...

  2. 从0开始学习Hadoop(2)安装JDK

    参考文档: 安装包方式安装:http://www.cnblogs.com/wuyudong/p/ubuntu-jdk8.html PPA方式安装:推荐  http://www.cnblogs.com/ ...

  3. Linux下 本地yum源搭建

    第1章 关于yum源 1.1 什么是yum源 yum(Yellow dog Updater, Modified)是一个在 Fedora 和 RedHat 以及 CentOS 中的 Shell 前端软件 ...

  4. nginx https反向代理tomcat

    Context体现在server.xml中的Host里的<Context>元素,它由Context接口定义.每个<Context>元素代表了运行在虚拟主机上的单个Web应用. ...

  5. CF916E

    Codeforces 916E 简要题解Description Description 有一棵n个点的树,每个节点上有一个权值wi,最开始根为1号点.现在有3种类型的操作: 1 root, 表示将根设 ...

  6. vijos1846 [NOIP2013] 华容道【最短路】

    传送门:https://vijos.org/p/1983 (其实noip的题各个oj都会有的,就不贴其它传送门了) 这道题真的是,怎么说,我都不知道怎么评价了= =.果然数据量小的题怎么暴力都可以过. ...

  7. HDFS执行getDatanodeReport输出信息

    HDFS执行getDatanodeReport输出信息: Name: (192.168.101.100) Hostname: bigsrv Decommission Status : Normal C ...

  8. Service官方教程(8)Bound Service示例之2-跨进程使用Messenger

    Compared to AIDL When you need to perform IPC, using a Messenger for your interface is simpler than ...

  9. go环境搭建及vscode中调试

    1.下载go安装包一般国内用户无法在官网下载,可以自行百度找一些共享的资源墙内下载地址: http://www.golangtc.com/downloadCSDN上资源下载(一般需要积分):http: ...

  10. Android最简单的实例 :环境搭建及HelloWorld

    Android开发之旅:环境搭建及HelloWorld 2010-04-12 00:45 by 吴秦, 883961 阅读, 140 评论, 收藏,  编辑 ——工欲善其事必先利其器 引言 本系列适合 ...