计划用一个月时间来学习Spring,在这里把自己的学习过程记录下来,方便想学习Spring的人,也为自己日后复习有个参考。以下通过一个简单的例子来先了解下Spring的用法。
(1)创建一个java工程,建立如下类:HelloBean
package com.ducklyl;
public class HelloBean {
 private String helloWord;
 public String getHelloWord() {
  return helloWord;
 }
 public void setHelloWord(String helloWord) {
  this.helloWord = helloWord;
 }
}
(2)创建Spring配置文件:beans-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helloBean" class="com.ducklyl.HelloBean">
 <property name="helloWord">
    <value>Hello,ducklyl!</value>
 </property>
</bean>
</beans>
(3)导入Spring所需的包:commons-logging.jar,spring.jar 。(日志包和Spring包)
包下载地址:
http://www.ziddu.com/download/3555993/Spring.rar.html
(4)创建测试类:SpringDemo.java
package com.ducklyl;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.*;
public class SpringDemo{
 public static void main(String[] args)
 {
  //读取配置文件
  Resource rs=new FileSystemResource("beans-config.xml");
  //实例化Bean工厂
  BeanFactory factory=new XmlBeanFactory(rs);
  
  //获取id="helloBean"对象
  HelloBean hello=(HelloBean)factory.getBean("helloBean");
  //调用helloBean对象getHelloWord()方法
  System.out.println(hello.getHelloWord());
 }
}
如果以上配置正确的话,运行SpringDemo.java,可以看到输出结果:Hello,ducklyl!