Spring学习--集合属性

集合属性

Spring 中可以通过一组内置的 xml 标签(例如: <list> , <set> 或 <map>) 来配置集合属性。

配置java.util.Set 需要使用 <set> 标签 , 定义元素的方法与 List 一样。

下面我们就以 List 和 Map 为例:

配置List属性

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="person" class="com.itdoc.spring.beans.Person">
 7         <property name="name" value="华崽儿"/>
 8         <property name="sex" value="女"/>
 9         <property name="age" value="27"/>
10         <property name="cars">
11             <list>
12                 <ref bean="car1"/>
13                 <ref bean="car2"/>   <!--可引入 Bean , 也可用内部 Bean-->
14                 <bean id="car3" class="com.itdoc.spring.beans.Car">
15                     <property name="brand" value="Ferrari"/>
16                     <property name="price" value="22500000"/>
17                     <property name="maxSpeed" value="330"/>
18                 </bean>
19             </list>
20         </property>
21     </bean>
22 
23     <bean id="car1" class="com.itdoc.spring.beans.Car">
24         <property name="brand" value="Lamborghini"/>
25         <property name="price" value="27000000"/>
26         <property name="maxSpeed" value="300"/>
27     </bean>
28     <bean id="car2" class="com.itdoc.spring.beans.Car">
29         <property name="brand" value="Rolls-Royce"/>
30         <property name="price" value="24500000"/>
31         <property name="maxSpeed" value="310"/>
32     </bean>
33 
34 </beans>
 1 package com.itdoc.spring.beans;
 2 
 3 /**
 4  * 集合中的对象
 5  * http://www.cnblogs.com/goodcheap
 6  *
 7  * @author: Wáng Chéng Dá
 8  * @create: 2017-02-28 19:56
 9  */
10 public class Car {
11 
12     private String brand;
13 
14     private double price;
15 
16     private int maxSpeed;
17 
18     public String getBrand() {
19         return brand;
20     }
21 
22     public void setBrand(String brand) {
23         this.brand = brand;
24     }
25 
26     public double getPrice() {
27         return price;
28     }
29 
30     public void setPrice(double price) {
31         this.price = price;
32     }
33 
34     public int getMaxSpeed() {
35         return maxSpeed;
36     }
37 
38     public void setMaxSpeed(int maxSpeed) {
39         this.maxSpeed = maxSpeed;
40     }
41 
42     @Override
43     public String toString() {
44         return "Car{" +
45                 "brand='" + brand + '\'' +
46                 ", price=" + price +
47                 ", maxSpeed=" + maxSpeed +
48                 '}';
49     }
50 }
 1 package com.itdoc.spring.beans;
 2 
 3 import java.util.List;
 4 
 5 /**
 6  * 集合属性注入
 7  * http://www.cnblogs.com/goodcheap
 8  *
 9  * @author: Wáng Chéng Dá
10  * @create: 2017-02-28 19:58
11  */
12 public class Person {
13 
14     private String name;
15 
16     private String sex;
17 
18     private int age;
19 
20     private List<Car> cars;
21 
22     public String getName() {
23         return name;
24     }
25 
26     public void setName(String name) {
27         this.name = name;
28     }
29 
30     public String getSex() {
31         return sex;
32     }
33 
34     public void setSex(String sex) {
35         this.sex = sex;
36     }
37 
38     public int getAge() {
39         return age;
40     }
41 
42     public void setAge(int age) {
43         this.age = age;
44     }
45 
46     public List<Car> getCars() {
47         return cars;
48     }
49 
50     public void setCars(List<Car> cars) {
51         this.cars = cars;
52     }
53 
54     @Override
55     public String toString() {
56         return "Person{" +
57                 "name='" + name + '\'' +
58                 ", sex='" + sex + '\'' +
59                 ", age=" + age +
60                 ", cars=" + cars +
61                 '}';
62     }
63 }
 1 package com.itdoc.spring.beans;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * http://www.cnblogs.com/goodcheap
 8  *
 9  * @author: Wáng Chéng Dá
10  * @create: 2017-02-28 20:12
11  */
12 public class Main {
13 
14     public static void main(String[] args) {
15 
16         ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
17         Person person = (Person) app.getBean("person");
18         System.out.println(person);
19 
20     }
21 }

 

控制台输出:

Person{name='华崽儿', sex='女', age=27, cars=[Car{brand='Lamborghini', price=2.7E7, maxSpeed=300}, Car{brand='Rolls-Royce', price=2.45E7, maxSpeed=310}, Car{brand='Ferrari', price=2.25E7, maxSpeed=330}]}

 

