shenang博客技術(shù)文檔


          理論不懂就實(shí)踐,實(shí)踐不會就學(xué)理論!

          posts - 35,comments - 55,trackbacks - 0
          一、準(zhǔn)備工作
          下載SpringFramework的最新版本,并解壓縮到指定目錄。
          在IDE中新建一個項(xiàng)目,并將Spring.jar將其相關(guān)類庫加入項(xiàng)目。 
          二、構(gòu)建 Spring 基礎(chǔ)代碼 
          1. Action接口:
          Action 接口定義了一個 execute 方法
          execute方法,以完成目標(biāo)邏輯。 

          public interface Action {
           
            public String execute(String str);
           


          2. Action接口的兩個實(shí)現(xiàn)UpperAction、LowerAction 

          public class UpperAction implements Action {
           
            private String message;
           
            public String getMessage() {
             return message;
            }
           
            public void setMessage(String string) {
              message = string;
            }
           
            public String execute(String str) {
             return (getMessage() + str).toUpperCase();
            }


          UpperAction將其message屬性與輸入字符串相連接,并返回其大寫形式。 
            SpringFrameWork Developer’s Guide  Version 0.6
           
          October 8, 2004     So many open source projects. Why not Open your Documents?
            public String getMessage() {
             return message;
            }
           
            public void setMessage(String string) {
              message = string;
            }
           
            public String execute(String str) {
             return (getMessage()+str).toLowerCase();
            }
          }
           
          LowerAction將其message屬性與輸入字符串相連接,并返回其小寫形式。 
           
          3. Spring配置文件(bean.xml)
          <beans>
               <description>Spring Quick Start</description>
               <bean id="TheAction"
           class="net.xiaxin.spring.qs.UpperAction">
             <property name="message">
          <value>HeLLo</value>
          </property>
            </bean>
          </beans>
          (請確保配置bean.xml位于工作路徑之下,注意工作路徑并不等同于CLASSPATH ,eclipse
          的默認(rèn)工作路徑為項(xiàng)目根路徑,也就是.project文件所在的目錄,而默認(rèn)輸出目錄/bin是項(xiàng)目
          CLASSPATH的一部分,并非工作路徑。 ) 
           
          4. 測試代碼 
            public void testQuickStart() {
           
              ApplicationContext ctx=new 
          FileSystemXmlApplicationContext("bean.xml");
             
              Action action = (Action) ctx.getBean("TheAction");
             
              System.out.println(action.execute("Rod Johnson"));
           
           }
          可以看到,上面的測試代碼中,我們根據(jù)"bean.xml"創(chuàng)建了一個ApplicationContext實(shí)
          例,并從此實(shí)例中獲取我們所需的Action實(shí)現(xiàn)。
           
             SpringFrameWork Developer’s Guide  Version 0.6
           
          October 8, 2004     So many open source projects. Why not Open your Documents?
          運(yùn)行測試代碼,我們看到控制臺輸出:
          ……
          HELLO ROD JOHNSON
           
           
          我們將bean.xml中的配置稍加修改:
          <bean id="TheAction" 
          class="net.xiaxin.spring.qs.LowerAction"/>
           
          再次運(yùn)行測試代碼,看到:
          ……
          hello rod johnson
           
           
          示例完成!
           
          很簡單的示例!從這個示例呢,可以簡單的了解Spring的基本構(gòu)造。但是,看完這些,還不明白Spring的好處。

          下一篇文章將就我的理解談?wù)凷ping的好處。
          posted on 2009-03-23 09:48 重慶理工小子 閱讀(2348) 評論(0)  編輯  收藏 所屬分類: Spring2
          主站蜘蛛池模板: 宁陕县| 疏附县| 鸡东县| 房产| 射阳县| 康马县| 庆云县| 凤山市| 肥乡县| 石嘴山市| 临沭县| 靖宇县| 连江县| 桐柏县| 平南县| 江达县| 彝良县| 那曲县| 历史| 平山县| 察雅县| 海原县| 安乡县| 黄梅县| 七台河市| 维西| 从化市| 当涂县| 贺兰县| 黄石市| 星座| 荥经县| 香河县| 双牌县| 屯门区| 衡东县| 武汉市| 沙坪坝区| 汾阳市| 西丰县| 龙井市|