给对象属性注入 Java Collection 类型 List、Set、Map 和 Properties,应该怎么做呢。为了处理这种情况,Spring 提供了四种类型的集合的配置元素,如下所示:
| 元素 | 描述 |
|---|---|
| List | 序列类型,有顺序可重复 |
| Set | 集合类型,无顺序不可重复 |
| Map | 名称-值对的集合,其中名称和值可以是任何类型。 |
| Properties | 名称-值对的集合,其中名称和值都是字符串类型。 |
实例
1.编写Person类实体类
package com.entity;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class Person {
private String name;
private String gender;
private int age;
@Resource(name="cars")
private List<Car> cars;
@Resource
private Set<String> address;
@Resource
private Map account;
@Resource
private Properties phoneZone;
public Person() {
}
public Person(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
//bean的初始化方法
@PostConstruct
public void initPerson(){
System.out.println("initPerson()...");
}
//bean的销毁方法
@PreDestroy
public void destroyPerson(){
System.out.println("destroyPerson()...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
public Set<String> getAddress() {
return address;
}
public void setAddress(Set<String> address) {
this.address = address;
}
public Map getAccount() {
return account;
}
public void setAccount(Map account) {
this.account = account;
}
public Properties getPhoneZone() {
return phoneZone;
}
public void setPhoneZone(Properties phoneZone) {
this.phoneZone = phoneZone;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", gender='" + gender + '\'' +
", age=" + age +
", cars=" + cars +
", address=" + address +
", account=" + account +
", phoneZone=" + phoneZone +
'}';
}
}
2.在SpringConfiguration中注入集合对象
package com.config;
import com.entity.Car;
import com.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import java.util.*;
@Configuration
@ComponentScan(basePackages = "com")
public class SpringConfiguration {
@Bean(name = {"p1","person"})
@Scope("singleton" ) // 等同于@Scope,默认是singleton
public Person getPerson(){
return new Person("zhangsan","男",20);
}
@Bean("cars")
public List<Car> getCars(){
List<Car> cars = new ArrayList<Car>();
cars.add(new Car("BMW","black"));
cars.add(new Car("BENZ","red"));
cars.add(new Car("TOYOTA","white"));
return cars;
}
@Bean("address")
public Set<String> getAddress(){
Set address = new HashSet();
address.add("西安市大学东路");
address.add("西安市雁塔路");
address.add("西安市咸宁西路");
return address;
}
@Bean("account")
public Map getAccount(){
Map account = new HashMap();
account.put("10000000","建设银行");
account.put("10000001","工商银行");
account.put("10000002","农业银行");
account.put("10000003","中国银行");
return account;
}
@Bean("phoneZone")
public Properties getPoneZone(){
Properties phoneZone = new Properties();
phoneZone.put("010","北京");
phoneZone.put("020","广州");
phoneZone.put("021","上海");
return phoneZone;
}
}
3.测试运行
package com.test;
import com.config.SpringConfiguration;
import com.entity.Person;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class SpringDemo {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
Person person = (Person) context.getBean("p1");
System.out.println(person);
}
}
运行结果
initPerson()...
Person{name='zhangsan', gender='男', age=20, cars=[Car{brand='BMW', color='black'}, Car{brand='BENZ', color='red'}, Car{brand='TOYOTA', color='white'}], address=[西安市大学东路, 西安市雁塔路, 西安市咸宁西路], account={10000000=建设银行, 10000001=工商银行, 10000002=农业银行, 10000003=中国银行}, phoneZone={010=北京, 021=上海, 020=广州}}
如果使用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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.entity.Car" id="car1">
<property name="brand" value="BMW"/>
<property name="color" value="black"/>
</bean>
<bean class="com.entity.Car" id="car2">
<property name="brand" value="BENZ"/>
<property name="color" value="red"/>
</bean>
<bean class="com.entity.Car" id="car3">
<property name="brand" value="TOYOTA"/>
<property name="color" value="white"/>
</bean>
<bean class="com.entity.Person" name="p1,person" scope="prototype">
<property name="name" value="zhangsan"/>
<property name="gender" value="男"/>
<property name="age" value="20"/>
<property name="cars">
<list>
<ref bean="car1"></ref>
<ref bean="car2"></ref>
<ref bean="car3"></ref>
</list>
</property>
<property name="address">
<set>
<value>西安市大学东路</value>
<value>西安市雁塔路</value>
<value>西安市咸宁西路</value>
</set>
</property>
<property name="account">
<map>
<entry key="10000000" value="建设银行"/>
<entry key="10000001" value="工商银行"/>
<entry key="10000002" value="农业银行"/>
<entry key="10000003" value="中国银行"/>
</map>
</property>
<property name="phoneZone">
<props>
<prop key="010">北京</prop>
<prop key="020">广州</prop>
<prop key="021">上海</prop>
</props>
</property>
</bean>
</beans>