Spring中的集合的注入方式

上代码  bean.xml的配置

<?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:context="http://www.springframework.org/schema/context"  
            xsi:schemaLocation="http://www.springframework.org/schema/beans  
                http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
                http://www.springframework.org/schema/context  
                http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
    
   	<bean id="collectionBean" class="org.heinrich.CollectionBean">
   		<property name="sets">
   			<set>
   				<value>Heinrich</value>
   				<value>Reading Thinking Java</value>
   			</set>
   		</property>
   		<property name="users">
   			<list>
   				<ref bean="user1"/>
   				<ref bean="user2"/>
   			</list>
   		</property>
   		<property name="maps">
   			<map>
   				<entry key="BookName">
   					<value>Thinking Java</value>
   				</entry>
   				<entry key="PhoneName">
   					<value>Apple 6 plus</value>
   				</entry>
   			</map>
   		</property>
   		<property name="properties">
   			<props>
   				<prop key="1">MySQL</prop>
   				<prop key="2">Oracle</prop>
   				<prop key="3">SQLSERVER</prop>
   			</props>
   		</property>
   	</bean>
    
    <bean id="user1" class="org.heinrich.User">
    	<property name="name">
    		<value>Heinrich</value>
    	</property>
    	<property name="age">
    		<value>21</value>
    	</property>
    </bean>   
    <bean id="user2" class="org.heinrich.User">
    	<property name="name">
    		<value>Zhangyanmei</value>
    	</property>
    	<property name="age">
    		<value>22</value>
    	</property>
    </bean> 
  </beans>
package org.heinrich;

public class User {
	
	private String name;
	private Integer age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}

}

上面是实体类User,下面是需要注入的实体类

package org.heinrich;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;




public class CollectionBean {

	
	private List<User> users;
	private Map<String,String> maps;
	private Set<String> sets;
	private Properties properties;
	public List<User> getUsers() {
		return users;
	}
	public void setUsers(List<User> users) {
		this.users = users;
	}
	public Map<String, String> getMaps() {
		return maps;
	}
	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}
	public Set<String> getSets() {
		return sets;
	}
	public void setSets(Set<String> sets) {
		this.sets = sets;
	}
	public Properties getProperties() {
		return properties;
	}
	public void setProperties(Properties properties) {
		this.properties = properties;
	}
	
}

测试类

package org.heinrich;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanTest {
	
	public static void main(String[] args) {
		
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		User user1 = (User)ctx.getBean("user1");
		User user2 = (User)ctx.getBean("user1");
		System.out.println(user1);
		System.out.println(user2);
		CollectionBean collectionBean = (CollectionBean)ctx.getBean("collectionBean");
		System.out.println(collectionBean);
		System.out.println(collectionBean.getMaps());
		System.out.println(collectionBean.getSets());
		System.out.println(collectionBean.getUsers());
		System.out.println(collectionBean.getProperties());
	
	}

}

测试结果

org.heinrich.User@f8dd88b
org.heinrich.User@f8dd88b
org.heinrich.CollectionBean@298395a7
{BookName=Thinking Java, PhoneName=Apple 6 plus}
[Heinrich, Reading Thinking Java]
[org.heinrich.User@f8dd88b, org.heinrich.User@7dd61c3b]
{3=SQLSERVER, 2=Oracle, 1=MySQL}


转载于:https://my.oschina.net/heinrichchen/blog/599759

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值