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
          主站蜘蛛池模板: 即墨市| 北辰区| 临夏市| 宁德市| 龙海市| 隆化县| 盐亭县| 巴东县| 台南县| 平昌县| 卓尼县| 沧源| 东港市| 商丘市| 慈利县| 咸宁市| 重庆市| 平利县| 澄江县| 遵义市| 岳阳县| 广东省| 和平县| 浦江县| 高清| 嘉峪关市| 禄丰县| 定南县| 新平| 江津市| 台州市| 麻阳| 济宁市| 马公市| 克拉玛依市| 临颍县| 清水河县| 宜良县| 启东市| 资溪县| 江永县|