Sunday, August 26, 2007

[原创] Spring 入门实战_享受 Java 与清风

[原创] Spring 入门实战_享受 Java 与清风


一、创建项目

    创建名为“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!



    可见,程序编写后,不需要修改,只需改动配置文件,即可实现中英文输出。



    (完)


No comments: