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 * ...
随机推荐
- NetBeans-xdebug的安装
如果总是提示正在连接,ok,应该是没有装xdebug; 1,下载Php版本对应的xdebug.dll文件,以下是官方提供的网址,可以智能判断环境,给出下载链接和使用指南 http://www.xdeb ...
- Android下实现tab页个人比较推崇的方法
使用fragment实现tab页的效果: 三个页面是单独的三个Fragment 主Activity的实现: package com.hsx.tab; import android.os.Bundle; ...
- RAC 之 RMAN 备份
这篇主要介绍的是RAC 环境下的RMAN 备份. 关于Oracle 备份与恢复的一些理论知识参考我的Blog: Oracle 备份 与 恢复 概述 http://blog.csdn.net ...
- IconRes提供免费高质量的Material风格android官方图标库
连接地址: http://www.iconres.com/android/index.php
- ArcEngine下投影坐标和经纬度坐标的相互转换
jojojojo2002 原文 ArcEngine下投影坐标和经纬度坐标的相互转换 投影转经纬度 private IPoint PRJtoGCS( double x, double y) { IPoi ...
- selenium python (四)键盘事件
#!/usr/bin/python# -*- coding: utf-8 -*-__author__ = 'zuoanvip' #在实际测试过程中,有时候我们需要使用tab键将焦点转移到下一个需要操作 ...
- sqlserver数据可空插入报错
数据库和C#中均为可空类型. 这时候直接给字段赋值为null parameters[9].Value = null : 执行的时候报错了,一大堆,总之说它少了一个参数. 用sql ser ...
- nmon基础
nmon是分析 AIX 和 Linux 性能的免费工具 最简单的安装方式(Ubuntu apt源) sudo apt-get install nmon 在terminal下打开nmon 敲CMD,出现 ...
- 比赛组队问题 --- 递归解法 --- java代码 --- 八皇后问题
两队比赛,甲队为A.B.C3人,乙队为X.Y.Z3人.已知A不和X比,C不和X.Z比,请编程序找出3队赛手名单 采用了与八皇后问题相似的解法,代码如下: 如有疑问请链接八皇后问题的解法:http:// ...
- bzoj1036 树的统计Count
第一次写链剖,于是挑了个简单的裸题写. 以下几点要注意: 1.链剖中的height是从根到该店经过的轻边个数 2.分清num与sum..... #include<cstdio> #incl ...