狂奔 lion

          自強不息

          用代碼一步步學習Spring:IoC,AOP

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


          4 實現業務方法,注意這是的setWords使用了依賴注入,所謂依賴注入就是把配置文件中的字符串什么的在程序運行時“自動”放到我們的程序中來。如果不是這樣,我們就只能在代碼中固化這些東西,從而違背了面向對象的依賴倒置原則,還有一種滿足依賴倒置的方法,即依賴查詢,這就是所謂的factory模式,即在代碼中請求某種抽象的東西,然后根據配置得到它,但這種辦法向對于依賴注入多了對環境的依賴,且代碼冗余,EJB的JNDI查詢就屬于這種。另外我們的Spring配置文件是以bean為核心的,就是我們寫的一個類,在XML中描述它的名稱、位置和涵蓋的內容、關系。
          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 建立一個運行方法類,從配置文件spring-beans.xml中讀入bo這個類的定義,然后實例化一個對象
          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 建立一個攔截器類invoke是MethodInterceptor必須實現的方法,表示攔截時的動作,大家仔細體會代碼中的含義
          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 建立配置文件組織上面的類之間的關系,AOP有切入點和增強這兩個重要的概念,把兩個概念結合到一起,就是一個在某個方法執行的時候附加執行,切入點表示在哪里附加,增強表示附加什么,配置文件中的myPointcut表示切入點,myInterceptor表示增強的內容,myAdvisor表示增強器,即兩者的結合,在bo這個bean中,我們把這個增強器附加到了bo這個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>正在執行業務方法</value>
                  
          </property>
              
          </bean>
              
          <bean id="myInterceptor" class="MyInterceptor">
                  
          <property name="before">
                      
          <value>執行業務方法前</value>
                  
          </property>
                  
          <property name="after">
                      
          <value>執行業務方法后</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 運行Main類,觀察控制臺輸出結果,重新審查代碼,反思為什么會出現這種結果。

           @2008 楊一. 版權所有. 保留所有權利

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

          評論

          # re: 用代碼一步步學習Spring:IoC,AOP 2007-04-11 20:57 spring study

          沒有創建myAdvisor這個類阿 運行不了   回復  更多評論   

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

          導航

          公告

          本人在blogjava上發表的文章及隨筆除特別聲明外均為原創或翻譯,作品受知識產權法保護并被授權遵從 知識分享協議:署名-非商業性使用-相同方式共享 歡迎轉載,請在轉載時注明作者姓名(楊一)及出處(www.aygfsteel.com/yangyi)
          /////////////////////////////////////////
          我的訪問者

          常用鏈接

          留言簿(5)

          隨筆分類(55)

          隨筆檔案(55)

          相冊

          Java

          其他技術

          生活

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          自強不息


          用心 - 珍惜時間,勇于創造
          主站蜘蛛池模板: 永寿县| 新龙县| 望城县| 志丹县| 昌乐县| 松溪县| 钦州市| 叙永县| 哈密市| 五台县| 福安市| 樟树市| 梅州市| 常宁市| 木兰县| 宜黄县| 瓦房店市| 泊头市| 浪卡子县| 得荣县| 潍坊市| 布尔津县| 彭山县| 姜堰市| 成安县| 开鲁县| 仁怀市| 克什克腾旗| 漳浦县| 永福县| 琼中| 梨树县| 武鸣县| 太白县| 九龙城区| 通化市| 农安县| 怀柔区| 锦州市| 达拉特旗| 颍上县|