Spring_DI_XML_01

简介: Spring_DI_XML_01

基于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='华东交通大学'}}

相关文章
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
17221 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
6天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1300 3
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2369 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3846 22
|
20天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是DeepSeek AI开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、跑命令、处理表格、联网搜索等操作。免费开源,兼容Windows/macOS/Linux,提供桌面端与网页端,可灵活安装插件扩展能力。