176142998

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            116 Posts :: 0 Stories :: 45 Comments :: 0 Trackbacks
          Spring 的快速開發(fā), 說是快速開發(fā), 其實(shí)能幫助的地方除了語法高亮和自動添加類庫外也沒多少東西了。

            1. 新建普通 Java 項(xiàng)目 MySpringTest. 這個過程無需贅述了, 建議建項(xiàng)目的時候?qū)?src 目錄和 bin(或者classes)目錄分開, 另外提示你切換透視圖的時候一定要切換過去到 Java 透視圖, 此時默認(rèn)會在 Package Explorer 中選中剛才已經(jīng)建好的 Java Project, 但是背景為灰色。

            2. 首先單擊一下左邊的 Package Explorer 中新建的 MySpringTest 項(xiàng)目來使其高亮選中, 接著點(diǎn)擊菜單項(xiàng) MyEclipse -> Add Spring Capabilities……, 接著會彈出對話框 Add Spring Capabilities 提示你設(shè)置當(dāng)前項(xiàng)目的 Spring 屬性。

            對話框的第一頁可以選擇全部的 Spring 框架, 這是最保險(xiǎn)的做法, 不過我們的例子只需要選中Spring 2.0 Core Libraries 就可以了。 點(diǎn)擊 "Next" 繼續(xù)。

            第二頁是 Add Spring bean configuration file. 保持默認(rèn)值不變就可以了。 最后點(diǎn)擊 Finish.

            3. Spring 的開發(fā)沒法自動生成 Bean, 這里大家只好手工來寫了, 也很簡單。 分別復(fù)制下面的三個代碼, 然后在 MyEclipse src 目錄上點(diǎn)擊右鍵后選擇菜單項(xiàng) Paste 就可以生成 Java 類文件了。

          public interface Action {
                      public String execute(String str);
                      }
                      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();
                      }
                      }
                      import org.springframework.context.ApplicationContext;
                      import org.springframework.context.support.ClassPathXmlApplicationContext;
                      public class TestAction {
                      public static void main(String[] args) {
                      ApplicationContext ctx = new ClassPathXmlApplicationContext(
                      "applicationContext.xml");
                      Action bean = (Action) ctx.getBean("theAction");
                      System.out.println(bean.execute("Rod"));
                      }
                      }

            4. 雙擊左側(cè)在第2步生成的 applicationContext.xml, 然后選擇菜單項(xiàng) Window -> Show View -> Other……, 在彈出的對話框中選擇 MyEclipse Enterprise Workbench 節(jié)點(diǎn)下的 Spring Beans 子節(jié)點(diǎn)打開視圖 Spring Beans. 此視圖講出現(xiàn)在主界面的右下側(cè)。

            5. 展開此視圖中的 MySpringTest 父節(jié)點(diǎn), 并選中 src/applicationContext.xml 子節(jié)點(diǎn), 在此節(jié)點(diǎn)上點(diǎn)擊右鍵并選擇彈出菜單項(xiàng)中的 New Bean 來打開 Create a new Spring bean 對話框, 并按照下圖輸入對應(yīng)的內(nèi)容。

            Bean Id: [theAction]

            Bean class: [UpperAction]

            接下來請單擊一下 Tab 面板 Properties 并點(diǎn)擊其中的 Add…… 按鈕, 在接下來彈出的 Property Wizard 對話框中按照下圖輸入/選擇內(nèi)容:

            Name: [message]

            Spring type: [value]

            Type: [java.lang.String]

            Value:[Hello_]

            最后點(diǎn)擊兩次 Finish 按鈕關(guān)閉所有向?qū)υ捒颉?然后點(diǎn)擊菜單 File -> Save. 此時可以看到 applicationContext.xml 的內(nèi)容如下所示:

          <?xml version="1.0" encoding="UTF-8"?>
          <beans
          xmlns=" xmlns:xsi=" xsi:schemaLocation="http://www.springframework.org/schema/beans


          <bean id="theAction" class="UpperAction" abstract="false"
          lazy-init="default" autowire="default" dependency-check="default">
          <property name="message">
          <value type="java.lang.String">Hello_</value>
          </property>
          </bean></beans>

            然后雙擊 Package Explorer 下 MySpringTest/src/TestAction.java 打開源代碼, 然后點(diǎn)擊菜單 Run -> Run As -> 1. Java Application, 如果沒有錯誤的話將會出現(xiàn)如下的輸入, 您的第一個 Hello Spring 運(yùn)行成功了:

          log4j:WARN No appenders could be found for logger 
          (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly. HELLO_ROD

            接著您就可以對著參考書繼續(xù)創(chuàng)建類, 修改 applicationContext.xml 做更多的練習(xí)了。

            開發(fā)整合 Hibernate 的關(guān)鍵操作點(diǎn)截圖:

            1. 在數(shù)據(jù)庫瀏覽器中選擇反向工程菜單;

            2. 對話框的選項(xiàng)說明



          http://java.chinaitlab.com/Spring/727426.html

          posted on 2008-06-12 15:15 飛飛 閱讀(217) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 满洲里市| 吉木乃县| 赞皇县| 榆社县| 遵义县| 辽中县| 开封市| 余庆县| 岢岚县| 石泉县| 乌恰县| 江孜县| 赫章县| 梧州市| 信阳市| 诸暨市| 宜兰县| 汕尾市| 克东县| 广南县| 巢湖市| 嘉黎县| 安吉县| 白朗县| 土默特左旗| 安龙县| 廊坊市| 濮阳市| 河东区| 五大连池市| 合阳县| 化隆| 建昌县| 建瓯市| 宿州市| 邓州市| 长乐市| 当涂县| 铁力市| 太湖县| 繁峙县|