spring 装配集合

1:创建pojo,属性包含集合,集合元素为基本类型

  

package com.liyafei.pojo;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class ComplexAssemble { private Long id;
private List<String> list;
private Map<String,String> map;
private Properties properties;
private Set<String> set;
private String[] array;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public String[] getArray() {
return array;
}
public void setArray(String[] array) {
this.array = array;
}
}
package com.liyafei.pojo; import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class ComplexAssemble { private Long id;
private List<String> list;
private Map<String,String> map;
private Properties properties;
private Set<String> set;
private String[] array; setter and getter
}

2:装配bean

  

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <bean id="complexAssembly" class="com.liyafei.pojo.ComplexAssemble">
<property name="id" value="1" />
<property name="list">
<list>
<value>value-list-1</value>
<value>value-list-2</value>
<value>value-list-3</value>
</list>
</property>
<property name="map">
<map>
<entry key="key1" value="value-key-1"/>
<entry key="key2" value="value-key-2"/>
<entry key="key3" value="value-key-3"/>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">value-prop-1</prop>
<prop key="prop2">value-prop-2</prop>
<prop key="prop3">value-prop-3</prop>
</props>
</property>
<property name="set">
<set>
<value>value-set-1</value>
<value>value-set-2</value>
<value>value-set-3</value>
</set>
</property>
<property name="array">
<array>
<value>value-array-1</value>
<value>value-array-2</value>
<value>value-array-3</value>
</array>
</property>
</bean>
</beans>

3:创建pojo,属性包含集合,集合元素为对象类型

  

public class Role {

    private Long id;
private String roleName;
private String note;
public Role(){ }
public Role(Long id, String roleName, String note) {
this.id = id;
this.roleName = roleName;
this.note = note;
}
setter and getter
}
public class User {

    private Long id;
private String userName;
setter and getter
}

装配类:

public class UserRoleAssembly {

    private Long id;
private List<Role> list;
private Map<Role, User> map;
private Set<Role> set;
setter and getter
}

4:装备bean

  

  <bean id="role1" class="com.liyafei.pojo.Role">
<property name="id" value="1"></property>
<property name="roleName" value="role_name_1"></property>
<property name="note" value="role_note_1"></property>
</bean>
<bean id="role2" class="com.liyafei.pojo.Role">
<property name="id" value="2"></property>
<property name="roleName" value="role_name_2"></property>
<property name="note" value="role_note_2"></property>
</bean>
<bean id="user1" class="com.liyafei.pojo.User">
<property name="id" value="1"></property>
<property name="userName" value="user_name_1"></property>
<property name="note" value="role_note_1"></property>
</bean>
<bean id="user2" class="com.liyafei.pojo.User">
<property name="id" value="2"></property>
<property name="userName" value="user_name_2"></property>
<property name="note" value="role_note_2"></property>
</bean> <bean id="userRoleAssembly" class="com.liyafei.pojo.UserRoleAssemply">
<property name="id" value="1"></property>
<property name="list">
<list>
<ref bean="role1"/>
<ref bean="role2"/>
</list>
</property>
<property name="map">
<map>
<entry key-ref="role1" value-ref="user1"></entry>
<entry key-ref="role2" value-ref="user2"></entry>
</map>
</property>
<property name="set">
<set>
<ref bean="role1"/>
<ref bean="role2"/>
</set>
</property>
</bean>
上一篇:dubbo调用服务时,利用zookeeper实现本地动态负载均衡


下一篇:命令行编译vs2013项目