上一篇博客我们主要讲解了IOC控制反转,也就是说IOC 让程序员不在关注怎么去创建对象,而是关注与对象创建之后的操作,把对象的创建、初始化、销毁等工作交给spring容器来做。那么创建对象的时候,有可能依赖于其他的对象,即类的属性如何赋值?这也是我们这篇博客讲解 Spring 另一个核心要点:DI依赖注入。

  PS:本篇博客源码链接:https://pan.baidu.com/s/1kjx5oZRtKjBVXd7OdFPcJQ 密码:s3e1

1、什么是DI依赖注入?

  spring动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了 spring我们就只需要告诉spring,A中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。在系统运行时,spring会在适当的时候制造一个Connection,然后像打针一样,注射到A当中,这样就完成了对各个对象之间关系的控制。A需要依赖 Connection才能正常运行,而这个Connection是由spring注入到A中的,依赖注入的名字就这么来的。那么DI是如何实现的呢? Java 1.3之后一个重要特征是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,spring就是通过反射来实现注入的。

  简单来说什么是依赖注入,就是给属性赋值(包括基本数据类型和引用数据类型)

2、利用 set 方法给属性赋值

  第一步:创建工程,并导入相应的 jar 包

  第二步:创建实体类 Person

package com.ys.di;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Person {
private Long pid;
private String pname;
private Student students;
private List lists;
private Set sets;
private Map maps;
private Properties properties; public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Student getStudents() {
return students;
}
public void setStudents(Student students) {
this.students = students;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
} }

  我们看到这个实体类包括引用类型 Student 类,基本数据类以及集合数据类型。

  第三步:在 applicationContext.xml 中进行赋值

    <!--
property是用来描述一个类的属性
基本类型的封装类、String等需要值的类型用value赋值
引用类型用ref赋值
-->
<bean id="person" class="com.ys.di.Person">
<property name="pid" value="1"></property>
<property name="pname" value="vae"></property>
<property name="students">
<ref bean="student"/>
</property> <property name="lists">
<list>
<value>1</value>
<ref bean="student"/>
<value>vae</value>
</list>
</property> <property name="sets">
<set>
<value>1</value>
<ref bean="student"/>
<value>vae</value>
</set>
</property> <property name="maps">
<map>
<entry key="m1" value="1"></entry>
<entry key="m2" >
<ref bean="student"/>
</entry>
</map>
</property> <property name="properties">
<props>
<prop key="p1">p1</prop>
<prop key="p2">p2</prop>
</props>
</property> </bean> <bean id="student" class="com.ys.di.Student"></bean>

  第四步:测试

//利用 set 方法给对象赋值
@Test
public void testSet(){
//1、启动 spring 容器
//2、从 spring 容器中取出数据
//3、通过对象调用方法
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person");
System.out.println(person.getPname());//vae
}

  

3、利用 构造函数 给属性赋值

  第一步:在实体类 Per'son.java 中添加两个构造方法:有参和无参

//默认构造函数
public Person(){}
//带参构造函数
public Person(Long pid,Student students){
this.pid = pid;
this.students = students;
}

  第二步:在 applicationContext.xml 中进行赋值

<!-- 根据构造函数赋值 -->
<!--
index 代表参数的位置 从0开始计算
type 指的是参数的类型,在有多个构造函数时,可以用type来区分,要是能确定是那个构造函数,可以不用写type
value 给基本类型赋值
ref 给引用类型赋值
-->
<bean id="person_con" class="com.ys.di.Person">
<constructor-arg index="0" type="java.lang.Long" value="1">
</constructor-arg>
<constructor-arg index="1" type="com.ys.di.Student" ref="student_con"></constructor-arg>
</bean>
<bean id="student_con" class="com.ys.di.Student"></bean>

  第三步:测试

//利用 构造函数 给对象赋值
@Test
public void testConstrutor(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) context.getBean("person_con");
System.out.println(person.getPid());//1
}

  

  总结:

  1、如果spring的配置文件中的bean中没有<constructor-arg>该元素,则调用默认的构造函数

  2、如果spring的配置文件中的bean中有<constructor-arg>该元素,则该元素确定唯一的构造函数

