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

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

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

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

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

构造函数注入

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

Car.java

package com.itzn.model;
import java.util.Date;
public class Car {
private String name;
private long price;
private Date buyDate; public Car(String _name, long _price, Date _buyDate) {
name = _name;
price = _price;
buyDate = _buyDate;
} public void MyStr() {
System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
}
}

bean.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="car" class="com.itzn.model.Car">
<constructor-arg name="_name" value="逍客"></constructor-arg>
<constructor-arg name="_price" value=""></constructor-arg>
<constructor-arg name="_buyDate" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
</beans>

SpringTest.java

public class SpringTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Car car=(Car) ac.getBean("car");
car.MyStr(); }
}

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

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

涉及的标签:

constructor-arg

属性:

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

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

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

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

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

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

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

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

set 方法注入

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

Car2.java

package com.itzn.model;
import java.util.Date;
public class Car2 {
private String name;
private long price;
private Date buyDate; public void setName(String name) {
this.name = name;
}
public void setPrice(long price) {
this.price = price;
}
public void setBuyDate(Date buyDate) {
this.buyDate = buyDate;
}
public void MyStr() {
System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
}
}

bean.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="car" class="com.itzn.model.Car">
<constructor-arg name="_name" value="逍客"></constructor-arg>
<constructor-arg name="_price" value=""></constructor-arg>
<constructor-arg name="_buyDate" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
<!--set注入-->
<bean id="dasauto" class="com.itzn.model.Car2">
<property name="name" value="朗逸"></property>
<property name="price" value=""></property>
<property name="buyDate" ref="now"></property>
</bean>
</beans>

SpringTest.java

package com.itzn.test;

import com.itzn.model.Car;
import com.itzn.model.Car2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Car car=(Car) ac.getBean("car");
Car2 car2=(Car2) ac.getBean("dasauto");
car.MyStr();
car2.MyStr();
}
}

使用 set 方法的方式

涉及的标签:

property

属性:

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

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

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

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

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

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

集合属性注入

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

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

Car3.java

package com.itzn.model;
import java.util.*;
public class Car3 {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs; }
public void setMyList(List<String> myList) {
this.myList = myList; }
public void setMySet(Set<String> mySet) {
this.mySet = mySet; }
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap; }
public void setMyProps(Properties myProps) {
this.myProps = myProps; } public void MyStr() {
System.out.println(Arrays.toString(myStrs));
System.out.println(myList);
System.out.println(mySet);
System.out.println(myMap);
System.out.println(myProps);
}
}

bean.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="car" class="com.itzn.model.Car">
<constructor-arg name="_name" value="逍客"></constructor-arg>
<constructor-arg name="_price" value=""></constructor-arg>
<constructor-arg name="_buyDate" ref="now"></constructor-arg>
</bean>
<bean id="now" class="java.util.Date"></bean>
<!--set注入-->
<bean id="dasauto" class="com.itzn.model.Car2">
<property name="name" value="朗逸"></property>
<property name="price" value=""></property>
<property name="buyDate" ref="now"></property>
</bean> <!--集合属性注入-->
<bean id="autos" class="com.itzn.model.Car3">
<!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
<!-- 给数组注入数据 -->
<property name="myStrs">
<set>
<value>一汽大众</value>
<value>上海大众</value>
<value>一汽奥迪</value>
</set>
</property>
<!-- 注入 list 集合数据 -->
<property name="myList">
<array>
<value>奔驰</value>
<value>宝马</value>
<value>奥迪</value>
</array>
</property>
<!-- 注入 set 集合数据 -->
<property name="mySet">
<list>
<value>丰田</value>
<value>本田</value>
<value>日产</value>
</list>
</property>
<!-- 注入 Map 数据 -->
<property name="myMap">
<props>
<prop key="dasauto">大众迈腾</prop>
<prop key="aodi">A6L</prop>
</props>
</property>
<!-- 注入 properties 数据 -->
<property name="myProps">
<map>
<entry key="keyA" value="aaa"> </entry>
<entry key="keyB">
<value>bbb</value>
</entry>
</map>
</property>
</bean>
</beans>

SpringTest.java

package com.itzn.test;
import com.itzn.model.Car;
import com.itzn.model.Car2;
import com.itzn.model.Car3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
Car car=(Car) ac.getBean("car");
Car2 car2=(Car2) ac.getBean("dasauto");
Car3 car3=(Car3) ac.getBean("autos");
car.MyStr();
car2.MyStr();
car3.MyStr();
}
}

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

用于给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. UE项目打包

    https://docs.unrealengine.com/zh-CN/Engine/Basics/Projects/Packaging/index.html 必须先对虚幻项目进行正确打包,之后才能将 ...

  2. [转帖]推荐一款比 Find 快 10 倍的搜索工具 FD

    推荐一款比 Find 快 10 倍的搜索工具 FD https://www.hi-linux.com/posts/15017.html 试了下 很好用呢. Posted by Mike on 2018 ...

  3. Weave跨主机实现docker互通,固定ip及dns使用介绍

    一.weave介绍Weave是由weaveworks公司开发的解决Docker跨主机网络的解决方案,现在就采用它来实现Docker多宿主机互联的目的,它能够创建一个虚拟网络,用于连接部署在多台主机上的 ...

  4. 48 容器(七)——HashMap底层:哈希表结构与哈希算法

    哈希表结构 哈希表是由数组+链表组成的,首先有一个数组,数组的每一个位置都用来存储一个链表,链表的基本节点为:[hash值,key值,value值,next],当存入一个键值对时,首先调用hashco ...

  5. REST framework之分页组件

    REST framework之分页组件 一 简单分页 查看第n页,每页显示n条 from rest_framework.pagination import PageNumberPagination # ...

  6. shell中if条件字符串、数字比对,[[ ]]和[ ]区别

    目录 shell 括号 test 和 []符号 [[]] 符号 let和(())符号 "[]" , "[[]]" 和 "(())"对比 sh ...

  7. 一个长耗时SQL在TiDB和Mysql上的耗时测试

    之前看到的TiDB和MySql的性能对比都是大量短耗时请求下的压测,单机情况下TiDB和MySql的确有些差距,不过笔者最近碰到的场景更多是sql要扫描的行数不小的情况下单sql比较耗时的问题,所以自 ...

  8. flink ETL数据处理

    Flink ETL 实现数据清洗   一:需求(针对算法产生的日志数据进行清洗拆分) 1. 算法产生的日志数据是嵌套json格式,需要拆分 2.针对算法中的国家字段进行大区转换 3.最后把不同类型的日 ...

  9. 【LEETCODE】49、数组分类,简单级别,题目:566,1089

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

  10. 解决跟Docker私有仓库登陆,推送,拉取镜像出现的报错

    出现问题:Error response from daemon: Get https://192.168.186.120/v1/users/: dial tcp 192.168.186.120:443 ...