- 集合注入的第一种方式可以参考spring依赖注入的案例2
- 使用util实现集合注入的案例:
- 实体类:
- public class TeacherBean {
- private List<String> list;
- private Set<String> set;
- private Map<String,String> map;
- private Properties prop;
- public Properties getProp() {
- return prop;
- }
- public void setProp(Properties prop) {
- this.prop = prop;
- }
- public void setList(List<String> list) {
- this.list = list;
- }
- public void setSet(Set<String> set) {
- this.set = set;
- }
- public void setMap(Map<String, String> map) {
- this.map = map;
- }
- //生成get方法用于得到集合
- public List<String> getList() {
- return list;
- }
- public Set<String> getSet() {
- return set;
- }
- public Map<String, String> getMap() {
- return map;
- }
- }
- beanTeacher1.xml文件
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:util="http://www.springframework.org/schema/util"
- 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
- http://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd">
- <!-- 首先如果要使用一个util
- xmlns:util="http://www.springframework.org/schema/util"
- 然后引用
- xsi:schemaLocation=" http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-2.5.xsd"
- 然后使用的时候以util为前缀
- -->
- <bean id="teacherBean" class="cn.csdn.hr.service.TeacherBean">
- <property name="list">
- <util:list>
- <value>java编程</value>
- </util:list>
- </property>
- <property name="set">
- <util:set>
- <value>java开发</value>
- </util:set>
- </property>
- <property name="map">
- <util:map>
- <entry key="0xxx00001" value="java编程开发"></entry>
- </util:map>
- </property>
- <property name="prop">
- <util:properties>
- <prop key="000000xx001">
- java
- </prop>
- </util:properties>
- </property>
- </bean>
- </beans>
- 测试方法
- @Test
- public void test1() {
- //第一步:获取应用程序上下文对象
- ApplicationContext ac=new ClassPathXmlApplicationContext("classpath:beanTeacher1.xml");
- //第二步:根据应用程序上下文对象的getBean(id名称)方法获取实例bean对象
- TeacherBean t=(TeacherBean) ac.getBean("teacherBean");
- System.out.println("set注入list-----------------------------------------------------");
- List<String> list=t.getList();
- for(String str:list){
- System.out.println(str);
- }
- System.out.println("set注入set-----------------------------------------------------");
- Set<String> set=t.getSet();
- for(String str:set){
- System.out.println(str);
- }
- System.out.println("set注入map----------------------------------------------------");
- Map<String,String> map=t.getMap();
- Set<Entry<String,String>> en=map.entrySet();
- for(Entry ent:en){
- System.out.println(ent.getKey()+"===================="+ent.getValue());
- }
- System.out.println("set注入prop---------------------------------------------------");
- Properties prop=t.getProp();
- Set<Entry<Object, Object>> en1=prop.entrySet();
- for(Entry ent:en1){
- System.out.println(ent.getKey()+"===================="+ent.getValue());
- }
- }