Spring_DI_XML_01

歡迎移步博客查看-http://futaosmile.oschina.io/coder957

基于XMl的DI
1.設(shè)值注入
2.構(gòu)造注入
3.p命名空間設(shè)值注入
4.c命名空間構(gòu)造注入

官方文檔

設(shè)值注入

調(diào)用屬性的set方法,使用<property name="name" value="FutaoSmile"/>設(shè)值注入

環(huán)境搭建 -- 屬性需要set方法,類不需要構(gòu)造方法
Bean-Student

/**
 * Created by futao on 2017/10/10.
 */
public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

配置文件-applicationContext.xml

<?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">
    <bean id="student" class="Student">
  
    </bean>
</beans>

測試

import org.junit.Test
import org.springframework.context.support.ClassPathXmlApplicationContext

/**
 * Created by futao on 2017/10/10.
 */
class Mytest {
    @Test
    fun forTest() {
        val xml = ClassPathXmlApplicationContext("applicationContext.xml")
        val student = xml.getBean("student") as Student
        println(student)
    }
}

輸出結(jié)果

Student{name='null', age=0}

修改配置文件applicationContext.xml

<?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">
    <bean id="student" class="Student">
        <!--設(shè)值注入,實際上是通過調(diào)用set方法實現(xiàn)-->
        <property name="name" value="FutaoSmile"/>
        <property name="age" value="18"/>
    </bean>
</beans>

測試結(jié)果:

Student{name='FutaoSmile', age=18}

新建School類

/**
 * Created by futao on 2017/10/10.
 */
public class School {
    private String schoolName;

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    @Override
    public String toString() {
        return "School{" +
                "schoolName='" + schoolName + '\'' +
                '}';
    }
}

School在Student類中作為屬性存在

/**
 * Created by futao on 2017/10/10.
 */
public class Student {
    private String name;
    private int age;
    private School school;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }

  
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

修改配置文件applicationContext.xml注入school屬性值

<?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">
    <bean id="student" class="Student">
        <!--設(shè)值注入,實際上是通過調(diào)用set方法實現(xiàn)-->
        <!-- 即:property標(biāo)簽調(diào)用到的是set方法 -->
        <property name="name" value="FutaoSmile"/>
        <property name="age" value="18"/>
        <!-- 添加school的引用 -->
        <property name="school" ref="school"/>
    </bean>
    <bean id="school" class="School">
        <property name="schoolName" value="Ecjtu"/>
    </bean>
</beans>

測試結(jié)果

Student{name='FutaoSmile', age=18, school=School{schoolName='Ecjtu'}}


基于XMl的DI-構(gòu)造注入

直接調(diào)用帶參構(gòu)造器 - 使用<constructor-arg name="0" value="李四"/>構(gòu)造注入
環(huán)境搭建 -- 屬性不需要set方法,類需要帶參的構(gòu)造方法
Student類

package gouzaoDI;

/**
 * Created by futao on 2017/10/10.
 */
public class Student {
    private String name;
    private int age;
    private School school;

    /**
     * 帶參構(gòu)造器
     *
     * @param name   姓名
     * @param age    年齡
     * @param school 學(xué)校
     */
    public Student(String name, int age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

    /**
     * 無參構(gòu)造器
     */
    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school=" + school +
                '}';
    }
}

School類

package gouzaoDI;

/**
 * Created by futao on 2017/10/10.
 */
public class School {
    private String schoolName;

    /**
     * 帶參構(gòu)造方法
     *
     * @param schoolName 學(xué)校名稱
     */
    public School(String schoolName) {
        this.schoolName = schoolName;
    }

    /**
     * 無參構(gòu)造方法
     */
    public School() {
    }

    @Override
    public String toString() {
        return "School{" +
                "schoolName='" + schoolName + '\'' +
                '}';
    }
}

配置文件applicationContextGouzaoDI.xml

