依耐注入DI

DI 依耐注入通俗来将就是给对象的成员变量(属性) 赋值

依耐注入的两种方式

⚫ set注入
⚫ 构造器注入

set注入

set注入(主流)
⚫ 名称:property
⚫ 类型:标签
⚫ 归属:bean标签
⚫ 作用:使用set方法的形式为bean提供资源
⚫ 格式:
< bean>
< property />
< /bean>
⚫ 基本属性:
< property name=“propertyName” value=“propertyValue” ref=“beanId”/>

◆ name:对应bean中的属性名,要求该属性必须提供可访问的set方法(严格规范为此名称是set方法对应名称
◆ value:设定非引用类型属性对应的值,不能与ref同时使用
◆ ref:设定引用类型属性对应bean的id ,不能与value同时使用
⚫ 注意:一个bean可以有多个property标签

实体类准备

创建一个person类,并创建3个属性,2个基本类型,一个引用类型,并提供set方法(让DI依耐注入)

package com.fs.di;
/*
spring的DI依耐注入*/
public class Person {private String name;private int age;private Student student;//因为我们DI的注入方式为set注入,所以生成一些set方法public void setName(String name) {/*为什么spring要使用set呢,不直接反射获得属性直接复制如果我们使用set的话,可以在set中做校验,比如我这里的名字,我可以在set方法中做判断*/if (name.length()<2){throw new RuntimeException("注入的名字的字数请大于2位~~~");}this.name = name;}public void setAge(int age) {this.age = age;}public void setStudent(Student student) {this.student = student;}public Student getStudent() {return student;}@Overridepublic String toString() {return "Person{" +"name='" + name + '\'' +", age=" + age +", student=" + student +'}';}
}

创建一个类,这个类是person中的一个属性,目的就是体现依耐注入可以给实体类属性注入值

package com.fs.di;public class Student {public void study(){System.out.println("好好学习!天天向上");}
}

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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!--DI 依耐注入通俗来将就是给对象的成员变量(属性) 赋值
--><!--    把student交给spring管理--><bean id="student" class="com.fs.di.Student"/><!--把person交给spring管理,并且set给person的属性赋值--><bean id="person" class="com.fs.di.Person">
<!--        基本数据类型--><property name="name" value="小付"/><property name="age" value="18"/>
<!--     ref是引用IOC中已有的bean,从ioc容器中获取对应的类将其注入这里注入的就是上面的student--><property name="student" ref="student"/></bean>
</beans>

测试方法

 //测试DI依耐注入set方法注入@Testpublic void DISet(){//创建spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//先从ioc中获取student对象Student student = (Student) applicationContext.getBean("student");//从ioc容器中获取对象Person person = (Person) applicationContext.getBean("person");//得到person中的student对象Student personStudent = person.getStudent();//判断一下两个对象是否是一个,是一个,说明是在配置文件中注入的student,都是从ioc中获取的System.out.println(student == personStudent);//true//打印输出一下System.out.println(person);/*由配置文件得知person是spring通过set注入的,在配置文件通过ref引用的上面配置的student所以,这两个student地址值肯定是一样的,因为springIOC默认是单例模式*/}

控制台输出结果

构造器注入

构造器注入(了解)
⚫ 名称:constructor-arg
⚫ 类型:标签
⚫ 归属:bean标签
⚫ 作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
⚫ 格式:
< bean>
< constructor-arg />
< /bean>
⚫ 基本属性:
<constructor-arg name=“argsName” value="argsValue />
◆ name:对应bean中的构造方法所携带的参数名
◆ value:设定非引用类型构造方法参数对应的值,不能与ref同时使用
⚫ 注意:一个bean可以有多个constructor-arg标签
⚫ 其他属性:
< constructor-arg index=“arg-index” type=“arg-type” ref=“beanId”/>
◆ ref:设定引用类型构造方法参数对应bean的id ,不能与value同时使用
◆ type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
◆ index :设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始计数

实体类准备

创建一个animal类,创建3个属性,一个属性为dog实体类,提供一个构造方法供给DI依耐注入

package com.fs.di;
/*
spring使用有参构造给属性赋值*/
public class Animal {private String name;private String genre;private Dog dog;//创建一个有参构造public Animal(String name, String genre, Dog dog) {this.name = name;this.genre = genre;this.dog = dog;}public Dog getDog() {return dog;}@Overridepublic String toString() {return "Animal{" +"name='" + name + '\'' +", genre='" + genre + '\'' +'}';}
}

dao实体类

package com.fs.di;public class Dog {public void testDog(){System.out.println("汪汪汪~~~");}
}

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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!--DI 依耐注入通俗来将就是给对象的成员变量(属性) 赋值
-->
<!--    将dog交给ioc管理--><bean id="dog" class="com.fs.di.Dog"/>
<!--    将Animal交给ioc管理,使用有参构造创建Animal bean --><bean id="animal" class="com.fs.di.Animal">
<!--    name 构造方法参数的名   value  基本数据   ref 引用ioc已有的类--><constructor-arg name="name" value="旺财"/><constructor-arg name="genre" value="柴犬"/><constructor-arg name="dog" ref="dog"/></bean>
</beans>

测试方法

    //测试有参构造将类交给ioc管理@Testpublic void testConstructor(){//创建spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//获取animalAnimal animal = (Animal) applicationContext.getBean("animal");//获取spring通过有参构造方法注入的dogDog dog = animal.getDog();dog.testDog();//输出一下spring通过有参构造注入的属性System.out.println(animal);}

控制台输出结果

集合类型数据注入

集合类型数据注入
⚫ 名称:array,list,set,map,props
⚫ 类型:标签
⚫ 归属:property标签 或 constructor-arg标签
⚫ 作用:注入集合数据类型属性
⚫ 格式:
< property>
< list>< /list>
< /property>

实体类准备

创建一个类,提供三个属性,类型为int[]和List< String>和HashMap< String,Object> ,提供set方法让DI依耐注入

package com.fs.list;import java.util.Arrays;
import java.util.HashMap;
import java.util.List;/*
集合类型的数据注入
set方式注入*/
public class CollectionDI {private int[] array;private List<String> list;private HashMap<String,Object> map;public void setArray(int[] array) {this.array = array;}public void setList(List<String> list) {this.list = list;}public void setMap(HashMap<String, Object> map) {this.map = map;}@Overridepublic String toString() {return "CollectionDI{" +"array=" + Arrays.toString(array) +", list=" + list +", map=" + map +'}';}
}

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"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!--DI 依耐注入通俗来将就是给对象的成员变量(属性) 赋值
--><!--    把student交给spring管理--><bean id="student" class="com.fs.di.Student"/><!--    set方式注入数组集合--><bean id="collection" class="com.fs.list.CollectionDI"><property name="array"><array><value>10</value><value>20</value></array></property><property name="list"><list><value>小付</value><value>小花</value></list></property><property name="map"><map><entry key="学生map" value="学生map的值"/><entry key="学生" value-ref="student"/></map></property></bean>
</beans>

测试方法

    //set注入集合类型@Testpublic void testCollection(){//创建spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");CollectionDI collection = (CollectionDI) applicationContext.getBean("collection");System.out.println(collection);}

控制台输出结果

使用p命名空间简化配置

applicationContext.xml配置文件

<!--    p命名空间 了解xmlns:p="http://www.springframework.org/schema/p"需要引入命名空间--><bean id="personP" class="com.fs.di.Person"p:name="小花"p:age="18"p:student-ref="student"/>

SpEL

applicationContext.xml配置文件

<!--Spring提供了对EL表达式的支持,统一属性注入格式注意:所有属性值不区分是否引用类型,统一使用value赋值--><bean id="personEL" class="com.fs.di.Person"><property name="name" value="#{'小李'}"/><property name="age" value="#{12}"/><property name="student" value="#{student}"/></bean>

SpringIOC的依耐注入DI---set注入---constructor有参构造注入---了解P命名空间---了解SpEL相关推荐

  1. IOC操作Bean管理XML方式(有参构造注入属性)

    IOC操作Bean管理XML方式 目录 有参构造注入属性 (1)步骤(创建类,定义属性,创建属性对应的有参构造方法): (2)步骤:在Spring 的xml配置文件中进行配置 (3)步骤:进行测试 结 ...

  2. [Spring5]IOC容器_Bean管理XML方式_创建对象_set注入属性and有参构造注入属性

    IOC操作 Bean管理 什么是Bean管理 1.Bean管理指的是两个操作: a.Spring创建对象 b.Spring注入属性 2.Bean管理操作有两种方式 a.基于xml配置文件方式实现 b. ...

  3. 控制反转(IoC)与依赖注入(DI)详解

    文章目录 什么是控制反转(IoC) 控制反转(IoC)有什么作用 控制反转(IoC)是怎么分类的 依赖注入 接口注入 Setter方法注入 构造器注入 依赖查找 上下文依赖查找(Contextuali ...

  4. Spring框架—③依赖注入DI、Bean作用域及自动装配

    依赖注入 DI,Dependency injection 依赖: 指bean对象的创建依赖于Spring容器 注入: 指Bean对象所依赖的资源,由容器来设置和装配 在beans.xml中配置 1.常 ...

  5. java-12:spring MVC - 控制反转IOC,依赖注入DI

    学习spring框架之前,先理解几个概念: 1.第一部分:依赖倒置原则 2.第二部分:控制反转,控制反转容器(实例) 3.第三部分:控制反转,控制反转容器(全面理解,面试题) 综合性理解:控制反转(I ...

  6. 依赖注入(DI)和Ninject,Ninject

    我们所需要的是,在一个类内部,不通过创建对象的实例而能够获得某个实现了公开接口的对象的引用.这种"需要",就称为DI(依赖注入,Dependency Injection),和所谓的 ...

  7. 依赖倒置(DIP),控制反转(IoC)与依赖注入(DI)

    DIP,IoC与DI概念解析 依赖倒置 DIP(Dependency Inversion Principle) DIP的两大原则: 1.高层模块不应该依赖于低层模块,二者都应该依赖于抽象. 2.抽象不 ...

  8. Spring学习4之依赖注入(DI)

    前言 上节学习了IOC创建对象的方式,我们在不知不觉中使用了最简单的构造注入,什么是构造注入,什么又是依赖注入呢? 一.首先我们要了解DI是什么? 创建对象的过程中Spring可以依据配置对象的属性进 ...

  9. 控制反转(Ioc)和依赖注入(DI)

    控制反转IOC, 全称 "Inversion of Control".依赖注入DI, 全称 "Dependency Injection". 面向的问题:软件开发 ...

最新文章

  1. Matlab编程与数据类型 -- 多维数组
  2. IntelliJ IDEA使用技巧(七)——恢复代码的方法(进阶篇)
  3. 2016设置方框的尺寸_四种模板脚手架分类、优缺点及参数设置对比
  4. .NET 缩略图服务器 ResizingServer
  5. oracle每一行的hash值,Hash分区表分区数与数据分布的测试
  6. c#中的socket(tcp)
  7. Linux下redis的安装及用法
  8. 入门到精通!珍藏资源!VAE变分自编码器
  9. python软件是什么作用,python-dotenv的用途是什么?
  10. NUC980 DIY项目大挑战 - EtherCAT实现
  11. 6款良心本地视频播放器,功能强大还完全免费
  12. 彻底解决--“未能加载文件或程序集“xxx.dll”或它的某一个依赖项”
  13. 互联网“围猎”老年人
  14. html b5纸尺寸,A5纸的尺寸多大(各种标准纸张大小A1,A2,A3,A4纸的尺寸)
  15. React学习笔记_从create-react-app学习webpack
  16. I2C协议研读(九):十位寻址
  17. 历史 微信开发者工具_不用微信开发者工具也能调试微信页面
  18. 聊聊语音聊天室app源码实时音视频中的技术难点:回声消除+噪声消除
  19. csp试题2:小明种苹果(绪)
  20. win7有杂音----彻底解决

热门文章

  1. 用JS实现发邮件的功能 完美解决
  2. Java多线程之捕获异常
  3. Linux Socket TCP/IP通信
  4. Asp.net動態添加控件(转)
  5. python中requests的用法总结
  6. 【第一季】CH07_FPGA_RunLED创建VIVADO工程实验
  7. easyUI的combobox设置隐藏和显示
  8. 【HDU2795】Billboard(线段树)
  9. 【转】Hibernate数据过滤
  10. 【转载】Python日期时间模块datetime详解与Python 日期时间的比较,计算实例代码