Spring集合注入

Spring可以对集合类型进行注入包括:Set集合,properties属性集合,Map集合以及List集合

注入方式如下:

Java代码 复制代码  收藏代码
  1. package com.test;   
  2.   
  3. import java.util.ArrayList;   
  4. import java.util.HashMap;   
  5. import java.util.HashSet;   
  6. import java.util.Map;   
  7. import java.util.Properties;   
  8. import java.util.Set;   
  9. import java.util.List;   
  10.   
  11. public class UserServiceImplement implements IUserService {   
  12.   
  13.     public Set<String> getS() {   
  14.         return s;   
  15.     }   
  16.   
  17.     public void setS(Set<String> s) {   
  18.         this.s = s;   
  19.     }   
  20.   
  21.     public Map<String, String> getM() {   
  22.         return m;   
  23.     }   
  24.   
  25.     public void setM(Map<String, String> m) {   
  26.         this.m = m;   
  27.     }   
  28.   
  29.     public Properties getP() {   
  30.         return p;   
  31.     }   
  32.   
  33.     public void setP(Properties p) {   
  34.         this.p = p;   
  35.     }   
  36.   
  37.     public List<String> getL() {   
  38.         return l;   
  39.     }   
  40.   
  41.     public void setL(List<String> l) {   
  42.         this.l = l;   
  43.     }   
  44.   
  45.     private Set<String> s = new HashSet<String>();   
  46.     private Map<String, String> m = new HashMap<String, String>();   
  47.     private Properties p = new Properties();   
  48.     private List<String> l = new ArrayList<String>();   
  49.   
  50.     public void saveUser() {   
  51.         System.out.println("Set集合注入");   
  52.         for (String str : s) {   
  53.             System.out.println(str);   
  54.         }   
  55.   
  56.         System.out.println("------------------------------");   
  57.         System.out.println("Map集合注入");   
  58.         for (String str : m.values()) {   
  59.             System.out.println(str);   
  60.         }   
  61.   
  62.         System.out.println("------------------------------");   
  63.         System.out.println("Properties属性集合注入");   
  64.         for (Object str : p.values()) {   
  65.             System.out.println(str);   
  66.         }   
  67.   
  68.         System.out.println("------------------------------");   
  69.         System.out.println("List集合注入");   
  70.         for (String str : l) {   
  71.             System.out.println(str);   
  72.         }   
  73.     }   
  74. }  
package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.List;

public class UserServiceImplement implements IUserService {

	public Set<String> getS() {
		return s;
	}

	public void setS(Set<String> s) {
		this.s = s;
	}

	public Map<String, String> getM() {
		return m;
	}

	public void setM(Map<String, String> m) {
		this.m = m;
	}

	public Properties getP() {
		return p;
	}

	public void setP(Properties p) {
		this.p = p;
	}

	public List<String> getL() {
		return l;
	}

	public void setL(List<String> l) {
		this.l = l;
	}

	private Set<String> s = new HashSet<String>();
	private Map<String, String> m = new HashMap<String, String>();
	private Properties p = new Properties();
	private List<String> l = new ArrayList<String>();

	public void saveUser() {
		System.out.println("Set集合注入");
		for (String str : s) {
			System.out.println(str);
		}

		System.out.println("------------------------------");
		System.out.println("Map集合注入");
		for (String str : m.values()) {
			System.out.println(str);
		}

		System.out.println("------------------------------");
		System.out.println("Properties属性集合注入");
		for (Object str : p.values()) {
			System.out.println(str);
		}

		System.out.println("------------------------------");
		System.out.println("List集合注入");
		for (String str : l) {
			System.out.println(str);
		}
	}
}

 要注意的是:这些集合属性也必须要有对应的setter方法

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans    
  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  6.            http://www.springframework.org/schema/context   
  7.            http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  8.     <context:annotation-config />  
  9.   
  10.     <bean id="userservice" class="com.test.UserServiceImplement">  
  11.         <property name="s">  
  12.             <set>  
  13.                 <value>SetValue1</value>  
  14.                 <value>SetValue2</value>  
  15.                 <value>SetValue3</value>  
  16.             </set>  
  17.         </property>  
  18.   
  19.         <property name="m">  
  20.             <map>  
  21.                 <entry key="MapKey1" value="MapValue1"></entry>  
  22.                 <entry key="MapKey2" value="MapValue2"></entry>  
  23.                 <entry key="MapKey3" value="MapValue3"></entry>  
  24.             </map>  
  25.         </property>  
  26.   
  27.         <property name="p">  
  28.             <props>  
  29.                 <prop key="PropertiesKey1">PropertiesValue1</prop>  
  30.                 <prop key="PropertiesKey2">PropertiesValue2</prop>  
  31.                 <prop key="PropertiesKey3">PropertiesValue3</prop>  
  32.             </props>  
  33.         </property>  
  34.   
  35.         <property name="l">  
  36.             <list>  
  37.                 <value>ListValue1</value>  
  38.                 <value>ListValue2</value>  
  39.                 <value>ListValue3</value>  
  40.             </list>  
  41.         </property>  
  42.     </bean>  
  43. </beans>  

 测试类:

Java代码 复制代码  收藏代码
  1. package com.test;   
  2.   
  3. import org.springframework.context.ApplicationContext;   
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  5.   
  6. public class Test {   
  7.   
  8.     public static void main(String[] args) {   
  9.         ApplicationContext ctx = new ClassPathXmlApplicationContext(   
  10.                 "com/test/bean.xml");   
  11.         IUserService us = (IUserService) ctx.getBean("userservice");   
  12.         us.saveUser();   
  13.     }   
  14. }  
package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(
				"com/test/bean.xml");
		IUserService us = (IUserService) ctx.getBean("userservice");
		us.saveUser();
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值