applicationContext
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="test" class="com.test.Test">
<property name="properties">
<props>
<prop key="1">small dog</prop>
<prop key="2">old dog</prop>
<prop key="3">super dog</prop>
</props>
</property>
</bean>
<bean id="run" class="com.run.TestRun">
<property name="test_run" ref="test"/>
</bean>
</beans>
Test
package com.test;
import java.util.Iterator;
import java.util.Properties;
public class Test {
/*声明一个属性类对象,Properties 类表示了一个持久的属性集
Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。 */
Properties properties;
//输出属性类对象的priperties内容
public void print_prop(){
//Iterator是对collection 进行迭代的迭代器
Iterator iterator = properties.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = properties.get(key);
System.out.println(key+"__"+value);
}
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
TestRun
package com.run;
import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.Test;
public class TestRun {
Test test_run;
public Test getTest_run() {
return test_run;
}
public void setTest_run(Test test_run) {
this.test_run = test_run;
}
public static void main(String[] args) {
//取得应用程序上下文接口
ApplicationContext apl = new ClassPathXmlApplicationContext("applicationContext.xml");
//在IOC容器中取出指定Bean对象
TestRun run = (TestRun) apl.getBean("run");
//运行Bean中的方法
run.getTest_run().print_prop();
}
}