配置Map属性

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="person" class="com.itdoc.spring.map.Person">
 7         <property name="name" value="华崽儿"/>
 8         <property name="sex" value="女"/>
 9         <property name="age" value="28"/>
10         <property name="cars">
11             <map>
12                 <entry key="Lamborghini" value-ref="car1"/>
13                 <entry key="Rolls-Royce" value-ref="car2"/> <!--可以引入 Bean , 也可用内部 Bean-->
14                 <entry key="Ferrari">
15                     <bean id="car3" class="com.itdoc.spring.beans.Car">
16                         <property name="brand" value="Ferrari"/>
17                         <property name="price" value="22500000"/>
18                         <property name="maxSpeed" value="330"/>
19                     </bean>
20                 </entry>
21             </map>
22         </property>
23     </bean>
24 
25     <bean id="car1" class="com.itdoc.spring.beans.Car">
26         <property name="brand" value="Lamborghini"/>
27         <property name="price" value="27000000"/>
28         <property name="maxSpeed" value="300"/>
29     </bean>
30 
31     <bean id="car2" class="com.itdoc.spring.beans.Car">
32         <property name="brand" value="Rolls-Royce"/>
33         <property name="price" value="24500000"/>
34         <property name="maxSpeed" value="310"/>
35     </bean>
36 
37 </beans>
 1 package com.itdoc.spring.map;
 2 
 3 import com.itdoc.spring.beans.Car;
 4 
 5 import java.util.Map;
 6 
 7 /**
 8  * 集合属性注入
 9  * http://www.cnblogs.com/goodcheap
10  *
11  * @author: Wáng Chéng Dá
12  * @create: 2017-02-28 19:58
13  */
14 public class Person {
15 
16     private String name;
17 
18     private String sex;
19 
20     private int age;
21 
22     private Map<String, Car> cars;
23 
24     public String getName() {
25         return name;
26     }
27 
28     public void setName(String name) {
29         this.name = name;
30     }
31 
32     public String getSex() {
33         return sex;
34     }
35 
36     public void setSex(String sex) {
37         this.sex = sex;
38     }
39 
40     public int getAge() {
41         return age;
42     }
43 
44     public void setAge(int age) {
45         this.age = age;
46     }
47 
48     public Map<String, Car> getCars() {
49         return cars;
50     }
51 
52     public void setCars(Map<String, Car> cars) {
53         this.cars = cars;
54     }
55 
56     @Override
57     public String toString() {
58         return "Person{" +
59                 "name='" + name + '\'' +
60                 ", sex='" + sex + '\'' +
61                 ", age=" + age +
62                 ", cars=" + cars +
63                 '}';
64     }
65 }

 

控制台输出:

Person{name='华崽儿', sex='女', age=28, cars={Lamborghini=Car{brand='Lamborghini', price=2.7E7, maxSpeed=300}, Rolls-Royce=Car{brand='Rolls-Royce', price=2.45E7, maxSpeed=310}, Ferrari=Car{brand='Ferrari', price=2.25E7, maxSpeed=330}}}

 

<props>标签

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="dataSource" class="com.itdjx.spring.dependency.injection.DataSource">
 7         <property name="properties" >
 8             <props>
 9                 <prop key="user">root</prop>
10                 <prop key="possword">123456</prop>
11                 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
12                 <prop key="driverClass">com.mysql.jdbc.Driver</prop>
13             </props>
14         </property>
15     </bean>
16 
17 </beans>
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 import java.util.Properties;
 4 
 5 /**
 6  * @author Wáng Chéng Dá
 7  * @create 2017-03-01 10:21
 8  */
 9 public class DataSource {
10 
11     private Properties properties;
12 
13     public Properties getProperties() {
14         return properties;
15     }
16 
17     public void setProperties(Properties properties) {
18         this.properties = properties;
19     }
20 
21     @Override
22     public String toString() {
23         return "DataSource{" +
24                 "properties=" + properties +
25                 '}';
26     }
27 }
 1 package com.itdjx.spring.dependency.injection;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 /**
 7  * 依赖注入main
 8  *
 9  * @author Wáng Chéng Dá
10  * @create 2017-02-28 15:16
11  */
12 public class MainIOC {
13 
14     public static void main(String[] args) {
15 
16         ApplicationContext app = new ClassPathXmlApplicationContext("applicationConfig.xml");
17         DataSource dataSource = (DataSource) app.getBean("dataSource");
18         System.out.println(dataSource);
19 
20     }
21 }

 

控制台输出:

DataSource{properties={driverClass=com.mysql.jdbc.Driver, user=root, jdbcUrl=jdbc:mysql:///test, possword=123456}}
posted @ 2017-02-28 20:49  Chinda  阅读(472)  评论(0编辑  收藏  举报