Spring、基本类型属性和集合类型属性的注入

Spring 还可以对基本属性和集合类型属性进行注入:

public interface PersonIService {
	public String getBaseProperty();
	public Set<String> getSets();
	public List<Integer> getList();
	public Properties getProperties();
	public Map<String, String> getMaps();
}


public class PersonServiceImpl implements PersonIService {
	private String baseProperty;
	private Set<String> sets;
	private List<Integer> list;
	private Properties properties;
	private Map<String,String> maps;
	
	
	public Map<String, String> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}

	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public List<Integer> getList() {
		return list;
	}

	public void setList(List<Integer> list) {
		this.list = list;
	}

	public Set<String> getSets() {
		return sets;
	}
	
	public String getBaseProperty() {
		return baseProperty;
	}

	public void setBaseProperty(String baseProperty) {
		this.baseProperty = baseProperty;
	}

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}
}

beans.xml:

<bean id="personIService" class="cn.server.impl.PersonServiceImpl">
		<property name="baseProperty" value="value:基本属性1" />
		<property name="sets">
			<set>
				<value>set装配-value1</value>
				<value>set装配-value2</value>
				<value>set装配-value3</value>
			</set>
		</property>
		<property name="list">
			<list>
				<value>11</value>
				<value>12</value>
				<value>13</value>
			</list>
		</property>
		<property name="properties">
			<props>
				<prop key="property1">value1</prop>
				<prop key="property2">value2</prop>
				<prop key="property3">value3</prop>
			</props>
		</property>
		<property name="maps">
			<map>
				<entry key="map1" value="value1" />
				<entry key="map2" value="value2" />
				<entry key="map3" value="value3" />
			</map>
		</property>
	</bean>

测试代码:

@Test
	public void otherTest(){
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		PersonIService personIService=(PersonIService)ac.getBean("personIService");
		System.out.println("========基本属性注入============");
		System.out.println(personIService.getBaseProperty());
		System.out.println("========Set集合类型注入============");
		for(String str : personIService.getSets()){
			System.out.println(str);
		}
		System.out.println("========list集合类型注入============");
		for(Integer i : personIService.getList()){
			System.out.println(i);
		}
		System.out.println("========properties集合类型注入============");
		for(Object key : personIService.getProperties().keySet()){
			System.out.println(key+"="+personIService.getProperties().getProperty(key.toString()));
		}
		System.out.println("========map集合类型注入============");
		for(Object key : personIService.getMaps().keySet()){
			System.out.println(key+"="+personIService.getMaps().get(key));
		}
	}



转载于:https://www.cnblogs.com/raphael5200/p/5114740.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值