spring 切面 前置后置通知 环绕通知demo
环绕通知:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> <bean id="saxophone" class="imple.Saxophone" /> <!-- aop -->
<bean id="audience" class="AopTest.Audience"/> <aop:config>
<aop:aspect ref="audience"> <aop:pointcut expression="execution(* imple.Saxophone.play(..))" id="performance"/>
<aop:around pointcut-ref="performance" method="watchPerformance"/>
</aop:aspect> </aop:config> </beans>
Saxophone:
package imple; import inter.Instrument; public class Saxophone implements Instrument{ private String song = "a"; public Saxophone(){ } public void play() {
System.out.println("TOOT TOOT TOOT");
} public void setSong(String song) {
this.song = song;
} }
package imple; import AopTest.aop;
import inter.Instrument;
import inter.Performer; public class Instrumentalist implements Performer { private String song;
private Instrument instrument; public void perform() {
// System.out.println("playing " + song + " : ");
instrument.play();
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }
运行结果:
begin!
TOOT TOOT TOOT
end! performance took 0 milliseconds
前置后置通知:
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> <bean id="saxophone" class="imple.Saxophone" />
<!-- aop -->
<bean id="audience" class="AopTest.Audience"/> <aop:config>
<aop:aspect ref="audience"> <aop:pointcut expression="execution(* imple.Saxophone.play(..))" id="performance"/> <aop:before method="takeSeats" pointcut-ref="performance"/>
<aop:before method="turnoffCellPhones" pointcut-ref="performance"/>
<aop:after-returning method="applaud" pointcut-ref="performance"/>
<aop:after-throwing method="demandRefund" pointcut-ref="performance"/>
</aop:aspect> </aop:config> </beans>
其余同上
测试代码:
@Test
public void test6() {
Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
instrumentalist.perform();
}
运行结果:
the audience is taking their seats.
the audience is turning off their cellphones
TOOT TOOT TOOT
CLAP CLAP CLAP CLAP CLAP
-----------------------------------------------------------以下基于注解实现前置后置通知-----------------------------------------------------------------------------
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="audienceAspectJ" class="AopTest.AudienceAspectJ"/> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> </beans>
package AopTest; import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class AudienceAspectJ { @Pointcut("execution(* imple.Saxophone.play(..))") //表达式
public void performance(){ //performance切点名字 } @Before("performance()")
public void takeSeats(){
System.out.println("AudienceAspectJ the audience is taking their seats.");
} @Before("performance()")
public void turnoffCellPhones(){
System.out.println("AudienceAspectJ the audience is turning off their cellphones");
} @AfterReturning("performance()")
public void applaud(){
System.out.println("AudienceAspectJ CLAP CLAP CLAP CLAP CLAP");
} @AfterThrowing("performance()")
public void demandRefund(){
System.out.println("AudienceAspectJ Boo! we wangt our money back!");
} }
package imple; import AopTest.aop;
import inter.Instrument;
import inter.Performer; public class Instrumentalist implements Performer { private String song;
private Instrument instrument; public void perform() {
// System.out.println("playing " + song + " : ");
instrument.play();
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }
package imple; import inter.Instrument; public class Saxophone implements Instrument{ private String song = "a"; public Saxophone(){ } public void play() {
System.out.println("TOOT TOOT TOOT");
} public void setSong(String song) {
this.song = song;
} }
@Test
public void test10(){
Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
instrumentalist.perform(); }
输出结果:
AudienceAspectJ the audience is taking their seats.
AudienceAspectJ the audience is turning off their cellphones
TOOT TOOT TOOT
AudienceAspectJ CLAP CLAP CLAP CLAP CLAP
-------------------------------------------------注解环绕通知--------------------------------------------------------------------------------
<?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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> <bean id="audienceAspectJ" class="AopTest.AudienceAspectJ"/> <bean id="instrumentalist" class="imple.Instrumentalist" >
<property name="song" value="Jingle Bells" />
<property name="instrument" >
<bean class="imple.Saxophone"/>
</property>
</bean> </beans>
package AopTest; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class AudienceAspectJ { @Pointcut("execution(* imple.Saxophone.play(..))")
public void performance(){ } @Around("performance()")
public void watchPerformance(ProceedingJoinPoint joinpoint) {
try {
System.out.println("AudienceAspectJ @Around begin!");
long start = System.currentTimeMillis(); joinpoint.proceed(); long end = System.currentTimeMillis();
System.out.println("AudienceAspectJ @Around end! performance took " + (end - start) + " milliseconds");
} catch (Throwable e) {
System.out.println("AudienceAspectJ @Around eee!We want our money back!");
}
} }
package imple; import AopTest.aop;
import inter.Instrument;
import inter.Performer; public class Instrumentalist implements Performer { private String song;
private Instrument instrument; public void perform() {
// System.out.println("playing " + song + " : ");
instrument.play();
} public String getSong() {
return song;
} public void setSong(String song) {
this.song = song;
} public Instrument getInstrument() {
return instrument;
} public void setInstrument(Instrument instrument) {
this.instrument = instrument;
} }
package imple; import inter.Instrument; public class Saxophone implements Instrument{ private String song = "a"; public Saxophone(){ } public void play() {
System.out.println("TOOT TOOT TOOT");
} public void setSong(String song) {
this.song = song;
} }
@Test
public void test10(){
Instrumentalist instrumentalist = (Instrumentalist) ctx.getBean("instrumentalist");
instrumentalist.perform(); }
输出结果
AudienceAspectJ @Around begin!
TOOT TOOT TOOT
AudienceAspectJ @Around end! performance took 0 milliseconds
spring 切面 前置后置通知 环绕通知demo的更多相关文章
- Spring Bean前置后置处理器的使用
Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...
- AOP 环绕通知 集成了前置 后置 返回通知等功能
AOP 环绕通知 集成了前置 后置 返回通知等功能
- 20_AOP_Advice增强1(前置、后置、环绕)
[增强的类型] 1.前置增强:org.springframework.aop.BeforeAdvice. 由于Spring只支持方法级别的增强,所以MethodBeforeAdvice是目前可用的前置 ...
- 19Spring返回通知&异常通知&环绕通知
在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...
- Spring 如何保证后置处理器的执行顺序 - OrderComparator
Spring 如何保证后置处理器的执行顺序 - OrderComparator Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.htm ...
- pytest_前置后置
今天总结下pytest,pytest简直就是python自动化中的高富帅,各种操作,哈哈 这次总结主要涉及到了以下几点: 1.unittest中的setUp.tearDown.setUpClass.t ...
- unittest的前置后置,pytest的fixture和共享机制conftest.py
Unittest setUp/tearDown setUp当中得到的变量,用self.xxx = value传递给测试用例 setUpClass/tearDownClass setupClass当中得 ...
- Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理
1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...
- spring的几个通知(前置、后置、环绕、异常、最终)
1.没有异常的 2.有异常的 1.被代理类接口Person.java package com.xiaostudy; /** * @desc 被代理类接口 * * @author xiaostudy * ...
随机推荐
- bootstrap-datetimepicker时间控件
欢迎各种吐槽. 本人小前端,学习过程中,某日遇到做时间控件的需求,于是无休止的召唤了度娘,发现看不太懂.算是为自己做个笔记,也便于菜鸟级别的看的懂. 首先,我们看看点击选择时间的时候的展示页面吧 年 ...
- Arduino中的数据类型范围
注意int不是4字节而仅仅是2字节!!! int: -32,768 ~ 32,767 (2字节) long: 4字节 http://www.arduino.cc/en/Reference/Int
- Xcode5 支持 SVN 1.7
Xcode升级了之后出现了各种问题,SVN升级到subversion 1.7后,Xcode自带有svn,版本是1.6,所以svn的1.6和1.7不兼容. 解决的办法,要么是降低系统的svn 版本,要么 ...
- 开启Nginx的gzip压缩功能详解
默认情况下,Nginx的gzip压缩是关闭的, gzip压缩功能就是可以让你节省不少带宽,但是会增加服务器CPU的开销哦,Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行 ...
- Locker
题意: 有2个数字串,每次可以变化1-3位(每位+1或-1(0-9,9-0)可循环),求由1串变到2串的最小用的次数. 分析: dp[i][num]表示变到第i位时最后两位组成的数是num时最小次数( ...
- Nodejs_day03
1.Stream (流) Stream有四种流类型 1.Readable - 可读操作 2.Writable - 可写操作 3.Duplex - 可读可写操作 4.Transform - 操作被写入数 ...
- Oracle 热备份batch脚本 Windows
1.最初来源于网络. 2.根据环境和喜好自己修改. 3.实测是可以完成备份任务的. 4.不推荐用于实际环境. bak.bat:执行时执行此脚本,其他脚本是调用和生成或者生成之后再调用.(需要自己修改先 ...
- python 安装预编译库注意事项-pip
一般安装依赖库用pip install 库名 就可以,某些情况下依赖的库需要安装预编译好的, 可以参考pip 安装时的错误信息 下面这个链接中可以直接下载 http://www.lfd.uci.edu ...
- 使用jqueryui
$(function () { $('#search_button').button(); /* 同时打开两个对话框 $('#reg').dialog(); $('#login').dialog(); ...
- Linux-head,tail用法
linux ---tail命令 linux中tail命令---用于查看文件内容 最基本的是cat.more和less. 1. 如果你只想看文件的前5行,可以使用head命令,如: head -5 /e ...