依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。

我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。

ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。

那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。

简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。

构造函数注入

顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。具体代码如下:

Car.java

  1. package com.itzn.model;
  2. import java.util.Date;
  3. public class Car {
  4. private String name;
  5. private long price;
  6. private Date buyDate;
  7.  
  8. public Car(String _name, long _price, Date _buyDate) {
  9. name = _name;
  10. price = _price;
  11. buyDate = _buyDate;
  12. }
  13.  
  14. public void MyStr() {
  15. System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
  16. }
  17. }

bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--构造函数注入-->
  7. <bean id="car" class="com.itzn.model.Car">
  8. <constructor-arg name="_name" value="逍客"></constructor-arg>
  9. <constructor-arg name="_price" value=""></constructor-arg>
  10. <constructor-arg name="_buyDate" ref="now"></constructor-arg>
  11. </bean>
  12. <bean id="now" class="java.util.Date"></bean>
  13. </beans>

SpringTest.java

  1. public class SpringTest {
  2. public static void main(String[] args) {
  3. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  4. Car car=(Car) ac.getBean("car");
  5. car.MyStr();
  6.  
  7. }
  8. }

使用构造函数的方式,要求:

类中需要提供一个对应参数列表的构造函数。

涉及的标签:

constructor-arg

属性:

index:指定参数在构造函数参数列表的索引位置

type:指定参数在构造函数中的数据类型

name:指定参数在构造函数中的名称 用这个找给谁赋值

=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============

value:它能赋的值是基本数据类型和 String 类型

ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean

优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。

弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。

set 方法注入

顾名思义,就是在类中提供需要注入成员的 set 方法。具体代码如下:

Car2.java

  1. package com.itzn.model;
  2. import java.util.Date;
  3. public class Car2 {
  4. private String name;
  5. private long price;
  6. private Date buyDate;
  7.  
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public void setPrice(long price) {
  12. this.price = price;
  13. }
  14. public void setBuyDate(Date buyDate) {
  15. this.buyDate = buyDate;
  16. }
  17. public void MyStr() {
  18. System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
  19. }
  20. }

bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--构造函数注入-->
  7. <bean id="car" class="com.itzn.model.Car">
  8. <constructor-arg name="_name" value="逍客"></constructor-arg>
  9. <constructor-arg name="_price" value=""></constructor-arg>
  10. <constructor-arg name="_buyDate" ref="now"></constructor-arg>
  11. </bean>
  12. <bean id="now" class="java.util.Date"></bean>
  13. <!--set注入-->
  14. <bean id="dasauto" class="com.itzn.model.Car2">
  15. <property name="name" value="朗逸"></property>
  16. <property name="price" value=""></property>
  17. <property name="buyDate" ref="now"></property>
  18. </bean>
  19. </beans>

SpringTest.java

  1. package com.itzn.test;
  2.  
  3. import com.itzn.model.Car;
  4. import com.itzn.model.Car2;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. public class SpringTest {
  9. public static void main(String[] args) {
  10. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  11. Car car=(Car) ac.getBean("car");
  12. Car2 car2=(Car2) ac.getBean("dasauto");
  13. car.MyStr();
  14. car2.MyStr();
  15. }
  16. }

使用 set 方法的方式

涉及的标签:

property

属性:

name:找的是类中 set 方法后面的部分

ref:给属性赋值是其他 bean 类型的

value:给属性赋值是基本数据类型和 string 类型的

实际开发中,此种方式用的较多。

优势:创建对象时没有明确的限制,可以直接使用默认构造函数

弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行。

集合属性注入

顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。

我们这里介绍注入数组,List,Set,Map,Properties。具体代码如下:

Car3.java

  1. package com.itzn.model;
  2. import java.util.*;
  3. public class Car3 {
  4. private String[] myStrs;
  5. private List<String> myList;
  6. private Set<String> mySet;
  7. private Map<String,String> myMap;
  8. private Properties myProps;
  9. public void setMyStrs(String[] myStrs) {
  10. this.myStrs = myStrs; }
  11. public void setMyList(List<String> myList) {
  12. this.myList = myList; }
  13. public void setMySet(Set<String> mySet) {
  14. this.mySet = mySet; }
  15. public void setMyMap(Map<String, String> myMap) {
  16. this.myMap = myMap; }
  17. public void setMyProps(Properties myProps) {
  18. this.myProps = myProps; }
  19.  
  20. public void MyStr() {
  21. System.out.println(Arrays.toString(myStrs));
  22. System.out.println(myList);
  23. System.out.println(mySet);
  24. System.out.println(myMap);
  25. System.out.println(myProps);
  26. }
  27. }

