03-Spring基於xml的IOC配置--spring的依賴注入

一、概念

依賴注入:Dependency Injection(簡稱DI注入)。它是 spring 框架核心 ioc 的具體實現。 簡單理解:能夠在一個類中不經過new的方式依賴其它對象。目的是爲了解耦。java

 

PS:工程依舊沿用02課程的,maven和applicationContet.xml能夠直接去02複製粘貼。spring

 

二、構造方法注入屬性

2.1 建立Phone對象app

public class Phone {

}

2.2 調整Student類。學生擁有姓名、年齡和手機。框架

public class Student {
    private String name;
    private Integer age;
    private Phone phone;

    public Student(String name, Integer age, Phone phone) {
        this.name = name;
        this.age = age;
        this.phone = phone;
        System.out.println("I'm " + name + ", age " + age + ", phone " + phone);
    }

    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public void say() {
        System.out.println("I'm a Student");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

2.3 修改applicationContext.xml文件裝配Student的bean標籤dom

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--
        1. spring會經過反射的方式建立對象,並將該對象以key和value的方式存入到IOC容器中。
        2. bean標籤的id就是key,vaule就是類的全路徑
        3. 經過bean標籤將對象建立並存入到IOC容器中的方式,咱們能夠稱之爲裝配bean
        4. 只要能夠正常new出來的對象,均可以經過這種方式裝配到IOC容器中
    -->
    <!-- 裝配Phone對象到IOC容器中 -->
    <bean id="phone" class="com.demo.domain.Phone"/>

    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student">
        <!--
            constructor-arg標籤:
                name屬性:指定參數在構造函數中的名稱,指定給誰賦值
                value屬性:只能是基本數據類型和String類型的
                ref屬性:指定其它bean類型,且必須在IOC容器中
        -->
        <constructor-arg name="name" value="zhangsan"/>
        <constructor-arg name="age" value="21"/>
        <constructor-arg name="phone" ref="phone"/>

    </bean>

    <!-- 使用靜態工廠方法將Teacher對象裝配到IOC容器中 -->
    <bean id="teacher" class="com.demo.factory.StaticFactory" factory-method="createTeacher"/>

    <!-- 使用實例工廠方法實例化bean -->
    <!-- 1. 裝配實例工廠-->
    <bean id="instanceFactory" class="com.demo.factory.InstanceFactory"/>
    <!-- 2. 使用實例工廠建立cat對象-->
    <bean id="cat" factory-bean="instanceFactory" factory-method="createCat"/>

</beans>

2.4 運行方法maven

 

 

三、Setter方法注入

3.1 修改Teacher類函數

public class Teacher {

    private String name;

    private Integer age;

    private Phone phone;

    public Phone getPhone() {
        return phone;
    }

    public void setPhone(Phone phone) {
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void say() {
        System.out.println("I'm a teacher. name: " + name + ", age: " + age + ", phone: " + phone);
    }
}

3.2 修改applicationContext.xml中裝配Teacher對象的bean標籤測試

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--
        1. spring會經過反射的方式建立對象,並將該對象以key和value的方式存入到IOC容器中。
        2. bean標籤的id就是key,vaule就是類的全路徑
        3. 經過bean標籤將對象建立並存入到IOC容器中的方式,咱們能夠稱之爲裝配bean
        4. 只要能夠正常new出來的對象,均可以經過這種方式裝配到IOC容器中
    -->
    <!-- 裝配Phone對象到IOC容器中 -->
    <bean id="phone" class="com.demo.domain.Phone"/>

    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student">
        <!--
            constructor-arg標籤:
                name屬性:指定參數在構造函數中的名稱,指定給誰賦值
                value屬性:只能是基本數據類型和String類型的
                ref屬性:指定其它bean類型,且必須在IOC容器中
        -->
        <constructor-arg name="name" value="zhangsan"/>
        <constructor-arg name="age" value="21"/>
        <constructor-arg name="phone" ref="phone"/>

    </bean>

    <!-- 使用靜態工廠方法將Teacher對象裝配到IOC容器中 -->
    <bean id="teacher" class="com.demo.factory.StaticFactory" factory-method="createTeacher">
        <!--
             property標籤
                name屬性:找的類中set方法後面的部分
                value屬性:給屬性賦值是基本數據類型和String類型的
                ref:給屬性賦值是其餘bean類型的。
         -->
        <property name="name" value="lisi"/>
        <property name="age" value="25"/>
        <property name="phone" ref="phone"/>
    </bean>

    <!-- 使用實例工廠方法實例化bean -->
    <!-- 1. 裝配實例工廠-->
    <bean id="instanceFactory" class="com.demo.factory.InstanceFactory"/>
    <!-- 2. 使用實例工廠建立cat對象-->
    <bean id="cat" factory-bean="instanceFactory" factory-method="createCat"/>

</beans>

3.3 運行getTeaccherObjectFromSrpingIoc方法this

 

 PS:咱們看到打印了兩句話。爲何?spa

  這是由於加載配置文件的時候,初始化對象裝配到IOC容器中,因爲Student對象是有參構造,而且在構造方法中打印了一句話。

四、P名稱空間注入

4.1 在applicationContext.xml文件中添加P名稱空間

xmlns:p="http://www.springframework.org/schema/p"

 

 4.2 修改cat類

 
 
public class Cat {
private String name;
private Integer age;
private Phone phone;

public String getName() {
return name;
}

public Phone getPhone() {
return phone;
}

public void setPhone(Phone phone) {
this.phone = phone;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public void say() {
System.out.println("I'm a cat. name: " + name + ", age: " + age + ", phone: " + phone);
}
}

4.3 修改applicationContext.xml文件中裝配cat對象bean標籤

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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"
       xmlns:p="http://www.springframework.org/schema/p">

    <!--
        1. spring會經過反射的方式建立對象,並將該對象以key和value的方式存入到IOC容器中。
        2. bean標籤的id就是key,vaule就是類的全路徑
        3. 經過bean標籤將對象建立並存入到IOC容器中的方式,咱們能夠稱之爲裝配bean
        4. 只要能夠正常new出來的對象,均可以經過這種方式裝配到IOC容器中
    -->
    <!-- 裝配Phone對象到IOC容器中 -->
    <bean id="phone" class="com.demo.domain.Phone"/>

    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student">
        <!--
            constructor-arg標籤:
                name屬性:指定參數在構造函數中的名稱,指定給誰賦值
                value屬性:只能是基本數據類型和String類型的
                ref屬性:指定其它bean類型,且必須在IOC容器中
        -->
        <constructor-arg name="name" value="zhangsan"/>
        <constructor-arg name="age" value="21"/>
        <constructor-arg name="phone" ref="phone"/>

    </bean>

    <!-- 使用靜態工廠方法將Teacher對象裝配到IOC容器中 -->
    <bean id="teacher" class="com.demo.factory.StaticFactory" factory-method="createTeacher">
        <!--
             property標籤
                name屬性:找的類中set方法後面的部分
                value屬性:給屬性賦值是基本數據類型和String類型的
                ref:給屬性賦值是其餘bean類型的。
         -->
        <property name="name" value="lisi"/>
        <property name="age" value="25"/>
        <property name="phone" ref="phone"/>
    </bean>

    <!-- 使用實例工廠方法實例化bean -->
    <!-- 1. 裝配實例工廠-->
    <bean id="instanceFactory" class="com.demo.factory.InstanceFactory"/>
    <!-- 2. 使用實例工廠建立cat對象-->
    <bean id="cat" factory-bean="instanceFactory" factory-method="createCat" p:name="tom" p:age="21"
          p:phone-ref="phone"/>

</beans>

4.4 運行getCatObjectFromSrpingIoc方法

 

 

五、複雜類型的注入

5.1 建立HelloWorld實體類

import java.util.*;


public class HelloWorld {
    private String[] myArrays;
    private List<String> myList;
    private Set<String> mySet;
    private Map<String, String> myMap;
    private Properties myProps;

    public String[] getMyArrays() {
        return myArrays;
    }

    public void setMyArrays(String[] myArrays) {
        this.myArrays = myArrays;
    }

    public List<String> getMyList() {
        return myList;
    }

    public void setMyList(List<String> myList) {
        this.myList = myList;
    }

    public Set<String> getMySet() {
        return mySet;
    }

    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }

    public Map<String, String> getMyMap() {
        return myMap;
    }

    public void setMyMap(Map<String, String> myMap) {
        this.myMap = myMap;
    }

    public Properties getMyProps() {
        return myProps;
    }

    public void setMyProps(Properties myProps) {
        this.myProps = myProps;
    }

    public void sayHello() {
        System.out.println("Hello World!");
        System.out.println(Arrays.toString(myArrays));
        System.out.println(myList);
        System.out.println(mySet);
        System.out.println(myMap);
        System.out.println(myProps);

    }
}

5.2 在applicationContext.xml中裝配HelloWorld對象

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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"
       xmlns:p="http://www.springframework.org/schema/p">

    <!--
        1. spring會經過反射的方式建立對象,並將該對象以key和value的方式存入到IOC容器中。
        2. bean標籤的id就是key,vaule就是類的全路徑
        3. 經過bean標籤將對象建立並存入到IOC容器中的方式,咱們能夠稱之爲裝配bean
        4. 只要能夠正常new出來的對象,均可以經過這種方式裝配到IOC容器中
    -->
    <!-- 裝配Phone對象到IOC容器中 -->
    <bean id="phone" class="com.demo.domain.Phone"/>

    <!-- 裝配Studnt對象到IOC容器中 -->
    <bean id="student" class="com.demo.domain.Student">
        <!--
            constructor-arg標籤:
                name屬性:指定參數在構造函數中的名稱,指定給誰賦值
                value屬性:只能是基本數據類型和String類型的
                ref屬性:指定其它bean類型,且必須在IOC容器中
        -->
        <constructor-arg name="name" value="zhangsan"/>
        <constructor-arg name="age" value="21"/>
        <constructor-arg name="phone" ref="phone"/>

    </bean>

    <!-- 使用靜態工廠方法將Teacher對象裝配到IOC容器中 -->
    <bean id="teacher" class="com.demo.factory.StaticFactory" factory-method="createTeacher">
        <!--
             property標籤
                name屬性:找的類中set方法後面的部分
                value屬性:給屬性賦值是基本數據類型和String類型的
                ref:給屬性賦值是其餘bean類型的。
         -->
        <property name="name" value="lisi"/>
        <property name="age" value="25"/>
        <property name="phone" ref="phone"/>
    </bean>

    <!-- 使用實例工廠方法實例化bean -->
    <!-- 1. 裝配實例工廠-->
    <bean id="instanceFactory" class="com.demo.factory.InstanceFactory"/>
    <!-- 2. 使用實例工廠建立cat對象-->
    <bean id="cat" factory-bean="instanceFactory" factory-method="createCat" p:name="tom" p:age="21"
          p:phone-ref="phone"/>

    <!-- 裝配HelloWorld對象到IOC容器中 -->
    <bean id="helloWorld" class="com.demo.domain.HelloWorld">
        <property name="myArrays">
            <array>
                <value>AAA</value>
                <value>BBB</value>
                <value>CCC</value>
            </array>
        </property>
        <property name="myList">
            <list>
                <value>CCC</value>
                <value>DDD</value>
                <value>EEE</value>
            </list>
        </property>
        <property name="mySet">
            <set>
                <value>FFF</value>
                <value>GGG</value>
                <value>HHH</value>
            </set>
        </property>
        <property name="myMap">
            <map>
                <entry key="name1" value="III"/>
                <entry key="name2" value="JJJ"/>
                <entry key="name3" value="KKK"/>
            </map>
        </property>
        <property name="myProps">
            <props>
                <prop key="name1">LLL</prop>
                <prop key="name2">MMM</prop>
                <prop key="name3">NNN</prop>
            </props>
        </property>
    </bean>
</beans>

5.3 測試

    @Test
    public void getHelloWorldObjectFromSrpingIoc() {
        //從SpringIOC容器中獲取HelloWorld對象

        //1. 根據bean的id去IOC容器中獲取HelloWorld對象
        HelloWorld helloWorld = (HelloWorld) ac.getBean("helloWorld");
        //2. 調用helloWolrd中的sayHello()方法
        helloWorld.sayHello();
    }

相關文章
相關標籤/搜索