1. 程式人生 > >六 Spring的配置:屬性註入

六 Spring的配置:屬性註入

1.0 set property pro version () ng- 空間 demo

Spring的屬性註入:

  • 構造方法的屬性註入
  • set方法的屬性註入

技術分享圖片

構造方法的屬性註入:

技術分享圖片

set方法的屬性註入:

技術分享圖片

set方法註入對象:

技術分享圖片

 1 package com.itheima.spring.demo4;
 2 
 3 public class Employee {
 4       private String name;
 5       private Car2 car2;
 6     
 7       public void setName(String name) {
 8         this.name = name;
 9     }
10       public
void setCar2(Car2 car2) { 11 this.car2 = car2; 12 } 13 14 @Override 15 public String toString() { 16 return "Employee [name=" + name + ", car=" + car2 + "]"; 17 } 18 19 }

P名稱空間的屬性註入

技術分享圖片

技術分享圖片

spEL的屬性註入

spEL: Spring Expression Language:Spring表達式語言

語法:

#{spEL}

技術分享圖片

復雜類型的屬性註入

 1 /**
 2  * 集合屬性的註入
 3  *
 4  */
 5    public class CollectionBean { 
 6     private String[] arrs;
 7     private List<String> list;
 8     private Set<String> set;
 9     private Map<String,String> map;
10     
11     
12     
13     public void setMap(Map<String, String> map
) { 14 this.map = map; 15 } 16 17 public void setSet(Set<String> set) { 18 this.set = set; 19 } 20 21 public void setList(List<String> list) { 22 this.list = list; 23 } 24 25 public void setArrs(String[] arrs) { 26 this.arrs = arrs; 27 } 28 29 @Override 30 public String toString() { 31 return "CollectionBean [arrs=" + Arrays.toString(arrs) + ", list=" + list + ", set=" + set + ", map=" + map 32 + "]"; 33 } 34 35 36 37 38 39 40 }

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3       xmlns:p="http://www.springframework.org/schema/p"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans 
 7         http://www.springframework.org/schema/beans/spring-beans.xsd">
 8    
 9    <!-- Spring的集合屬性的註入 -->
10    <bean id="collectionBean" class="com.itheima.spring.demo5.CollectionBean">
11      <property name="arrs">
12      <!-- 註入的數組類型 -->
13      <list>
14        <value>王東</value>
15        <value>趙洪</value>
16        <value>李冠希</value>
17      </list>
18      </property>
19    
20    <!--註入list集合  -->
21    <property name="list">
22    <list>
23    <value>李兵</value>
24    <value>趙如花</value>
25    <value>鄧風</value>
26    </list>
27    </property>
28    
29     <!--註入Set集合  -->
30     <property name="set">
31     <set>
32     <value>aaa</value>
33     <value>vvv</value>
34     <value>ccc</value>
35     </set>
36     </property>
37     
38     <!--註入Map集合  -->
39     <property name="map">
40     <map>
41       <entry key="aaa" value="111"></entry>
42        <entry key="aaaa" value="111a"></entry>
43         <entry key="aaaa" value="111a"></entry>
44     </map>
45     </property>
46     
47     </bean>
48 </beans>

六 Spring的配置:屬性註入