Spring学习笔记 Hello World (code)

来源:互联网 发布:知乎首页进不去 编辑:程序博客网 时间:2024/06/10 09:40


记录使用Spring3实现一个完整的Hello World示例的过程。
工具使用Eclipse indigo JavaEE IDE。

Srping2. X时使用Spring只需要引入一个JAR包即可,比如Spring-2.5.6.jar,从Spring3开始Spring framework提供了一系列JAR包,使用的时候要根据工程需要引入相关JAR包。并且一些Spring 的JAR包还会依赖与Spring framework以外的JAR包。可能来自Apache, Oracle, Coucho,比如即使这个最简单的Hello World程序也要依赖于Apache的commons-logging-1.x.jar。

    图1 Spring3 包含的所有Jar包
 
Hello World 示例只需要这些JAR包中的数个,以及io-common-1.x.jar,需要注意Spring3需要使用JDK1.5+
Demo的工程结构如图:
图2 Demo工程结构
一、使用Eclipse建立一个普通的JAVA工程,这里命名为spring_learn
二、配置ClassPath(也可以在建立JAVA工程时进行配置),工程上单击鼠标右键->Build Path->Configure Build Path,在弹出窗口中将图2中所示所需JAR包加入到Libraries中。
 
图3 引入工程所需JAR包
 
三、在源文件夹src下建立包demo,并在demo包中新建Srping配置文件。(这个Demo中使用传统的XML配置文件的方式来配置Spring中的Bean以及Bean的关系)
文件名:spring-config.xml
code:
<?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-3.0.xsd"></beans>
其实这个文件放源文件夹的任何包路径下都可以,方便分类维护即可,在程序使用这个文件时只要使用这个文件的完整包路径即可。
 
四、实现Demo代码,以及修改配置文件,配置MessageLooper类对应的Bean

 

图4 代码实现

以上代码运行结果:

输出

2012-6-3 13:46:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a01335: startup date [Sun Jun 03 13:46:51 CST 2012]; root of context hierarchy2012-6-3 13:46:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [demo/spring-config.xml]2012-6-3 13:46:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b09468: defining beans [messageLooper,messager]; root of factory hierarchy
Goodbye!
Goodbye!
Goodbye!

将Spring-config.xml中<bean id="messager" class="demo.GoodbyeMessagerImpl" /> 
更改为 <bean id="messager" class="demo.HelloMessagerImpl" />

运行,输出:

2012-6-3 13:46:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a01335: startup date [Sun Jun 03 13:46:51 CST 2012]; root of context hierarchy2012-6-3 13:46:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [demo/spring-config.xml]2012-6-3 13:46:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b09468: defining beans [messageLooper,messager]; root of factory hierarchy
Hello!
Hello!
Hello!

Spring-config.xml<property name="numTimes" value="3" /> 
更改为 <property name="numTimes"value="4" />
运行输出:
2012-6-3 13:46:51 org.springframework.context.support.AbstractApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@a01335: startup date [Sun Jun 03 13:46:51 CST 2012]; root of context hierarchy2012-6-3 13:46:51 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [demo/spring-config.xml]2012-6-3 13:46:52 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b09468: defining beans [messageLooper,messager]; root of factory hierarchy
Hello!
Hello!
Hello!
Hello!

 

代码:

spring-config.xml

<?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-3.0.xsd"><bean id="messageLooper" class="demo.MessageLooper"><property name="messager" ref="messager" /><property name="numTimes" value="3" /></bean><bean id="messager" class="demo.GoodbyeMessagerImpl" /></beans>


Message.java

package demo;public interface Messager{    void printMessage();}


GoodbyeMessagerImpl.java
 

package demo;public class GoodbyeMessagerImpl implements Messager{    @Override    public void printMessage()    {        System.out.println("Goodbye!");    }}


HelloMessagerImpl.java

package demo;public class HelloMessagerImpl implements Messager{    @Override    public void printMessage()    {        System.out.println("Hello!");    }}


MessageLooper.java

package demo;public class MessageLooper{    private Messager messager;    public Messager getMessager()    {        return messager;    }    // 实际上Spring是在创建对象时通过类成员的set方法将配置文件中定义的实现类注入到MessageLooper中的     public void setMessager(Messager messager)     {        this.messager = messager;    }        private int numTimes;    public int getNumTimes()    {        return numTimes;    }    // 在创建对象时通过numTimes的set方法将配置文件中定义的数值注入到MessageLooper中的    public void setNumTimes(int numTimes)    {        this.numTimes = numTimes;    }    public void doIt()    {        int n = getNumTimes();        for (int i = 1; i <= n; i++)        {            messager.printMessage();        }    }}

HelloWorldMain.java

package demo;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class HelloWorldMain{        public static void main(String[] args)    {        String springConfig = "demo/spring-config.xml";        ApplicationContext spring =                 new ClassPathXmlApplicationContext(springConfig);        MessageLooper messageLooper =                spring.getBean("messageLooper", MessageLooper.class);        messageLooper.doIt();    }    }