bean.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--构造函数注入-->
  7. <bean id="car" class="com.itzn.model.Car">
  8. <constructor-arg name="_name" value="逍客"></constructor-arg>
  9. <constructor-arg name="_price" value=""></constructor-arg>
  10. <constructor-arg name="_buyDate" ref="now"></constructor-arg>
  11. </bean>
  12. <bean id="now" class="java.util.Date"></bean>
  13. <!--set注入-->
  14. <bean id="dasauto" class="com.itzn.model.Car2">
  15. <property name="name" value="朗逸"></property>
  16. <property name="price" value=""></property>
  17. <property name="buyDate" ref="now"></property>
  18. </bean>
  19.  
  20. <!--集合属性注入-->
  21. <bean id="autos" class="com.itzn.model.Car3">
  22. <!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
  23. <!-- 给数组注入数据 -->
  24. <property name="myStrs">
  25. <set>
  26. <value>一汽大众</value>
  27. <value>上海大众</value>
  28. <value>一汽奥迪</value>
  29. </set>
  30. </property>
  31. <!-- 注入 list 集合数据 -->
  32. <property name="myList">
  33. <array>
  34. <value>奔驰</value>
  35. <value>宝马</value>
  36. <value>奥迪</value>
  37. </array>
  38. </property>
  39. <!-- 注入 set 集合数据 -->
  40. <property name="mySet">
  41. <list>
  42. <value>丰田</value>
  43. <value>本田</value>
  44. <value>日产</value>
  45. </list>
  46. </property>
  47. <!-- 注入 Map 数据 -->
  48. <property name="myMap">
  49. <props>
  50. <prop key="dasauto">大众迈腾</prop>
  51. <prop key="aodi">A6L</prop>
  52. </props>
  53. </property>
  54. <!-- 注入 properties 数据 -->
  55. <property name="myProps">
  56. <map>
  57. <entry key="keyA" value="aaa">
  58.  
  59. </entry>
  60. <entry key="keyB">
  61. <value>bbb</value>
  62. </entry>
  63. </map>
  64. </property>
  65. </bean>
  66. </beans>

SpringTest.java

  1. package com.itzn.test;
  2. import com.itzn.model.Car;
  3. import com.itzn.model.Car2;
  4. import com.itzn.model.Car3;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7.  
  8. public class SpringTest {
  9. public static void main(String[] args) {
  10. ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
  11. Car car=(Car) ac.getBean("car");
  12. Car2 car2=(Car2) ac.getBean("dasauto");
  13. Car3 car3=(Car3) ac.getBean("autos");
  14. car.MyStr();
  15. car2.MyStr();
  16. car3.MyStr();
  17. }
  18. }

复杂类型的注入/集合类型的注入

用于给List结构集合注入标签:

list array set

用于Map结构集合注入的标签:

map props

结构相同,标签可以互换

你喜欢的代码:点击下载

04 Spring的依赖注入的更多相关文章

  1. 04 Spring框架 依赖注入(一)

    整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...

  2. (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)

    Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...

  3. Spring的依赖注入(DI)三种方式

    Spring依赖注入(DI)的三种方式,分别为: 1.  接口注入 2.  Setter方法注入 3.  构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...

  4. spring的依赖注入的最常见的两种方法

    package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...

  5. 一步一步深入spring(3)--spring的依赖注入方式

    对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...

  6. spring的依赖注入是什么意思

    最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...

  7. SpringBoot系列: 理解 Spring 的依赖注入(一)

    ==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...

  8. Spring.NET依赖注入框架学习--实例化容器常用方法

    Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...

  9. Spring.NET依赖注入框架学习--简单对象注入

    Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...

随机推荐

  1. springboot2 配置 https

    package cn.xiaojf.aibus.configure; import org.apache.catalina.Context; import org.apache.catalina.co ...

  2. ansible的基础使用(一)

    ansible基础使用(一) ansible的主要功能 A:为什么是ansible B:ansible的安装 C:ansible的相关文件 D:ansible的基本使用 ansible的简单操作 A: ...

  3. 在ensp中RSTP基础设置

    为什么我们要有rstp? rstp就是stp的加强版 实验模拟内容 搭建拓扑 相关参数(实验的时候看看自己的mac地址可能与我的并不同) 我们开始配置RSTP基本功能,由于交换机默认开启MSTP,所有 ...

  4. 小程序云函数,解决接口https问题

    本实例只是简单记录http请求 1,云函数如下 // 云函数入口函数 exports.main = async (event, context) => { let req = await got ...

  5. Docker部署ELK 7.0.1集群之Elasticsearch安装介绍

    elk介绍这里不再赘述,本系列教程多以实战干货为主,关于elk工作原理介绍,详情查看官方文档. 一.环境规划 主机名 IP 角色 节点名 centos01 10.10.0.10 es node-10 ...

  6. c++基础(四)—— 泛型算法

    1.find(first, last, value) 头文件:algorithm 参数:前两个参数是“表示元素范围的迭代器”,第三个是一个值 说明:find 将范围中进行寻找.搜索失败:如果范围中无匹 ...

  7. 深度探索MySQL主从复制原理

    深度探索MySQL主从复制原理 一 .概要 MySQL Replication (MySQL 主从复制) 是什么? 为什么要主从复制以及它的实现原理是什么? 1.1 MySQL 主从复制概念 MySQ ...

  8. 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  9. [洛谷P4052][JSOI2007]文本生成器

    题目大意:有$n$个字符串$s_i$,问有多少个长度为$m$的字符串至少包含$n$个字符串中的一个,字符集 A-Z .$s_i,m\leqslant100,n\leqslant60$ 题解:$AC$自 ...

  10. 有助于改善性能的Java代码技巧

    前言 程序的性能受到代码质量的直接影响.这次主要介绍一些代码编写的小技巧和惯例.虽然看起来有些是微不足道的编程技巧,却可能为系统性能带来成倍的提升,因此还是值得关注的. 慎用异常 在Java开发中,经 ...