laoding
          本來我以為,隱身了別人就找不到我,沒有用的,像我這樣拉風(fēng)的男人,無論走到哪里,都像在黑暗中的螢火蟲一樣,那樣的鮮明,那樣的出眾。我那憂郁的眼神,稀疏的胡茬,那微微隆起的將軍肚和親切的笑容......都深深吸引了眾人......
          posts - 0,  comments - 37,  trackbacks - 0

          一直就用spring的IOC,遺憾spring的另一重要組成部分AOP卻沒用過,所以近幾天抽空研究了下AOP,學(xué)了些東西,在這里記錄下spring2.0的aop配置,以一個(gè)簡(jiǎn)單的記錄日志的實(shí)例來說明,先介紹下用XMLSchema來配置,下一篇介紹annotation配置,廢話不多說,開始吧
          先新建個(gè)web工程,將spring的包加進(jìn)去,為方便就把全部的jar包加進(jìn)去。

          先來看個(gè)接口,很簡(jiǎn)單就兩個(gè)方法

          public interface Print {
              
          public String print(String name);
              
          public String sleep(String name);
          }

          接下來是實(shí)現(xiàn)類

          public class SystemPrint implements Print{
              
              
          public String print(String name){
                  String result
          ="hello " + name;
                  System.out.println(result);
                  
          return result;
              }
              
              
          public String sleep(String name){
                  String result
          =name+" is sleep now";
                  System.out.println(result);
                  
          return result;
              }
          }

          下面是所要織入的代碼,也就是我們要用來記錄日志的

          public class GetLog {
              
          public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {
                  String reslut 
          = (String)joinpoint.proceed();
                  
          //這里是記錄日志的
                  System.out.println("result==="+reslut);
              }
          }

          再來看spring配置文件,沒有注釋的很清楚,可以去網(wǎng)上查查

          <?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:jee
          ="http://www.springframework.org/schema/jee"
              xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
              http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
              http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd"
          >
              
              
          <!--這個(gè)bean是作為切面    -->
              
          <bean id="log" class="spring2aop.GetLog"></bean>

              
          <!--
                  注意這里:expression="execution(* spring2aop.*.print*(..))" 
                  括號(hào)里面第一個(gè)*號(hào)代表返回值 接下來  spring2aop.*. 是你要切入的代碼的大概路徑,這里為什么用大概路徑來形容呢
                  因?yàn)檫@里的意思是符合以spring2aop的路徑都會(huì)作為選擇的對(duì)象,也不詳細(xì)介紹,查下就明白了, print*(..)是指
                  方法名以print開頭的都符合,括號(hào)里面的 .. 表示參數(shù)是隨意的都可以。
              
          -->
              
          <aop:config>
                  
          <aop:aspect ref="log">
                      
          <aop:pointcut id="printMethods" expression="execution(* spring2aop.*.print*(..))"/>
                      
          <aop:after-returning method="getLog" pointcut-ref="printMethods" returning="retVal"/>
                  
          </aop:aspect>
              
          </aop:config>
              
              
          <aop:config>
                  
          <aop:aspect ref="log">
                      
          <aop:pointcut id="sleepMethods" expression="execution(* spring2aop.*.sle*(..))"/>
                      
          <aop:after-returning method="getLog" pointcut-ref="sleepMethods" returning="retVal"/>
                  
          </aop:aspect>
              
          </aop:config>
              
              
          <!--要織入代碼的bean-->
              
          <bean id="print" class="spring2aop.SystemPrint"></bean>

          </beans>

          測(cè)試類:
          public class Test {

              
          /**  
               *   @Description 方法實(shí)現(xiàn)功能描述  
               *   
          @param args
               *   void
               *   
          @throws  拋出異常說明
               
          */
              
          public static void main(String[] args) {
                  ApplicationContext act 
          = new ClassPathXmlApplicationContext(
                  
          "applicationContext20.xml");
                  Print t 
          =(Print)act.getBean("print");
                  t.print(
          "ding");
                  System.out.println(
          "-----------------");
                  t.sleep(
          "laoding");

              }


          }

          運(yùn)行這個(gè)類,得到如下結(jié)果:
          hello ding
          hello ding
          result===hello ding
          -----------------
          laoding is sleep now
          laoding is sleep now
          result===laoding is sleep now

          這里的hello ding 打印了兩次,不用擔(dān)心,這是因?yàn)閳?zhí)行到getLog切面類的
           String reslut = (String)joinpoint.proceed();這句代碼的時(shí)候再執(zhí)行了一次,這句代碼是取回
          返回結(jié)果的,可以設(shè)置個(gè)斷點(diǎn)來測(cè)試下好了這里就輸出的result就是記錄的日志,當(dāng)然
          這里只是個(gè)很簡(jiǎn)單的實(shí)現(xiàn),但是很簡(jiǎn)單的實(shí)現(xiàn)卻很容易說清楚原理。

          posted on 2008-11-25 18:14 老丁 閱讀(3469) 評(píng)論(4)  編輯  收藏 所屬分類: spring

          FeedBack:
          # re: spring aop簡(jiǎn)單日志實(shí)例(1)
          2009-11-07 14:16 | 真爛
          垃圾,您還是改行吧。  回復(fù)  更多評(píng)論
            
          # re: spring aop簡(jiǎn)單日志實(shí)例(1)
          2009-11-10 16:34 | 老丁
          朋友,請(qǐng)自重!如果覺得爛請(qǐng)繞道!
            回復(fù)  更多評(píng)論
            
          # re: spring aop簡(jiǎn)單日志實(shí)例(1)
          2009-11-12 11:53 |
          ProceedingJoinPoint 少一個(gè)類  回復(fù)  更多評(píng)論
            
          # re: spring aop簡(jiǎn)單日志實(shí)例(1)
          2011-11-15 15:32 | codeme
          <aop:config>
          <aop:aspect ref="log">
          <aop:pointcut id="printMethods" expression="execution(* spring2aop.*.print*(..))"/>
          <aop:after-returning method="getLog" pointcut-ref="printMethods" returning="retVal"/>
          </aop:aspect>
          </aop:config>

          <aop:config>
          <aop:aspect ref="log">
          <aop:pointcut id="sleepMethods" expression="execution(* spring2aop.*.sle*(..))"/>
          <aop:after-returning method="getLog" pointcut-ref="sleepMethods" returning="retVal"/>
          </aop:aspect>
          </aop:config>
          沒有這么配置的吧,怎么不寫到一起?  回復(fù)  更多評(píng)論
            
          本博客主為學(xué)習(xí)和復(fù)習(xí)之用,無關(guān)其他,想罵人的繞道
          Email:dkm123456@126.com
          大家一起交流進(jìn)步
          QQ:283582761


          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(4)

          我參與的團(tuán)隊(duì)

          文章分類(50)

          文章檔案(48)

          相冊(cè)

          朋友

          搜索

          •  

          積分與排名

          • 積分 - 96919
          • 排名 - 597

          最新評(píng)論

          主站蜘蛛池模板: 屯留县| 陇西县| 高雄县| 荔波县| 万州区| 仙桃市| 沙河市| 永修县| 来凤县| 罗定市| 边坝县| 温泉县| 星座| 南宁市| 江北区| 金平| 罗平县| 湟源县| 新乡县| 桂阳县| 武强县| 固安县| 鹿邑县| 海伦市| 福泉市| 读书| 长葛市| 石渠县| 临汾市| 广丰县| 曲周县| 黄浦区| 霍邱县| 永仁县| 瓦房店市| 莱州市| 文登市| 兴业县| 东乡族自治县| 霍山县| 沛县|