Spring3.0中的前置通知、后置通知、环绕通知、异常通知

观众类Audience~~

package com.jCuckoo.demo;

import org.aspectj.lang.ProceedingJoinPoint;

public class Audience {
	public void takeSeats() {
		System.out.println("----开演之前,请占座----");
	}

	public void turnOffCellPhones() {
		System.out.println("----开始之前,请关机----");
	}

	public void applaud() {
		System.out.println("****鼓掌,继续鼓掌。****");
	}
	public void turnOnCellPhones() {
		System.out.println("****演出结束,可以开机****");
	}

	public void demandRefund() {
		System.out.println("$$太熊了,退我票钱$$");
	}

	public void watchPerformance(ProceedingJoinPoint joinpoint) {
		try {
			System.out.println("oooooooo环绕通知开始oooooooo");
			long start = System.currentTimeMillis();
			joinpoint.proceed();
			long end = System.currentTimeMillis();
			System.out.println("oooooooo环绕通知结束oooooooo");
			System.out.println("演出耗时共计:" + (end - start)
					+ "毫秒。");
		} catch (Throwable t) {
			System.out.println("Boo!Wewantourmoneyback!");
		}
	}
}


表演接口Performer

package com.jCuckoo.demo;

public interface Performer {
	void perform()throws Exception;
}


定义魔术师Juggler,实现表演接口

package com.jCuckoo.demo;

public class Juggler implements Performer {
	private int beanBags = 3;

	public Juggler() {
	}

	public Juggler(int beanBags) {
		this.beanBags = beanBags;
	}

	public void perform() throws Exception {
		System.out.println("表演开始:魔术师欺骗了" + beanBags + "个游戏豆。");
	}
}


spring的配置文档applicationContext.xml

<?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"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	<bean id="juggler" class="com.jCuckoo.demo.Juggler"/>
	<bean id="audience" class="com.jCuckoo.demo.Audience" />
	<aop:config>
		<aop:aspect ref="audience">
			<aop:pointcut id="performance"
				expression="execution(* com.jCuckoo.demo.Performer.perform(..))" />
			<aop:before pointcut-ref="performance" method="takeSeats" />
			<aop:before pointcut-ref="performance" method="turnOffCellPhones" />
			<aop:after pointcut-ref="performance" method="turnOnCellPhones" />
			<aop:after-returning pointcut-ref="performance"
				method="applaud" />
			<aop:after-throwing pointcut-ref="performance"
				method="demandRefund" />
			<aop:around pointcut-ref="performance" method="watchPerformance"/>
		</aop:aspect>
	</aop:config>
</beans>


测试类,获取魔术师,并进行表演。

package com.jCuckoo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.jCuckoo.demo.Performer;

public class MainTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		Performer performer=(Performer)ctx.getBean("juggler");
		try {
			performer.perform();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}


最终结果:

 

----开演之前,请占座----
----开始之前,请关机----
oooooooo环绕通知开始oooooooo
表演开始:魔术师欺骗了3个游戏豆。
****演出结束,可以开机****
****鼓掌,继续鼓掌。****
oooooooo环绕通知结束oooooooo
演出耗时共计:1毫秒。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值