spring之集合属性注入

版权声明: https://blog.csdn.net/qq_24313635/article/details/82108383

1.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<bean id="user" class="com.yj.spring.collection.User">
		<property name="name" value="yj"></property>

		<property name="addressArray">
			<list>
				<!-- <value>Spring</value> -->
				<ref bean="newlandComputer" />
				<ref bean="netWorkStart" />
			</list>
		</property>

		<property name="addressList">
			<list>
				<!-- <value>Spring</value> -->
				<ref bean="newlandComputer" />
				<ref bean="netWorkStart" />
			</list>
		</property>


		<property name="addressSet">
			<set>
				<!-- <value>address</value> -->
				<ref bean="newlandComputer" />
				<ref bean="netWorkStart" />
			</set>
		</property>
		
		<property name="addressMap">
			<map>
				<!-- <entry key="key" value="val"/> -->
				<entry key="newlandComputer" value-ref="newlandComputer" />
				<entry key="netWorkStart" value-ref="netWorkStart" />
			</map>
		</property>

		<property name="addressProp">
			<props>
				<prop key="newlandComputer">NewlandComputer</prop>
				<prop key="netWorkStart">NetWorkStart</prop>
			</props>
		</property>
	</bean>

	<bean id="newlandComputer" class="com.yj.spring.collection.Address">
		<property name="detail" value="NewlandComputer" />
	</bean>

	<bean id="netWorkStart" class="com.yj.spring.collection.Address">
		<property name="detail" value="NetWorkStart" />
	</bean>
</beans>

2.User对象

package com.yj.spring.collection;

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

public class User {
	private String name;
	private Address[] addressArray;
	private List<Address> addressList;
	private Set<Address> addressSet;
	private Map<String,Address> addressMap;
	private Properties addressProp;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Address> getAddressList() {
		return addressList;
	}
	public void setAddressList(List<Address> addressList) {
		this.addressList = addressList;
	}
	public Set<Address> getAddressSet() {
		return addressSet;
	}
	public void setAddressSet(Set<Address> addressSet) {
		this.addressSet = addressSet;
	}
	public Map<String, Address> getAddressMap() {
		return addressMap;
	}
	public void setAddressMap(Map<String, Address> addressMap) {
		this.addressMap = addressMap;
	}
	public Properties getAddressProp() {
		return addressProp;
	}
	public void setAddressProp(Properties addressProp) {
		this.addressProp = addressProp;
	}
	public Address[] getAddressArray() {
		return addressArray;
	}
	public void setAddressArray(Address[] addressArray) {
		this.addressArray = addressArray;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", addressArray=" + Arrays.toString(addressArray) + ", addressList=" + addressList
				+ ", addressSet=" + addressSet + ", addressMap=" + addressMap + ", addressProp=" + addressProp + "]";
	}
}

3.Address对象

package com.yj.spring.collection;

public class Address {
	private String detail;

	public String getDetail() {
		return detail;
	}

	public void setDetail(String detail) {
		this.detail = detail;
	}

	@Override
	public String toString() {
		return "Address [detail=" + detail + "]";
	}
}

4.验证

@Test
public void Test() {
	ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("Collection.xml");
	User user = ctx.getBean(User.class);
	System.out.println(user);
}

5.结果

User [name=yj, addressArray=[Address [detail=NewlandComputer], Address [detail=NetWorkStart]], addressList=[Address [detail=NewlandComputer], Address [detail=NetWorkStart]], addressSet=[Address [detail=NewlandComputer], Address [detail=NetWorkStart]], addressMap={newlandComputer=Address [detail=NewlandComputer], netWorkStart=Address [detail=NetWorkStart]}, addressProp={netWorkStart=NetWorkStart, newlandComputer=NewlandComputer}]

猜你喜欢

转载自blog.csdn.net/qq_24313635/article/details/82108383