生活随笔
收集整理的這篇文章主要介紹了
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中的前置通知、后置通知、环绕通知、异常通知的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。