配置bean之集合操作

siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springioc
│   │               └── domain
│   │                   └── User.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springioc
    │               └── domain
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

15 directories, 4 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springioc.domain;

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

public class User implements Serializable {

	private static final long serialVersionUID = 1L;
	private List<Integer> list;
	private Set<Integer> set;
	private Map<Integer, String> map;
	private Integer[] array;
	private Properties props;

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

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

	public Set<Integer> getSet() {
		return set;
	}

	public void setSet(Set<Integer> set) {
		this.set = set;
	}

	public Map<Integer, String> getMap() {
		return map;
	}

	public void setMap(Map<Integer, String> map) {
		this.map = map;
	}

	public Integer[] getArray() {
		return array;
	}

	public void setArray(Integer[] array) {
		this.array = array;
	}

	public Properties getProps() {
		return props;
	}

	public void setProps(Properties props) {
		this.props = props;
	}

	@Override
	public String toString() {
		return "User [list=" + list + ", set=" + set + ", map=" + map + ", array=" + Arrays.toString(array) + ", props="
				+ props + "]";
	}

	public User(List<Integer> list, Set<Integer> set, Map<Integer, String> map, Integer[] array, Properties props) {
		super();
		this.list = list;
		this.set = set;
		this.map = map;
		this.array = array;
		this.props = props;
	}

	public User() {
		super();
	}

}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 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.xsd">

	<!-- 集合元素的合并(merge)
		a)集合合并的使用常见一般是在涉及到bean之间的继承时使用的.
		父bean中设置`abstract="true"`属性,子bean中设置`parent=""`属性.
		然后再集合标签中设置属性`merge="true"`属性,即可实现集合属性的继承合并.
		b)仅作了解,不常使用.
		<bean id="parent" abstract="true" class="">
		<property name="">
		<list></list>
		</property>
		</bean>
		<bean id="child" parent="parent" class="">
		<property name="">
		<list merge="true"></list>
		</property>
		</bean>
		以上配置中,child的bean中会继承合并parent的bean中的list属性值.
		c)注意事项
		集合合并的时候,是有限制的,不能合并不同类型的集合.例如合并map和list,这种操作时被禁止的.


		此外,还可以使用util命名空间 -->

	<bean class="ocn.site.springioc.domain.User">
		<property name="list">
			<list value-type="java.lang.Integer" merge="default">
				<value>0</value>
				<value>0</value>
				<value>0</value>
			</list>
		</property>
		<property name="set">
			<set value-type="java.lang.Integer" merge="default">
				<value>0</value>
				<value>1</value>
				<value>2</value>
			</set>
		</property>
		<property name="map">
			<map key-type="java.lang.Integer" value-type="java.lang.String" merge="default">
				<entry key="0" value="jack"></entry>
				<entry key="1" value="rose"></entry>
				<entry key="2" value="hack"></entry>
			</map>
		</property>
		<property name="array">
			<array value-type="java.lang.Integer" merge="default">
				<value>3</value>
				<value>3</value>
				<value>3</value>
			</array>
		</property>
		<property name="props">
			<props value-type="java.lang.String" merge="default">
				<prop key="0">aaa</prop>
				<prop key="1">bbb</prop>
				<prop key="2">ccc</prop>
			</props>
		</property>
	</bean>

</beans>
package ocn.site.springioc.domain;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired User user;

	@Test
	public void run() throws Exception {
		logger.info(user.getList());
		logger.info(user.getSet());
		logger.info(user.getMap());
		logger.info(user.getProps());
		logger.info(Arrays.toString(user.getArray()));
	}

}
19-09-08 15:43:36 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-08 15:43:36 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4e04a765, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@783e6358, org.springframework.test.context.support.DirtiesContextTestExecutionListener@17550481]
19-09-08 15:43:36 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 15:43:37 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 15:43:37 CST 2019]; root of context hierarchy
19-09-08 15:43:37 ocn.site.springioc.domain.Runtest  =====>>> [0, 0, 0]
19-09-08 15:43:37 ocn.site.springioc.domain.Runtest  =====>>> [0, 1, 2]
19-09-08 15:43:37 ocn.site.springioc.domain.Runtest  =====>>> {0=jack, 1=rose, 2=hack}
19-09-08 15:43:37 ocn.site.springioc.domain.Runtest  =====>>> {2=ccc, 1=bbb, 0=aaa}
19-09-08 15:43:37 ocn.site.springioc.domain.Runtest  =====>>> [3, 3, 3]
19-09-08 15:43:37 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 15:43:37 CST 2019]; root of context hierarchy
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值