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。
<?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(); } }
- Spring学习笔记 Hello World (code)
- Spring学习笔记 Hello World
- Spring 学习笔记一 hello world
- Spring Data学习笔记-Hello world
- Spring学习笔记-1:SpringMVC hello world
- Spring-Boot学习笔记一:Hello World
- Spring学习笔记-1:SpringMVC hello world
- Hello World学习笔记
- spring学习笔记——spring Hello world Web版
- spring boot 学习笔记(002) Hello world
- Spring Boot学习笔记-从Hello World开始 (一)
- [C学习笔记].Hello World
- Jquery学习笔记:Hello World
- xlib学习笔记-hello world
- DWR学习笔记 - Hello World
- 【ruby学习笔记】Hello World
- WebService学习笔记-Hello world
- JQuery学习笔记-Hello world
- OGNL
- 强类型DataSet的基本知识
- 银行家算法
- Goto 2.12.06.03发布
- 睁开双眼玩立体游戏!3D显示技术解析
- Spring学习笔记 Hello World (code)
- 3D立体游戏的起源和目前的发展
- 424 - Integer Inquiry
- ASP.NET MVC DropDownList扩展,实现foreach@Html.DropDownListFor(p=>p.Type,p.Type,TypeItem)
- 旋转:矩阵,四元数和欧拉角向量
- Hash表读书笔记
- 20120603命令行下线程调用代码----OK
- AS3 localToGlobal、globalToLocal方法的总结
- 程序员的7个坏习惯