<?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-2.5.xsd">
<bean id="personService"
class="nine.spring.service.impl.PersonServiceBean">
<property name="sets">
<set>
<value>我</value>
<value>你</value>
<value>他</value>
</set>
</property>
<property name="lists">
<list>
<value>2</value>
<value>3</value>
<value>4</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">w</prop>
<prop key="key2">e</prop>
<prop key="key3">w</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key1" value="a"/>
<entry key="key1" value="s"/>
<entry key="key3" value="d"/>
</map>
</property>
</bean>
</beans>
package nine.spring.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import nine.spring.service.PersonService;
public class PersonServiceBean implements PersonService {
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>();
省略 get set 方法
package nine.spring.junit;
import java.util.Properties;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import nine.spring.service.PersonService;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring() {
// 在类路径下,寻找配置文件,实例化spring容器。
ApplicationContext ctx = new ClassPathXmlApplicationContext("/nine/ninebeans.xml");
//配置文件中bean的id,推荐首字母小写
PersonService personService = (PersonService) ctx.getBean("personService");
for (String value : personService.getSets()) {
System.out.print(value);
}
for (String value : personService.getLists()) {
System.out.print(value);
}
for (Object value : personService.getProperties().keySet()) {
System.out.print(personService.getProperties().getProperty((String)value));
}
for (String value : personService.getMaps().keySet()) {
System.out.print(personService.getMaps().get((String)value));
}
}
}