记录使用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。
<?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>
图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 hierarchyGoodbye!
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 hierarchyHello!
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();
}
}