<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    mvc 架構(gòu)

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      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
    創(chuàng)] Spring 入門實戰(zhàn)
    2007-07-20 17:50
    一、創(chuàng)建項目
    ??? 創(chuàng)建名為“SpringHelloworld”的Web Project,如下圖:

    二、加入Spring支持??
    ??? 點擊菜單“MyEclipse” -- “Add Spring Capabities"啟動向?qū)В陧椖恐屑尤雽pring的支持。
    ??? 第一步:如下圖:


    ??? 第二步:如下圖:

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

    四、編寫名為HelloWorld的JavaBean
    ??? 在com.pqf.beans 包下面創(chuàng)建 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 包下面創(chuàng)建測試程序,名為: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());
    ??? }
    ??? }

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

    六、設(shè)置Spring配置文件
    ??? 雙擊打開 applicationContext.xml 文件,這個文件是Spring的核心配置文件。
    ??? 編輯修改文件內(nèi)容如下:
    ??? <?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 為標(biāo)識,注入到其他程序中;
    ??? <property> 描述將屬性 msg 的值注入到程序中。

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

    ??? 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"); //反轉(zhuǎn)控制,得到注入的類
    ??? ??? System.out.println(hw.getMsg());??? //使用注入的類
    ??? }
    ??? }


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

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

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

    2、實現(xiàn)接口:在com.pqf.beans下面,創(chuàng)建兩個Hello接口實現(xiàn)類,分別實現(xiàn)中文和英文的Hello接口。
    ??? 中文問候的實現(xiàn):
    ??? 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;
    ??? ??? }
    ??? }


    英文問候的實現(xiàn):
    ??? 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配置文件,內(nèi)容為:
    ??? <?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配置文件,內(nèi)容為:
    ??? <?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>

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

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


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲啪啪免费视频| 亚洲国产精品成人精品无码区 | 亚洲日产无码中文字幕| 免费国产污网站在线观看不要卡| 毛片a级毛片免费观看免下载| 国产亚洲玖玖玖在线观看| 一本无码人妻在中文字幕免费| 亚洲一区电影在线观看| 毛片a级毛片免费播放下载| 亚洲av无码兔费综合| 亚洲A∨精品一区二区三区| 黄视频在线观看免费| 日本红怡院亚洲红怡院最新| a级片免费在线观看| 久久亚洲日韩精品一区二区三区| 99re视频精品全部免费| 亚洲制服丝袜第一页| 四虎在线播放免费永久视频| 伊人久久国产免费观看视频| 亚洲午夜久久久影院伊人 | 一级一级一片免费高清| 久久精品九九亚洲精品天堂| 99久久99久久免费精品小说| 亚洲一卡二卡三卡| 全部免费毛片免费播放| 精品成人免费自拍视频| 99久久婷婷国产综合亚洲| 免费人成视频在线观看视频| 九九美女网站免费| 97se亚洲国产综合自在线| 亚洲国产精品国产自在在线| 久久久久久AV无码免费网站下载| 亚洲一区在线观看视频| 亚洲精品成人久久久| 亚洲精品视频免费看| 亚洲AV无码第一区二区三区| 一级毛片免费毛片毛片| 中文字幕亚洲色图| 免费a在线观看播放| 亚洲黄色免费电影| 无码日韩人妻AV一区免费l |