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”。 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值