1. 导入Spring基础包
- 参照上图,导入下列基础包:
- spring-beans-4.3.9.RELEASE.jar
- spring-core-4.3.9.RELEASE.jar
- spring-context-4.3.9.RELEASE.jar
- spring-expression-4.3.9.RELEASE.jar
- com.springsource.org.apache.commons.logging-1.1.1.jar(log包)
2. 创建对象
- 在src目录下创建包和对象,详细如下代码:
package com.ccblogs.spring; public class ComboList { private String key; private String value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }
3. 配置注册对象到容器
- 在src下创建xml文件,命名为appicationContext.xml(名字可以任意),详细如下图:
- 导入约束,详细如下图:
4. 创建测试对象
- 在src下创建包和测试对象,详细如下代码:
测试结果package com.ccblogs.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.ccblogs.spring.ComboList; public class ComboListTest { @SuppressWarnings("resource") @Test public static void main(String[] args) { ApplicationContext cont = new ClassPathXmlApplicationContext("applicationContext.xml"); ComboList cmbLst = (ComboList)cont.getBean("ComboList"); cmbLst.setKey("key_0"); cmbLst.setValue("value_0"); System.out.println(String.format("%s, %s", cmbLst.getKey(), cmbLst.getValue())); } }
- 详细如下图:
- 详细如下图: