日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

發(fā)布時(shí)間:2025/6/15 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring3.0中的前置通知、后置通知、环绕通知、异常通知 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

觀眾類Audience~~

[java] view plain copy
  • package?com.jCuckoo.demo;??
  • ??
  • import?org.aspectj.lang.ProceedingJoinPoint;??
  • ??
  • public?class?Audience?{??
  • ????public?void?takeSeats()?{??
  • ????????System.out.println("----開演之前,請(qǐng)占座----");??
  • ????}??
  • ??
  • ????public?void?turnOffCellPhones()?{??
  • ????????System.out.println("----開始之前,請(qǐng)關(guān)機(jī)----");??
  • ????}??
  • ??
  • ????public?void?applaud()?{??
  • ????????System.out.println("****鼓掌,繼續(xù)鼓掌。****");??
  • ????}??
  • ????public?void?turnOnCellPhones()?{??
  • ????????System.out.println("****演出結(jié)束,可以開機(jī)****");??
  • ????}??
  • ??
  • ????public?void?demandRefund()?{??
  • ????????System.out.println(" 退 ");??
  • ????}??
  • ??
  • ????public?void?watchPerformance(ProceedingJoinPoint?joinpoint)?{??
  • ????????try?{??
  • ????????????System.out.println("oooooooo環(huán)繞通知開始o(jì)ooooooo");??
  • ????????????long?start?=?System.currentTimeMillis();??
  • ????????????joinpoint.proceed();??
  • ????????????long?end?=?System.currentTimeMillis();??
  • ????????????System.out.println("oooooooo環(huán)繞通知結(jié)束oooooooo");??
  • ????????????System.out.println("演出耗時(shí)共計(jì):"?+?(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;??
  • }??

  • 定義魔術(shù)師Juggler,實(shí)現(xiàn)表演接口

    [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("表演開始:魔術(shù)師欺騙了"?+?beanBags?+?"個(gè)游戲豆。");??
  • ????}??
  • }??

  • 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>??

  • 測(cè)試類,獲取魔術(shù)師,并進(jìn)行表演。

    [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?{??
  • ??
  • ????/**?
  • ?????*?@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();??
  • ????????}??
  • ??
  • ????}??
  • ??
  • }??

  • 最終結(jié)果:

    ?

    ----開演之前,請(qǐng)占座----
    ----開始之前,請(qǐng)關(guān)機(jī)----
    oooooooo環(huán)繞通知開始o(jì)ooooooo
    表演開始:魔術(shù)師欺騙了3個(gè)游戲豆。
    ****演出結(jié)束,可以開機(jī)****
    ****鼓掌,繼續(xù)鼓掌。****
    oooooooo環(huán)繞通知結(jié)束oooooooo
    演出耗時(shí)共計(jì):1毫秒。

    總結(jié)

    以上是生活随笔為你收集整理的Spring3.0中的前置通知、后置通知、环绕通知、异常通知的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。