mvc 架構

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            74 Posts :: 60 Stories :: 5 Comments :: 0 Trackbacks
          posted on 2007-08-25 16:50 e全 閱讀(117) 評論(0)  編輯  收藏

          ?
          來源 http://hi.baidu.com/panqf/blog/item/3839ad13827fbc826538db48.html
          創] Spring 入門實戰
          2007-07-20 17:50
          一、創建項目
          ??? 創建名為“SpringHelloworld”的Web Project,如下圖:

          二、加入Spring支持??
          ??? 點擊菜單“MyEclipse” -- “Add Spring Capabities"啟動向導,在項目中加入對Spring的支持。
          ??? 第一步:如下圖:


          ??? 第二步:如下圖:

          三、增加Java包
          ??? 在src下面創建三個Java包,分別名為:com.pqf.beans, com.pqf.impl, com.pqf.test。 如下圖:

          四、編寫名為HelloWorld的JavaBean
          ??? 在com.pqf.beans 包下面創建 java 類,名為HelloWorld,其源代碼如下:
          ??? ??? public class HelloWorld {
          ??? ??? public String msg=null;

          ??? ??? public String getMsg() {
          ??? ?? ?? return msg;
          ??? ??? }

          ??? ??? public void setMsg(String msg) {
          ??? ?? ?? this.msg = msg;
          ??? ??? }
          ??? ??? }

          五、編寫不使用Spring的測試程序
          ??? 在 com.pqf.test 包下面創建測試程序,名為:TestHelloWorldNoSpring, 源代碼如下:
          ??? package com.pqf.test;
          ??? import com.pqf.beans.HelloWorld;
          ??? public class TestHelloWorldNoSpring {
          ??? public static void main(String[] args) {
          ??? ??? HelloWorld hw=new HelloWorld();
          ??? ??? hw.setMsg("向全世界問好!");
          ??? ??? System.out.println(hw.getMsg());
          ??? }
          ??? }

          ??? 運行這個測試程序,應該在控制臺輸出:向全世界問好!

          六、設置Spring配置文件
          ??? 雙擊打開 applicationContext.xml 文件,這個文件是Spring的核心配置文件。
          ??? 編輯修改文件內容如下:
          ??? <?xml version="1.0" encoding="UTF-8"?>
          ??? <beans xmlns="http://www.springframework.org/schema/beans"
          ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
          ??? <bean id="HelloWorld" class="com.pqf.beans.HelloWorld">
          ??? ??? <property name="msg">
          ??? ??? ??? <value>愿世界和平,向全世界問好!</value>
          ??? ??? </property>
          ??? </bean>

          </beans>

          ??? 其中,<bean> 描述將類 com.pqf.beans.HelloWorld 以 HelloWorld 為標識,注入到其他程序中;
          ??? <property> 描述將屬性 msg 的值注入到程序中。

          六、編寫使用Spring的測試程序
          ??? 在com.pqf.test包下面創建使用Spring技術的測試程序,源代碼如下:

          ??? import org.springframework.context.ApplicationContext;
          ??? import org.springframework.context.support.FileSystemXmlApplicationContext;
          ??? import com.pqf.beans.HelloWorld;

          ??? public class TestHelloWorldUseSpring {
          ??? public static void main(String[] args) {
          ??? ??? ApplicationContext actx = new FileSystemXmlApplicationContext(
          ??? ??? ??? ??? "/src/applicationContext.xml");??? //打開和分析Spring配置文件
          ??? ??? HelloWorld hw = (HelloWorld) actx.getBean("HelloWorld"); //反轉控制,得到注入的類
          ??? ??? System.out.println(hw.getMsg());??? //使用注入的類
          ??? }
          ??? }


          ???? 運行這個程序,控制臺將輸出: 愿世界和平,向全世界問好!

          七、測試注入
          ??? 編輯修改 applicationContext.xml 中屬性 msg 的值為其他只,再次運行上面的程序,控制臺將輸出新的 msg 的值。
          ??? 可見,變更輸出內容,無需修改程序。

          八、進一步:實現中英文輸出
          ??? 1、編寫接口文件:在 com.pqf.impl 包下面創建名為 Hello 的接口,源代碼如下:
          ??? ??? package com.pqf.impl;
          ??? ??? public interface Hello {
          ??? ??? ??? ??? public String SayHello();
          ??? ??? }

          2、實現接口:在com.pqf.beans下面,創建兩個Hello接口實現類,分別實現中文和英文的Hello接口。
          ??? 中文問候的實現:
          ??? package com.pqf.beans;
          ??? import com.pqf.impl.Hello;
          ??? public class cnHello implements Hello {
          ??? ??? public String msg = null;
          ??? ??? public String SayHello() {
          ??? ??? ??? return ”您好,“+msg;
          ??? ??? }
          ??? ??? public String getMsg() {
          ??? ??? ??? return msg;
          ??? ??? }
          ??? ??? public void setMsg(String msg) {
          ??? ??? ??? this.msg = msg;
          ??? ??? }
          ??? }


          英文問候的實現:
          ??? package com.pqf.beans;
          ??? import com.pqf.impl.Hello;
          ??? public class enHello implements Hello {
          ?? ???? public String msg=null;
          ?? ???? public String getMsg() {
          ?? ???? ??? return msg;
          ?? ???? }
          ?? ???? public void setMsg(String msg) {
          ?? ???? ??? this.msg = msg;
          ?? ???? }
          ?? ???? public String SayHello() {
          ?? ???? ??? return "Hello "+msg;
          ?? ???? }
          ??? }

          3、配置Spring配置文件
          ??? 修改Spring配置文件,內容為:
          ??? <?xml version="1.0" encoding="UTF-8"?>
          ??? <beans
          ??? xmlns="http://www.springframework.org/schema/beans"
          ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
          ??? <bean id="HelloWorld" class="com.pqf.beans.cnHello">
          ??? <property name="msg">
          ??? <value>偉大的祖國!</value>
          ??? </property>
          ??? </bean>
          </beans>

          4、修改測試程序 TestHelloWorldUseSpring ,源代碼:
          package com.pqf.test;
          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.FileSystemXmlApplicationContext;
          import com.pqf.impl.Hello;
          public class TestHelloWorldUseSpring {
          ??? public static void main(String[] args) {
          ??? ??? ApplicationContext actx=new FileSystemXmlApplicationContext("/src/applicationContext.xml ");
          ??? ??? Hello hw=(Hello) actx.getBean("HelloWorld");
          ??? ??? System.out.println(hw.sayHello());
          ??? }
          }
          運行改程序,將在控制臺呼出中文的問候語:您好,偉大的祖國!

          5、改為輸出英文:修改Spring配置文件,內容為:
          ??? <?xml version="1.0" encoding="UTF-8"?>
          ??? <beans
          ??? xmlns="http://www.springframework.org/schema/beans"
          ??? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          ??? xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
          ??? <bean id="HelloWorld" class="com.pqf.beans.enHello">
          ??? <property name="msg">
          ??? <value>My dear friends!</value>
          ??? </property>
          ??? </bean>
          </beans>

          ??? 然后,運行測試程序,應該輸出: Hello My dear friends!

          ??? 可見,程序編寫后,不需要修改,只需改動配置文件,即可實現中英文輸出。


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


          網站導航:
           
          主站蜘蛛池模板: 淮北市| 城固县| 余姚市| 阿拉尔市| 卢氏县| 壤塘县| 花莲县| 柳林县| 北票市| 偏关县| 门头沟区| 股票| 贵德县| 六枝特区| 新邵县| 穆棱市| 宜君县| 杭州市| 云南省| 锦州市| 通城县| 庄浪县| 通化市| 威信县| 梨树县| 抚州市| 远安县| 平江县| 罗江县| 旺苍县| 台北市| SHOW| 溧阳市| 太仆寺旗| 瓦房店市| 临沭县| 涞源县| 区。| 宣恩县| 将乐县| 福海县|