Spring之Hello World (轉)
1.下載Spring相關的開發包
下載地址:http://sourceforge.net/project/showfiles.php?group_id=73357
有spring-framework-1.2.6-with-dependencies.zip,一個是spring-framework-1.2.6.zip,最好下載with-dependencies的,里面有另外一些附加包,下載后解壓縮,dist目錄下是spring自身的jar,lib目錄下是附加的jar。
2.新建Java Project,將spring.jar(dist目錄下)和commons-logging.jar(lib目錄下)添加到project的build path中。
3.新建POJO Bean類:HelloBean
//HelloBean.java
/**
?*
?*/
package com.lzy;
/**
?* @author lzy
?*
?*/
public class HelloBean{
?
?private String hello;
?
?public void sayHello(){
??System.out.println(this.getHello());
?}
?/**
? * @return Returns the hello.
? */
?public String getHello() {
??return hello;
?}
?/**
? * @param hello The hello to set.
? */
?public void setHello(String hello) {
??this.hello = hello;
?}
?
}
4.新建文件bean.xml,將在這個XML文件中為一個HelloBean的實例的hello屬性賦值。
//bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<description>Spring Quick Start</description>
<bean id="helloBean" class="com.lzy.HelloBean">
??<property name="hello">
???<value>hello world</value>
??</property>
</bean>
</beans>
5.新建測試類:TestSpring
//TestSpring.java
/**
?*
?*/
package com.lzy;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
?* @author lzy
?*
?*/
public class TestSpring {
?/**
? * @param args
? */
?public static void main(String[] args) {
??// TODO Auto-generated method stub
??
??ApplicationContext ctx=new FileSystemXmlApplicationContext("bean.xml");
????HelloBean hello=(HelloBean)ctx.getBean("helloBean");
??hello.sayHello();
?}
}
6.運行測試類:
??? 如果沒有出錯,輸出中將會看到“hello world”。
posted on 2006-07-21 01:41 liaojiyong 閱讀(338) 評論(0) 編輯 收藏 所屬分類: Spring