1. 程式人生 > >Spring:(二)DI依賴註入方式

Spring:(二)DI依賴註入方式

sta static demo 完整 進行 import align 得到 xmla

DI 依賴註入

  DI(Dependency Injection)依賴註入,說簡單一點就將類裏面的屬性在創建類的過程中給屬性賦值,即將對象依賴屬性(簡單值,集合,對象)通過配置設值給該對象。

  

屬性註入的方式

  •  構造方法的方式
  •  set方法的方式
  •  工廠方法註入

  主要學習前兩種方式

構造方法的方式

  當是構造方法時註入Bean的屬性值(簡單值,集合,對象)

  利用<constructor-arg>標簽進行屬性的註入

    name:被設置屬性的名

    value:被設置屬性的值

 編寫用構造方法的pojo

 1 package
spring_test1.pojo; 2 3 public class UserConstructor { 4 private String name; 5 private int id; 6 7 public UserConstructor(String name, int id) { 8 super(); 9 this.name = name; 10 this.id = id; 11 } 12 13 @Override 14 public String toString() {
15 return "User_constructor [name=" + name + ", id=" + id + "]"; 16 } 17 }

XML配置編寫

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    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.xsd"> <!-- Spring構造方法註入 --> <bean name="user_cons" class="spring_test1.pojo.UserConstructor"> <constructor-arg name="name" value="Roy"/> <constructor-arg name="id" value="1001"/> </bean> </beans>

編寫測試類

 1 package spring_test1.test;
 2 
 3 import static org.junit.Assert.*;
 4 
 5 import org.junit.Test;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 
 9 import spring_test1.pojo.UserConstructor;
10 
11 public class UserConstructorTest {
12 
13     @Test
14     public void test() {
15         //創建Spring的工廠
16         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
17         //得到User對象
18         UserConstructor userConstructor = (UserConstructor) applicationContext.getBean("user_cons");
19         System.out.println(userConstructor);
20     }
21 }

運行結果

技術分享圖片

set方法的方式

  我在Spring:(一)那一篇中的第一個Spring程序便是set方法時的屬性註入方式

  利用<property>標簽

    name:被設置屬性的名

    value:被設置屬性的值

標準XML格式

編寫pojo

package spring_test1.pojo;

/**
 * @author jyroy
 *
 */
public class User {
    private String name;
    private int id;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", id=" + id + "]";
    }
}

編寫XML配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p"
    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.xsd">
        
        <!--Spring的set方法的屬性註入-->
        <bean name="user"  class="spring_test1.pojo.User">
            <property name="name" value="李東"/>
            <property name="id" value="1007" />
        </bean> 

        
</beans>

編寫測試類

 1 package spring_test1.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import spring_test1.pojo.User;
 8 
 9 public class UserTest {
10     
11     @Test
12     public void demo1() {
13         //創建Spring的工廠
14         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
15         //得到User對象
16         User user = (User) applicationContext.getBean("user");
17         System.out.println(user);
18     }
19 }

運行結果

技術分享圖片

p命名空間的方式

  看上面的XML配置,似乎用<property/>標簽還是比較臃腫。

  於是從2.0開始,Spring支持使用名稱空間的可擴展配置格式。這些名稱空間都是基於一種XML Schema定義。p命名空間就可以用bean 元素的屬性代替<property/>元素。

  還需要在使用p命名空間時先聲明使用對應的命名空間,即在bean元素上添加 xmlns:p="http://www.springframework.org/schema/p"

1         <!-- p命名空間的方式 -->
2         <bean id="user" class="spring_test1.pojo.User" p:name="Roy" p:id="1004"></bean>

c命名空間的方式

  C命名空間與p命名空間類似,但是使用c命名空間可以用內聯的構造參數代替嵌套的constructor-arg元素

  同樣先聲明使用對應的命名空間,即在bean元素上添加 xmlns:c="http://www.springframework.org/schema/c"

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 標準XML格式 -->
    <bean id="foo" class="x.y.Foo">
        <constructor-arg name="bar" ref="bar"/>
        <constructor-arg name="baz" ref="baz"/>
        <constructor-arg name="email" value="[email protected]"/>
    </bean>

    <!-- c命名空間格式 -->
    <bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="[email protected]"/>

    <!-- 還可以使用c命名空間的參數索引格式 -->
    <bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz" c:_2="[email protected]"/>

</beans>

SpEL表達式方式

  Spring 表達式語言 (Spring Expression Language),打算整理完整的一篇

集合類型屬性註入

 1         <bean id="collectionBean" class="com.roy.spring.demo5.CollectionBean">
 2             <!-- 數組類型 -->
 3             <property name="arrs">
 4                 <list>
 5                     <value>數組一</value>
 6                     <value>數組二</value>
 7                 </list>
 8             </property>
 9             <!-- 註入list集合類型 -->
10             <property name="list">
11                 <list>
12                     <value>list一</value>
13                     <value>list二</value>
14                 </list>
15             </property>
16 
17             <!-- 註入set集合類型-->
18             <property name="set">
19                 <set>
20                     <value>set一</value>
21                     <value>set二</value>
22                 </set>
23             </property>
24             
25             <!-- 註入Map集合 -->
26             <property name="map">
27                 <map>
28                     <entry key="aaa" value="111"></entry>
29                     <entry key="bbb" value="222"></entry>
30                     <entry key="ccc" value="333"></entry>
31                 </map>
32             </property>
33             
34             <!-- 註入property集合 -->
35             <property name="properties">
36                 <props>
37                     <prop key="key1">value1</prop>
38                     <prop key="key2">value2</prop>
39                     <prop key="key3">value3</prop>
40                 </props>
41             </property>
42         </bean>

Spring:(二)DI依賴註入方式