六 Spring的配置:属性注入

 

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>

猜你喜欢

转载自www.cnblogs.com/ltfxy/p/9862927.html