环绕通知:

 <?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的更多相关文章

  1. Spring Bean前置后置处理器的使用

    Spirng中BeanPostProcessor和InstantiationAwareBeanPostProcessorAdapter两个接口都可以实现对bean前置后置处理的效果,那这次先讲解一下B ...

  2. AOP 环绕通知 集成了前置 后置 返回通知等功能

    AOP 环绕通知 集成了前置 后置 返回通知等功能

  3. 20_AOP_Advice增强1(前置、后置、环绕)

    [增强的类型] 1.前置增强:org.springframework.aop.BeforeAdvice. 由于Spring只支持方法级别的增强,所以MethodBeforeAdvice是目前可用的前置 ...

  4. 19Spring返回通知&异常通知&环绕通知

    在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...

  5. Spring 如何保证后置处理器的执行顺序 - OrderComparator

    Spring 如何保证后置处理器的执行顺序 - OrderComparator Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.htm ...

  6. pytest_前置后置

    今天总结下pytest,pytest简直就是python自动化中的高富帅,各种操作,哈哈 这次总结主要涉及到了以下几点: 1.unittest中的setUp.tearDown.setUpClass.t ...

  7. unittest的前置后置,pytest的fixture和共享机制conftest.py

    Unittest setUp/tearDown setUp当中得到的变量,用self.xxx = value传递给测试用例 setUpClass/tearDownClass setupClass当中得 ...

  8. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  9. spring的几个通知(前置、后置、环绕、异常、最终)

    1.没有异常的 2.有异常的 1.被代理类接口Person.java package com.xiaostudy; /** * @desc 被代理类接口 * * @author xiaostudy * ...

随机推荐

  1. Java异常体系结构

    1)系统错误(system error)是由Java虚拟机抛出的,用Error类表示.Error类描述的是内部系统错误.这样的错误很少发生.如果发生,除了通知用户以及尽量稳妥地终止程序外,几乎什么都不 ...

  2. 如何在VMware虚拟机间建立共享磁盘?

    在同一台电脑上,有时难免要安装多个虚拟机,存储空间就成了最大的问题,那么如何解决虚拟机的硬盘问题呢,Vmware自带的工具可以很好的解决此问题,下面我们就来看看如何在Vmware虚拟机间建立共享磁盘? ...

  3. 格式化输出星期几 C#

    string Today = DateTime.Now.ToString("yyyy-MM-dd dddd",new System.Globalization.CultureInf ...

  4. Bootstrap-select:美化原生select

    官网:http://silviomoreto.github.io/bootstrap-select/ 1.下载zip 2.html代码 <select class="selectpic ...

  5. nginx的配置,要求根据不同的来路域名,发送到不同的端口去处理

    这一台电脑上既有tomcat 也有 apache,他俩是没有办法同时享用80端口的.我现在让tomcat用8088,apache用8080,然后让nginx用80,这样nginx在收到请求后,根据不同 ...

  6. C#单例模式的三种写法

    第一种最简单,但没有考虑线程安全,在多线程时可能会出问题,不过俺从没看过出错的现象,表鄙视我…… public class Singleton{    private static Singleton ...

  7. Android开发中这些小技巧

    http://blog.csdn.net/guxiao1201/article/details/40655661 http://blog.csdn.net/guxiao1201/article/det ...

  8. IIS6到7,web.config的配置

    如果在IIS6中你的web.config中是以下配置: 这是在IIS6中我们习惯的经典模式的配置 < system.web>  " " " " &q ...

  9. the hard thing about hard things 书摘

    1. from communist to VC 领导力是什么,书后面还举了乔布斯的例子,比如NEXT公司时期就是如此,是什么吸引了那些人在前景不明时还跟随乔布斯?   作者用自己与妻子的相遇说明,不要 ...

  10. 全面理解Linux输入输出重定向

    全面理解Linux输入输出重定向 本教程通过视频方式讲解shell操作,理解感念,教程通俗易懂,比起看一大堆文档要舒服的多.本次教程主要讲解  Linux Shell 中支持输入输出重定向,用符号&l ...