可以到spring官网下载,也可以到以下地址下载。
把下载的spring开发包解压缩,可以看到如下图所示目录
在工程中新建一个文件夹lib,用于存放项目所需要的jar包。
打开dist文件夹,复制里面所有的jar包到lib文件夹中,同时拷贝commons-logging-1.1.1.jar包到lib文件夹中。commons-logging-1.1.1.jar可以从struts的lib文件夹中拷贝。
右键项目HelloSpring,单击build pathàconfigure build path,在弹出的java build path对话框中选择libraries—> add library,并在弹出的addlibrary对话框中选择userlibrary,如下图所示
新建Spring 3.0类库,并导入lib文件夹中的所有jar包。配置完成之后spring类库中包含的jar包如下图所示:
package com.bean; public interfacePerson { public void Speak(); }
package com.bean; public classChineseImpl implements Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public void Speak() { System.out.println("I am Chinese,My name is :"+ this.name +",my age is :"+this.age); } }
package com.bean; public classAmericanImpl implements Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public void Speak() { System.out.println("My name is :"+this.name + "my age is :"+this.age); } }
在src目录中新建applicationContext.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="chinese" class="com.bean.ChineseImpl"> <property name="name"> <value>朱伟</value> </property> <property name="age"> <value>27</value> </property> </bean> <bean id="american" class="com.bean.AmericanImpl"> <property name="name"> <value>LeoChu</value> </property> <property name="age"> <value>26</value> </property> </bean> </beans>
在src目录下新建com.spring包,在包中新建类Test,代码为:
package com.spring; importorg.springframework.context.ApplicationContext; importorg.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.Person; public class Test { /** * @param args */ publicstatic void main(String[] args) { /** * 1.启动spring容器 * 2.从spring容器中吧对象取出 * 3.对象调用方法 */ ApplicationContextcontext = newClassPathXmlApplicationContext("applicationContext.xml"); Personperson = (Person) context.getBean("chinese"); person.Speak(); person= (Person) context.getBean("american"); person.Speak(); } }
原文地址:http://blog.csdn.net/longshengguoji/article/details/41878931