环绕通知:

 <?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. ORACLE 中KILL session

    我们知道,在Oracle数据库中,可以通过kill session的方式来终止一个进程,其基本语法结构为: alter system kill session 'sid,serial#' ; 被kil ...

  2. MSSQL常用操作及方法总结

    1.在安装Sql或sp补丁的时候系统提示之前有挂起的安装操作,要求重启的解决办法: 到注册表中找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control ...

  3. android LinearLayout 实现两端对齐

    <?xml version="1.0″ encoding="utf-8″?> <LinearLayout xmlns:android="http://s ...

  4. pdm 中怎么修改表的Name值时使Code值不变

    修改方法:PowerDesign中的选项菜单里修改,在[Tool]-->[General Options]->[Dialog]->[Operating modes]->[Nam ...

  5. Sikuli简介

    Sikuli是利用屏幕上能够看到的图型做自动化,能够通过这个手段来识别和控制元素,非常适合和Selenium和Robot Framework一起结合起来做自动化. 1.Sikuli主页 http:// ...

  6. Raspberry Pi3 ~ Eclipse中添加wiringPi 库函数

    这篇是在博客园原创 转载注明出处啊 以前用单片机.STM32之类的时候都是在一个集成的开发环境下进行的 比如Keil.IAR等 那么linux下编程,eclipse是个不错的选择 关于树莓派的GPIO ...

  7. 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理

    概述 iptables是一种运行在linux下的防火墙组件,下面的介绍可以快速的学习iptables的入门使用. 特点(重要) 它的工作逻辑分为 链.表.规则三层结构. 数据包通过的时候,在对应表中, ...

  8. 【原创】Docker容器及Spring Boot微服务应用

    Docker容器及Spring Boot微服务应用 1 什么是Docker 1.1 Docker的出现 问题一:项目实施环境复杂问题 传统项目实施过程中经常会出现“程序在我这跑得好好的,在你那怎么就不 ...

  9. Hbase学习记录(1)|伪分布式安装

    概述 Hbase –Haddop Database 是一个高性能,高可靠性.面向列.可伸缩的分布式存储系统. Hbase利用HDFS作为文件存储系统,利用MapReduce来处理Hbase的海量数据, ...

  10. Python 读取文件下所有内容、获取文件名、截取字符、写回文件

    # coding=gbk import os import os.path   #读取目录下的所有文件,包括嵌套的文件夹 def GetFileList(dir, fileList): newDir ...