Spring_DI_XML_01

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

基于XMl的DI
1.设值注入
2.构造注入
3.p命名空间设值注入
4.c命名空间构造注入

官方文档

设值注入

调用属性的set方法,使用<property name="name" value="FutaoSmile"/>设值注入

环境搭建 -- 属性需要set方法,类不需要构造方法
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)
    }
}

输出结果

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">
        <!--设值注入,实际上是通过调用set方法实现-->
        <property name="name" value="FutaoSmile"/>
        <property name="age" value="18"/>
    </bean>
</beans>

测试结果:

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">
        <!--设值注入,实际上是通过调用set方法实现-->
        <!-- 即:property标签调用到的是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>

测试结果

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


基于XMl的DI-构造注入

直接调用带参构造器 - 使用<constructor-arg name="0" value="李四"/>构造注入
环境搭建 -- 属性不需要set方法,类需要带参的构造方法
Student类

package gouzaoDI;

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

    /**
     * 带参构造器
     *
     * @param name   姓名
     * @param age    年龄
     * @param school 学校
     */
    public Student(String name, int age, School school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

    /**
     * 无参构造器
     */
    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;

    /**
     * 带参构造方法
     *
     * @param schoolName 学校名称
     */
    public School(String schoolName) {
        this.schoolName = schoolName;
    }

    /**
     * 无参构造方法
     */
    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">
        <!--构造注入方式1,用index,实际上是通过构造方法注入,index表示构造方法的参数索引-->
        <!--<constructor-arg index="0" value="李四"/>-->
        <!--<constructor-arg index="1" value="18"/>-->
        <!--<constructor-arg index="2" ref="school"/>-->
        <!--构造注入方式2,用name-->
        <!--<constructor-arg name="name" value="李四"/>-->
        <!--<constructor-arg name="age" value="18"/>-->
        <!--<constructor-arg name="school" ref="school"/>-->
        <!--构造注入方式3,省略index和name,但是这样要保证顺序与构造方法中参数的顺序是一样的-->
        <constructor-arg value="李四"/>
        <constructor-arg value="18"/>
        <constructor-arg ref="school"/>

    </bean>
    <bean id="school" class="gouzaoDI.School">
        <constructor-arg index="0" value="华东交通大学"/>
    </bean>
</beans>

测试

/**
     * 构造注入- 通过类的带参构造方法
     */
    @Test
    fun test4gouzaoDI() {
        val xml = ClassPathXmlApplicationContext("applicationContextGouzaoDI.xml")
        val student = xml.getBean("student") as gouzaoDI.Student
        println(student)
    }

结果

Student{name='李四', age=18, school=School{schoolName='华东交通大学'}}

P名字空间设值注入 p:schoolName="East China JiaoTong University"

实际上是通过属性的set方法实现的,所以如果要通过p名字空间进行设只注入的属性,需要为该属性设置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名字空间设值注入的原理其实是通过属性的set方法实现的,通过追踪代码可以出来-->
    <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命名空间设值注入
     */
    @Test
    fun test4pNamespaceDI() {
        val xml = ClassPathXmlApplicationContext("applicationContextPnamespaceDI.xml")
        val student = xml.getBean("student") as PnamespaceDI.Student
        println(student)
    }

结果

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

c名字空间构造注入 - c:schoolName="华东交通大学"

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

原理:c名字空间构造注入,原理实际上是通过类的构造方法实现的,所以要使用c名字空间注入就需要为类添加带参的构造方法

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名字空间设值注入的原理其实是通过属性的set方法实现的,通过追踪代码可以出来-->
    <!--<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名字空间构造注入,原理实际上是通过类的构造方法实现的-->
    <bean id="student" class="PnamespaceDI.Student" c:name="coder" c:age="18" c:school-ref="school"/>
    <bean id="school" class="PnamespaceDI.School"  c:schoolName="华东交通大学"/>
</beans>

测试结果:
Student{name='coder', age=18, school=School{schoolName='华东交通大学'}}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,539评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,594评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,871评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,963评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,984评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,763评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,468评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,357评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,850评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,002评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,144评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,823评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,483评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,026评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,150评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,415评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,092评论 2 355

推荐阅读更多精彩内容

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan阅读 4,166评论 2 7
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,672评论 18 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,644评论 18 399
  • 笔记 Xmind PPT Paths 总结 NSAttributedString描述
    CoderZXS阅读 130评论 0 0
  • 花开花落,静静等待 寂寞忧伤在心中打转 雨,淅淅沥沥地下着 若叹息 一丝幽香淡香飘过 是期许的诺言 未名花的葬礼,...
    零点过十分阅读 226评论 0 3