<?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">
    <bean id="student" class="gouzaoDI.Student">
        <!--構(gòu)造注入方式1,用index,實際上是通過構(gòu)造方法注入,index表示構(gòu)造方法的參數(shù)索引-->
        <!--<constructor-arg index="0" value="李四"/>-->
        <!--<constructor-arg index="1" value="18"/>-->
        <!--<constructor-arg index="2" ref="school"/>-->
        <!--構(gòu)造注入方式2,用name-->
        <!--<constructor-arg name="name" value="李四"/>-->
        <!--<constructor-arg name="age" value="18"/>-->
        <!--<constructor-arg name="school" ref="school"/>-->
        <!--構(gòu)造注入方式3,省略index和name,但是這樣要保證順序與構(gòu)造方法中參數(shù)的順序是一樣的-->
        <constructor-arg value="李四"/>
        <constructor-arg value="18"/>
        <constructor-arg ref="school"/>

    </bean>
    <bean id="school" class="gouzaoDI.School">
        <constructor-arg index="0" value="華東交通大學(xué)"/>
    </bean>
</beans>

測試

/**
     * 構(gòu)造注入- 通過類的帶參構(gòu)造方法
     */
    @Test
    fun test4gouzaoDI() {
        val xml = ClassPathXmlApplicationContext("applicationContextGouzaoDI.xml")
        val student = xml.getBean("student") as gouzaoDI.Student
        println(student)
    }

結(jié)果

Student{name='李四', age=18, school=School{schoolName='華東交通大學(xué)'}}

P名字空間設(shè)值注入 p:schoolName="East China JiaoTong University"

實際上是通過屬性的set方法實現(xiàn)的,所以如果要通過p名字空間進(jìn)行設(shè)只注入的屬性,需要為該屬性設(shè)置setter

XML shortcut with the p-namespace
The p-namespace enables you to use the bean element’s attributes, instead of nested <property/> elements(property), to describe your property values and/or collaborating beans.

配置文件applicationContextPnamespaceDI.xml

注意:需要添加約束xmlns:p="http://www.springframework.org/schema/p"

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"

       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--p名字空間設(shè)值注入的原理其實是通過屬性的set方法實現(xiàn)的,通過追蹤代碼可以出來-->
    <bean id="student" class="PnamespaceDI.Student" p:name="麻子" p:age="18" p:school-ref="school"/>
    <bean id="school" class="PnamespaceDI.School" p:schoolName="East China JiaoTong University"/>
</beans>

測試


    /**
     * p命名空間設(shè)值注入
     */
    @Test
    fun test4pNamespaceDI() {
        val xml = ClassPathXmlApplicationContext("applicationContextPnamespaceDI.xml")
        val student = xml.getBean("student") as PnamespaceDI.Student
        println(student)
    }

結(jié)果

Student{name='麻子', age=18, school=School{schoolName='East China Jiao Tong University'}}

c名字空間構(gòu)造注入 - c:schoolName="華東交通大學(xué)"

添加約束xmlns:c="http://www.springframework.org/schema/c"

原理:c名字空間構(gòu)造注入,原理實際上是通過類的構(gòu)造方法實現(xiàn)的,所以要使用c名字空間注入就需要為類添加帶參的構(gòu)造方法

XML shortcut with the c-namespace
Similar to the the section called “XML shortcut with the p-namespace”, the c-namespace, newly introduced in Spring 3.1, allows usage of inlined attributes for configuring the constructor arguments rather then nested constructor-arg elements.

applicationContextCnamespaceDI.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!--p名字空間設(shè)值注入的原理其實是通過屬性的set方法實現(xiàn)的,通過追蹤代碼可以出來-->
    <!--<bean id="student" class="PnamespaceDI.Student" p:name="麻子" p:age="18" p:school-ref="school"/>-->
    <!--<bean id="school" class="PnamespaceDI.School" p:schoolName="East China JiaoTong University"/>-->
    <!--c名字空間構(gòu)造注入,原理實際上是通過類的構(gòu)造方法實現(xiàn)的-->
    <bean id="student" class="PnamespaceDI.Student" c:name="coder" c:age="18" c:school-ref="school"/>
    <bean id="school" class="PnamespaceDI.School"  c:schoolName="華東交通大學(xué)"/>
</beans>

測試結(jié)果:
Student{name='coder', age=18, school=School{schoolName='華東交通大學(xué)'}}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,206評論 2 7
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,778評論 18 399
  • 筆記 Xmind PPT Paths 總結(jié) NSAttributedString描述
    CoderZXS閱讀 135評論 0 0
  • 花開花落,靜靜等待 寂寞憂傷在心中打轉(zhuǎn) 雨,淅淅瀝瀝地下著 若嘆息 一絲幽香淡香飄過 是期許的諾言 未名花的葬禮,...
    零點(diǎn)過十分閱讀 233評論 0 3