生活随笔
收集整理的这篇文章主要介绍了
Spring3.0中的前置通知、后置通知、环绕通知、异常通知
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
观众类Audience~~
[java] view plain
copy 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
[java] view plain
copy package com.jCuckoo.demo; public interface Performer { void perform()throws Exception; }
定义魔术师Juggler,实现表演接口
[java] view plain
copy 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
[html] view plain
copy <?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>
测试类,获取魔术师,并进行表演。
[java] view plain
copy package com.jCuckoo.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.jCuckoo.demo.Performer; public class MainTest { 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毫秒。
总结
以上是生活随笔为你收集整理的Spring3.0中的前置通知、后置通知、环绕通知、异常通知的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。