文档章节

What is Spring IoC ?

xyxo
 xyxo
发布于 2012/11/07 13:54
字数 667
阅读 128
收藏 0

Before going through the Spring IoC tutorial, we must know what IoC is and why we use it in real life project development.

IoC or Inversion of Control, is also known as Dependency Injection, is used to create loosely coupled software components. Here, loosely coupled means less dependent software components. Let us illustrate it with an example:

There is a Report class used to generate reports of a bank account and ReportFileWriter is a class that creates a file and writes the report data to the file. Data is provided by the Report class to ReportFileWriter and in response it returns the file instance containing the report. So Report class is dependent on ReportFileWriter class. The dependency graph will be as follow:

Dependency Graph

Dependency Graph

In this situation Report class will create an instance of class ReportFileWriter and will call the method “createReport” by passing report data:

ReportFileWriter fw = new ReportFileWriter();
fw.createReport(data);

If you think carefully, in this code we are using tight coupled dependency between Report class and ReportFileWriter class. That means if we want to change the file writer class to PDFReportFileWriter in future we have to change the code of Report class. They are tightly coupled.

IoC solves the problem of tight couple and helps to makes the software component loosely coupled, so that we can maintain our code pretty much easily.

IoC or Inversion of Control is a design pattern that provided instances of a bean or class to another when they needed. That means, we do not have to use new keyword to create instance of dependent class. We will just create a reference of dependent class and IoC pattern will set the instance to the reference when needed.

Now, using IoC Report class will have following code :

ReportFileWriter fw = null;

Public void setFw(ReportFileWriter fw){
    this.fw = fw;
}

IoC container will call setter method to set the instance of ReportFileWriter class. We have to configure the dependency in our classes in IoC configuration file. That will tell the IoC container when and how to inject the dependences.

Creating a better loosely coupled model

In this session we will see how to IoC to create effective loosely coupled components. Consider the previous example of Report class. There is a interface named “ReportWriter” which contains following definition:

public interface ReportWriter {
    public File createReport(ReportData data);
}

Any class implementing ReportWriter interface will be able to create report file of its own format. The report class contains following code:

private ReportWriter reportWriter = null;

public void setReportWriter(ReportWriter rw){
    this.reportWriter = rw;
}

//More Code
reportWriter.createReport(data);
//More Code

Now, we want to provide a facility to generate report in two formats, in PDF and in MS Word. So we will create two classes PDFReportWrite and MSWordReportWriter which will implement the interface ReportWriter. We will configure our IoC container in such a way that when we will need a PDF Report the IoC container will create instance of PDFReportWrite and set to ReportWriter reference of Report class and if we need MS Word format then IoC container will create instance of MSWordReportWriter and set to ReportWriter reference of Report class. No more we have to change the code of Report class to generate a different format report. This is the advantage of IoC design pattern.

Types of Dependency Injection:

There are three ways in which a IoC container can inject dependency class instance to the dependent class:

  1. Setter Injection: In this way, IoC container uses setter of the reference to set the instance of dependency class. Spring IoC provides setter Injection facility. We will learn about it with proper examples in next tutorial.
  2. Constructor Injection: In constructor injection IoC container users construction of dependent class to set the instance of dependency class. We will see example of constructor injection in upcoming tutorials using Spring IoC framework.
  3. Interface Injection: It is more complex process of dependency injection in which interfaces are used to inject the instances. Spring IoC container does not support this type of injection.

本文转载自:http://www.raistudies.com/spring/what-is-spring-ioc/

xyxo

xyxo

粉丝 8
博文 19
码字总数 5259
作品 0
深圳
程序员
私信 提问
【死磕 Spring】—– IOC 之 IOC 初始化总结

原文出自:http://cmsblogs.com 前面 13 篇博文从源码层次分析了 IOC 整个初始化过程,这篇就这些内容做一个总结将其连贯起来。 在前文提过,IOC 容器的初始化过程分为三步骤:Resource 定位...

chenssy
2018/10/09
0
0
Spring 的基本应用:IoC 和 AOP

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 https://blog.csdn.net/GitChat/article/details/97292080 前言spring 对于任何一个 Java 开...

GitChat技术杂谈
07/25
0
0
那些年,我们一起追的Spring

学无止境,但仍需及时总结。 自去年开始写作以来,写了一些关于Spring的文章,今天将它们汇总起来,一方面方便大家阅读,另一方面,也是一次小的复盘总结。 IOC 首先是Spring的IOC,也就是控...

SexyCode
2018/08/14
0
0
三条路线告诉你如何掌握Spring IoC容器的核心原理

一、前言 前三篇已经从历史的角度和大家一起探讨了为什么会有Spring,Spring的两个核心概念:IoC和AOP的雏形,Spring的历史变迁和如今的生态帝国。本节的主要目的就是通过一个切入点带大家一...

Java小铺
2018/08/27
0
0
Spring应用学习——IOC

Spring简介 1. Spring的出现是为了取代EJB(Enterprise JavaBean)的臃肿、低效、脱离现实的缺点。Spring致力于J2EE应用的各层(表现层、业务层、持久层)的解决方案,Spring是企业应用开发的...

江左煤郎
2018/11/16
42
0

没有更多内容

加载失败,请刷新页面

加载更多

开发中常用的正则表达式

为了能够更好地理解如何在C#环境中使用正则表达式,这里整理了一些常用的正则表达式: 罗马数字: string p1 = "^m*(d?c{0,3}|c[dm])" + "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$"; string t1 =......

木庄
16分钟前
4
0
【.NET程序打包】VS2019使用Installer Projects打包

C#—使用Installer Projects打包桌面应用程序 前言 打包桌面应用程序实在是一个不常使用的东西,偶尔使用起来经常会忘东忘西的耽误时间,因此,这篇文章多以图片记录过程,也是用于备忘。 下...

_Somuns
20分钟前
4
0
自定义注解,使用动态代理解决网站的字符集编码问题

第1章设置环境 安装操作系统,安装备份(镜像): JDK: 设置环境变量Eclipse:解压即可 Eclipse自身解压目录不包括中文 代码工作空间目录不包括中文Tomcat:解压不要包含中文目录M...

蓝来杯往
25分钟前
3
0
Solr中的字段类型field type

Solr含有多种字段类型,可用的字段类型基本都定义在了包org.apache.solr.schema中,列举如下: 类 说明 BinaryField 二进制数据 BoolField 布尔值,其中’t’/’T’/’1’都是true Collatio...

gantaos
38分钟前
4
0
《JAVA核心知识》学习笔记 (21. JAVA 算法)

21. JAVA 算法

Shingfi
45分钟前
4
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部