狂奔 lion

          自強(qiáng)不息

          用代碼一步步學(xué)習(xí)Spring:IoC,AOP

          1 從http://www.springframework.org下載Spring
          2 用eclipse新建Java項(xiàng)目
          3 建立我們的業(yè)務(wù)方法接口
          public interface BusinessObject {
              
          public void doSomething();
              
          public void doAnotherThing();
          }


          4 實(shí)現(xiàn)業(yè)務(wù)方法,注意這是的setWords使用了依賴注入,所謂依賴注入就是把配置文件中的字符串什么的在程序運(yùn)行時(shí)“自動(dòng)”放到我們的程序中來。如果不是這樣,我們就只能在代碼中固化這些東西,從而違背了面向?qū)ο蟮囊蕾嚨怪迷瓌t,還有一種滿足依賴倒置的方法,即依賴查詢,這就是所謂的factory模式,即在代碼中請(qǐng)求某種抽象的東西,然后根據(jù)配置得到它,但這種辦法向?qū)τ谝蕾囎⑷攵嗔藢?duì)環(huán)境的依賴,且代碼冗余,EJB的JNDI查詢就屬于這種。另外我們的Spring配置文件是以bean為核心的,就是我們寫的一個(gè)類,在XML中描述它的名稱、位置和涵蓋的內(nèi)容、關(guān)系。
          public class BusinessObjectImpl implements BusinessObject {
              
          private String words;
              
          public void setWords(String words){
                  
          this.words = words;
              }
              
          public void doSomething() {
                  Log log 
          = LogFactory.getLog(this.getClass());
                  log.info(words);
              }
              
          public void doAnotherThing() {
                  Log log 
          = LogFactory.getLog(this.getClass());
                  log.info(
          "Another thing");
              }

          }

          5 建立一個(gè)運(yùn)行方法類,從配置文件spring-beans.xml中讀入bo這個(gè)類的定義,然后實(shí)例化一個(gè)對(duì)象
          import org.springframework.beans.factory.xml.XmlBeanFactory;
          import org.springframework.core.io.ClassPathResource;


          public class Main {
              
          public static void main(String[] args){
                  XmlBeanFactory xbf 
          = new XmlBeanFactory(new ClassPathResource("spring-beans.xml"));
                  BusinessObject bo 
          = (BusinessObject)xbf.getBean("bo");
                  bo.doSomething();
                  bo.doAnotherThing();
              }
          }

          6 建立一個(gè)攔截器類invoke是MethodInterceptor必須實(shí)現(xiàn)的方法,表示攔截時(shí)的動(dòng)作,大家仔細(xì)體會(huì)代碼中的含義
          import org.aopalliance.intercept.MethodInterceptor;
          import org.aopalliance.intercept.MethodInvocation;
          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;


          public class MyInterceptor implements MethodInterceptor {
              
          private String before, after;
              
          public void setAfter(String after) {
                  
          this.after = after;
              }
              
          public void setBefore(String before) {
                  
          this.before = before;
              }
              
          public Object invoke(MethodInvocation invocation) throws Throwable {
                  Log log 
          = LogFactory.getLog(this.getClass());
                  log.info(before);
                  Object rval 
          = invocation.proceed();
                  log.info(after);
                  
          return rval;
              }
          }

          7 建立配置文件組織上面的類之間的關(guān)系,AOP有切入點(diǎn)和增強(qiáng)這兩個(gè)重要的概念,把兩個(gè)概念結(jié)合到一起,就是一個(gè)在某個(gè)方法執(zhí)行的時(shí)候附加執(zhí)行,切入點(diǎn)表示在哪里附加,增強(qiáng)表示附加什么,配置文件中的myPointcut表示切入點(diǎn),myInterceptor表示增強(qiáng)的內(nèi)容,myAdvisor表示增強(qiáng)器,即兩者的結(jié)合,在bo這個(gè)bean中,我們把這個(gè)增強(qiáng)器附加到了bo這個(gè)bean上。

          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
          <beans>
              
          <bean id="businessObjectImpl" class="BusinessObjectImpl">
                  
          <property name="words">
                      
          <value>正在執(zhí)行業(yè)務(wù)方法</value>
                  
          </property>
              
          </bean>
              
          <bean id="myInterceptor" class="MyInterceptor">
                  
          <property name="before">
                      
          <value>執(zhí)行業(yè)務(wù)方法前</value>
                  
          </property>
                  
          <property name="after">
                      
          <value>執(zhí)行業(yè)務(wù)方法后</value>
                  
          </property>
              
          </bean>
              
          <bean id="myPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
              
          <property name="patterns">
                  
          <list>
                      
          <value>BusinessObject.doSomething</value>
                  
          </list>
              
          </property>
              
          </bean>
              
          <bean id="myAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
                
          <property name="pointcut" ref="myPointcut"/>
                
          <property name="advice" ref="myInterceptor"/>
              
          </bean>
              
          <bean id="bo" class="org.springframework.aop.framework.ProxyFactoryBean">
                  
          <property name="target">
                      
          <ref local="businessObjectImpl"/>
                  
          </property>
                  
          <property name="proxyInterfaces">
                      
          <value>BusinessObject</value>
                  
          </property>
                  
          <property name="interceptorNames">
                      
          <list>
                          
          <value>myInterceptor</value>
                          
          <value>myAdvisor</value>
                      
          </list>
                  
          </property>
              
          </bean>
          </beans>


          8 運(yùn)行Main類,觀察控制臺(tái)輸出結(jié)果,重新審查代碼,反思為什么會(huì)出現(xiàn)這種結(jié)果。

           @2008 楊一. 版權(quán)所有. 保留所有權(quán)利

          posted on 2006-12-11 22:55 楊一 閱讀(2561) 評(píng)論(1)  編輯  收藏 所屬分類: Java EE

          評(píng)論

          # re: 用代碼一步步學(xué)習(xí)Spring:IoC,AOP 2007-04-11 20:57 spring study

          沒有創(chuàng)建myAdvisor這個(gè)類阿 運(yùn)行不了   回復(fù)  更多評(píng)論   

          <2006年12月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          公告

          本人在blogjava上發(fā)表的文章及隨筆除特別聲明外均為原創(chuàng)或翻譯,作品受知識(shí)產(chǎn)權(quán)法保護(hù)并被授權(quán)遵從 知識(shí)分享協(xié)議:署名-非商業(yè)性使用-相同方式共享 歡迎轉(zhuǎn)載,請(qǐng)?jiān)谵D(zhuǎn)載時(shí)注明作者姓名(楊一)及出處(www.aygfsteel.com/yangyi)
          /////////////////////////////////////////
          我的訪問者

          常用鏈接

          留言簿(5)

          隨筆分類(55)

          隨筆檔案(55)

          相冊(cè)

          Java

          其他技術(shù)

          生活

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          自強(qiáng)不息


          用心 - 珍惜時(shí)間,勇于創(chuàng)造
          主站蜘蛛池模板: 宜州市| 滦南县| 南陵县| 泾川县| 峨山| 濮阳县| 临泽县| 承德县| 霍山县| 渑池县| 临沭县| 夏邑县| 丹凤县| 永靖县| 安康市| 策勒县| 唐海县| 子洲县| 镇雄县| 安义县| 海淀区| 文昌市| 商都县| 枣阳市| 湾仔区| 武穴市| 渝中区| 奈曼旗| 厦门市| 若尔盖县| 江门市| 禹州市| 渑池县| 西充县| 城固县| 宁蒗| 固原市| 五台县| 藁城市| 花莲市| 百色市|