Spring详解(三)------DI依赖注入的更多相关文章

  1. 深入浅出spring IOC中三种依赖注入方式

    深入浅出spring IOC中三种依赖注入方式 spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和 ...

  2. Spring IOC(三)依赖注入

    本系列目录: Spring IOC(一)概览 Spring IOC(二)容器初始化 Spring IOC(三)依赖注入 Spring IOC(四)总结 目录 1.AbstractBeanFactory ...

  3. 【SSH系列】深入浅出spring IOC中三种依赖注入方式

    spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则来消减计算机程序的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入什么?控 ...

  4. spring IOC中三种依赖注入方式

    Spring的核心思想是IOC和AOP,IOC-控制反转,是一个重要的面向对象编程的法则,用来消减计算机程序之间的耦合问题,控制反转一般分为两种类型,依赖注入和依赖查找,依赖什么?为什么需要依赖?注入 ...

  5. Spring学习02(DI依赖注入)

    5.依赖注入(Dependency Injection,DI) 5.1 概念 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器 ...

  6. [心得笔记]spring常用的三种依赖注入方式

    一.目前使用最广泛的 @Autowired:自动装配 基于@Autowired的自动装配,默认是根据类型注入,可以用于构造器.接口.方法注入,使用方式如下: @Autowired 构造方法.方法.接口 ...

  7. 三大框架 之 Spring(IOC控制反转、DI依赖注入)

    目录 常用词汇 left join与left outer join的区别 Struts2的标签库导入 Spring Spring概述 什么是Spring spring特点 下载 IOC 什么IOC 传 ...

  8. Spring的三大核心思想:IOC(控制反转),DI(依赖注入),AOP(面向切面编程)

    Spring核心思想,IoC与DI详解(如果还不明白,放弃java吧) 1.IoC是什么?    IoC(Inversion of Control)控制反转,IoC是一种新的Java编程模式,目前很多 ...

  9. 学习Spring IOC控制反转和DI依赖注入总结

    30岁的小曹,20岁的身体,还在坚持在能力允许控制范围内22点睡觉,5点起床锻炼身体,好好学习,除了加班或者像今天这样的深夜,再一次写已经有X百万人写过的 spring Ioc 的总结博客. 一.IO ...

随机推荐

  1. 全面理解SSD和NAND Flash

    Flash Memory又叫做闪存,是一种非易失性存储器.非易失性是指断电之后数据不会丢失,这里就涉及到断电保护(后面详细讲解). 总体思路 1.NAND Flash的用途. 2.NAND Flash ...

  2. oracle分组-神奇的cube和rollup

    先看代码: 表结构如下: emp表 EMPNO                                     NOT NULL NUMBER(4) ENAME                 ...

  3. html中ul元素水平排列问题

    <!DOCTYPE html> <html> <head> <style> #pic_list { display:block; white-space ...

  4. [js高手之路] es6系列教程 - 函数的默认参数详解

    在ES6之前,我们一般用短路表达式处理默认参数 function show( a, b ){ var a = a || 10; var b = b || 20; console.log( a, b ) ...

  5. K个最近的点

    前段时间在网上看到一些准备找工作的人会在LintCode上刷题,然后我今天上去看了一下,也打算开始做题,然后把每天做的题目和以后的优化记录下来. 2017年8月6日 21:17:27 第一题: 描述: ...

  6. 2016 "Bird Cup" ICPC7th@ahstu--“波导杯”安徽科技学院第七届程序设计大赛

    "波导杯"安徽科技学院第七届程序设计大赛 原文章网页 Contest - 2016 "Bird Cup" ICPC7th@ahstu Start time:   ...

  7. 【NO.12-1】Jmeter - 在Linux执行性能测试的方法 [1]

    前面讲过在Windows执行性能测试的方法,就是这篇了<jmeter - 一个完整的接口测试的脚本>, 在Windows执行性能测试之前,首先要有1个性能测试脚本嘛, 但是这个性能测试脚本 ...

  8. .net core 2.0学习笔记(一):开发运行环境搭建

    期待已久的.net core 2.0终于发布了!大家等的花儿都谢了. 不过比预期提前了一个多月,这在微软历史上还真的不多见.按照历史经验看,2.0版本应该比较靠谱,我猜这也是社区非常火爆的原因吧.下面 ...

  9. Oracle安装oraInventory问题

    Oracle安装oraInventory问题-----------------------------2013/10/15 在使用安装Oracle软件或者使用dbca创建数据库时,所有的日志都会放在o ...

  10. eclipse中配置spring环境

    初识Spring框架 1.简单使用 eclipse中配置Spring环境,如果是初学的话,只需要在eclipse中引入几个jar包就可以用了, 在普通java project项目目录下,建一个lib文 ...