六、面向切面的spring(2)
这个是承接五的,这部分主要的内容是在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)的更多相关文章
- Spring使用笔记(四) 面向切面的Spring
面向切面的Spring 一.面向切面的概念 在软件开发中,散布于应用多处的功能被称为横切关注点(cross-cutting concern). 通常来讲这些横切关注带点从概念上来讲是与应用逻辑相分离的 ...
- Spring实战第四章学习笔记————面向切面的Spring
Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...
- 五、面向切面的spring(1)
spring的依赖注入看完了,接下来是spring中与DI一并重要的AOP了,开始吧,GO. 在软件开发中,散布于应用中多处的功能被称为横切发关注点,通常来讲,这些横切关注点从概念上市与应用的业务逻辑 ...
- Spring学习(四)--面向切面的Spring
一.Spring--面向切面 在软件开发中,散布于应用中多处的功能被称为横切关注点(cross- cutting concern).通常来讲,这些横切关注点从概念上是与应用的业 务逻辑相分离的(但是往 ...
- 面向切面的Spring
在软件开发中,发布于应用中多处的功能被称为横切关注点.通常,这些横切关注点从概念上是与应用的业务逻辑相分离的(但往往直接嵌入到应用的业务逻辑之中).将横切关注点与业务逻辑相分离是AOP所要解决的. 一 ...
- Spring系列(四) 面向切面的Spring
除了IOC外, AOP是Spring的另一个核心. Spring利用AOP解决应用横切关注点(cross-cutting concern)与业务逻辑的分离, 目的是解耦合. 横切关注点是指散布于代码多 ...
- 第04章-面向切面的Spring
1. 什么是面向切面编程 AOP是什么 切面帮助我们模块化横切关注点. 横切关注点可被描述为影响应用[多处的]功能.如安全,应用许多方法会涉及安全规则. 继承与委托是最常见的实现重用 通用功能 的面向 ...
- Spring学习笔记(三):面向切面的Spring
Spring之面向切面编程 一.理解何为面向切面编程 对于这个的理解,我觉得Spring实战中的例子讲得很明白: 假设我现在是一个小区用户,每个月小区都要收电费,这时候就会来人查看电表,算出来这个月电 ...
- Spring AOP 面向切面的Spring
定义AOP术语 描述切面的常用术语有: 通知 (advice) 切点 (pointcut) 连接点 (joinpoint) 下图展示了这些概念是如何关联的 Spring 对AOP的支持 Spring提 ...
随机推荐
- bzoj 2962 序列操作 —— 线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2962 维护 sum[i] 表示选 i 个的乘积和,合并两个子树就枚举两边选多少,乘起来即可: ...
- 手把手VirtualBox虚拟机下安装rhel6.4 linux位系统详细文档
使用Virtual Box,感觉跟Vmware差不多,我的本子的系统是win7 64位. 下面演示安装的是在VirtualBox里安装rhel 6.4 linux 32位系统.32位系统安装和 64位 ...
- bzoj 1096: [ZJOI2007]仓库建设【斜率优化】
好眼熟啊 直接dp显然很难算,所以设val为只在n点建一个仓库的费用,然后设f[i]为在i~n点建若干仓库并且i点一定建一个仓库的最大省钱数 转移很显然,设s为p的前缀和,f[i]=max{f[j]+ ...
- bzoj 1625: [Usaco2007 Dec]宝石手镯【背包】
裸的01背包 #include<iostream> #include<cstdio> using namespace std; int c,n,w,v,f[20001]; in ...
- 洛谷 P3356 火星探险问题 【最大费用最大流】
输出方案好麻烦啊 拆点,石头的连(i,i',1,1)(i,i',inf,0)表示可以取一次价值1,空地直接连(i,i',inf,0),对于能走到的两个格子(不包括障碍),连接(i',j,inf,0), ...
- react-native页面之间的相互传值
react-native页面之间的相互传值 之前在自己学习react-native的时候,在页面跳转和传值问题上花了一段时间去网上搜索和查找资料,自己总结了两个方法.可以参考 https://blog ...
- 30行JavaScript代码实现一个比特币量化策略
精简极致的均线策略 30行打造一个正向收益系统 原帖地址:https://www.fmz.com/bbs-topic-new/262 没错!你听的没错是30行代码!仅仅30行小编我习惯先通篇来看看 代 ...
- 在JS/jQuery中,怎么触发input的keypress/keydown/keyup事件?
怎么触发keypress/keydown/keyup事件? 问题: 1.在之前的写的input后面添加了搜索按钮 2.input只有keyup事件,如下: $("#desktop_folde ...
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- 关于python深度学习网站
大数据文摘作品,转载要求见文末 编译团队|姚佳灵 裴迅 简介 ▼ 深度学习,是人工智能领域的一个突出的话题,被众人关注已经有相当长的一段时间了.它备受关注是因为在计算机视觉(Computer Vi ...