di:就是依赖注入,给属性赋值。

di注入的分类:

1.设值注入,调用java类中的set方法,给属性赋值。

2. 构造注入,调用java类中的有参数构造方法,创建对象的同时,给属性赋值。

di的语法:

1. 基于xml的配置文件,在xml中使用标签和属性,完成属性的赋值。

2.基于注解的方式,使用注解创建对象,给属性赋值。

设值注入(set方法注入)

简单类型的设值注入

package com.atChina.Test;public class Student {private String name;private int age;public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}
}
<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 设值注入: 调用类中的set方法完成属性赋值 简单类型: spring中把string和java基本数据类型,称为简单类型简单类型的设值注入:<bean id="xx" class="yy"><property name="属性名" value="简单类型的属性值"/><property name="属性名" value="简单类型的属性值"/>...</bean>--><bean id="student" class="com.atChina.Test.Student"><property name="name" value="宋江"/><property name="age" value="20" /></bean>
</beans>

引用类型的设值注入:

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<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"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd "><!-- 设值注入: 调用类中的set方法完成属性赋值 简单类型: spring中把string和java基本数据类型,称为简单类型1)简单类型的设值注入:<bean id="xx" class="yy"><property name="属性名" value="简单类型的属性值"/><property name="属性名" value="简单类型的属性值"/>...</bean>2)引用类型的设值注入语法1: 使用ref作为属性<bean id="xx" class="yy"><property name="属性名" ref="bean的id"/></bean>语法2: 使用ref作为子标签<bean id="xx" class="yy"><property name="属性名"><ref bean="bean的id"/><property/></bean>--><!-- 使用语法1,给引用类型赋值,ref作为属性 --><bean id="student" class="com.atChina.Test2.Student"><property name="name" value="宋江"/><property name="age" value="20" /><property name="school" ref="xuexiao"/></bean><!-- 使用语法2,ref作为子标签性 --><bean id="student2" class="com.atChina.Test2.Student"><property name="name" value="吴用"/><property name="age" value="22" /><property name="school"><ref bean="xuexiao"/></property></bean><bean id="xuexiao" class="com.atChina.Test2.School"><property name="name" value="同济大学"/><property name="address" value="上海市" /></bean>
</beans>
package com.atChina.Test2;public class Student {private String name;private int age;private School school;public Student(){System.out.println("无参数构造方法...");}public void setSchool(School school) {this.school = school;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", school=" + school+ "]";}
}
package com.atChina.Test2;public class School {private String address;private String name;public void setAddress(String address) {this.address = address;}@Overridepublic String toString() {return "School [address=" + address + ", name=" + name + "]";}public void setName(String name) {this.name = name;}
}

spring7: di依赖注入--设值注入相关推荐

  1. Spring------基于xml的DI (一)设值注入、构造注入

    DI的分类 DI:bean实例在调用无参构造器创建了空值对象后,对bean对象的属性进行初始化.初始化是由容器自动完成的,称为注入. 注入分为:设值注入.构造注入. 设值注入 概念:是指,通过sett ...

  2. Spring受管Bean依赖注入(设值注入)

    设值注入是Spring支持的多种依赖注入类型中的一种,也是最为常见的一种.设值(setter)注入指在通过调用无参构造器(或无参静态工厂方法,或工厂Bean的蜚静态工厂方法)实例化受管Bean后调用s ...

  3. 构造方法注入和设值注入有什么区别?

    请注意以下明显的区别: 1.在设值注入方法支持大部分的依赖注入,如果我们仅需要注入int.string 和long 型的变量,我们不要用设值的方法注入.对于基本类型,如果我们没有注入的话,可以为基本类 ...

  4. 构造方法注入和设值注入有什么区别

    请注意以下明显的区别: (1)设值注入支持大部分依赖注入,如果我们仅需要注入int.string和long型的变量,不要用设值方法注入.对于基本类型,如果没有注入,可以为基本类型设置默认值.构造方法注 ...

  5. spring容器的设值注入和构造注入

    例如我们现在有一个Computer类: public class Computer {private String cpu;private String hdd;//硬盘private String ...

  6. 【Spring实战】—— 5 设值注入

    2019独角兽企业重金招聘Python工程师标准>>> 本篇主要讲解了Spring的最常用的功能--依赖注入. 注入的方式,是使用Getter Setter注入,平时大多的编程也都是 ...

  7. Spring DI[依赖注入]

    依赖注入(Dependency Injection,简称DI)意思是由容器或者框架将被调用类注入给调用对象,以此来降低调用对象和被调用类之间的依赖关系. 依赖注入主要有2种不同的实现形式: 1. 构造 ...

  8. DI 依赖注入实现原理

    深度理解依赖注入(Dependence Injection) 前面的话:提到依赖注入,大家都会想到老马那篇经典的文章.其实,本文就是相当于对那篇文章的解读.所以,如果您对原文已经有了非常深刻的理解,完 ...

  9. setter注入和构造器注入

    bean 实例在调用无参构造器创建对象后,就要对 bean 对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类: setter注入 构造器注入 目录 sette ...

最新文章

  1. FPP(彩包)、COEM(简包)、MOLP(license授) 介绍
  2. 送一台电脑显示器,我每天办公都用它,安利!
  3. Delphi三层开发小技巧:TClientDataSet的Delta妙用
  4. 个人作业1-数组(续1)
  5. mac homebrew装mysql_mac系统homebrew安装mysql
  6. 正则表达式元字符整理
  7. 工作315:uni-修改添加时间的逻辑
  8. ORACLE日期时间函数大全(一)
  9. day14——内置函数
  10. idea 编译内存溢出
  11. 构建AD域 、 管理AD域
  12. 强东变法——京东能否逢凶化吉?
  13. haskell 基础题解(20)
  14. 记录一次idea启动失败问题Improperly specified VM option. To fix the problem, edit your JVM options and remove t
  15. C#合并多个pdf到一个pdf文件;不使用Aspose.pdf.dll,避免水印
  16. 【Codeforces】1635E Cars 题解
  17. maven提示:Failed to execute goal on project ...: Could not resolve dependencies for project ...
  18. 关于Cisco交换机接口模式的详细介绍
  19. 配置java到cics_CICS入门
  20. 目标检测(3)—— 如何使用PyTorch加载COCO类型的数据集

热门文章

  1. js实现旋转木马轮播图
  2. intellijidea课程 intellijidea神器使用技巧 3-1 列操作
  3. 移动端整屏滑动的实现
  4. 按照Right-BICEP要求对实验二进行测试
  5. (Android Studio)ActionBar's Theme/Style [ActionBar主题风格修改]
  6. [整理] C#调用SQLDMO.DLL时间数据库备份 / 还原。 (香神无涯) // C#实现SQLSERVER2000数据库备份还原的两种方法 (带进度条)...
  7. DFS深度优先搜索算法/BFS广度优先搜索算法(c/c++)
  8. 动态二维数组赋值及for循环遍历和toString遍历
  9. 目前可用的微博秀的嵌入方法大全(亲测2019年2月仍有效)
  10. 【网寻】mui - 点击事件