好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring依赖注入和控制反转详情

前言:

在我们开始做任何事情之前,让我们先了解一下什么是控制反转。学习依赖注入和控制反转的概念,然后借助代码示例了解 Spring 框架如何支持它们。

控制反转

在我们开始做任何事情之前,让我们先了解一下什么是控制反转。

控制反转是面向对象编程中使用的术语,通过该术语,对象或对象集的控制权被赋予框架或由框架提供的容器。

虽然上面的图片是幽默的,但它描述了什么是控制反转。如果我们将人类视为软件组件或服务,他们的目的是执行诸如起床、开会或支付账单等行为。对于其他事情,例如跟踪会议,设置警报或提醒人们使用电话或任何智能设备。

什么是依赖?

一个应用程序由多个类组成。通常,每个类都应该有自己的专门职责。这导致我们的类与不同的类集成以完成某些功能。当A类调用B类的方法时,A类依赖B类。

紧耦合对象

了解依赖项如何导致紧耦合对象问题。请参阅下面的代码。

这是一个FileUploadService抓取文件,检查文件是否具有预期扩展名之一并要求 aFileStorageService存储文件。

?

1

2

3

4

5

6

7

8

9

10

11

< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >公共类 FileUploadService {</ font ></ font >< font ></ font >

  < font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

     private List< String > validFiles = Arrays.asList("xls", "doc"."txt", "ppt");</ font ></ font >< font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

     私有 FileStorageService 服务 = 新 AzureBlobStorageService();</ font ></ font >< font ></ font >

  < font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

     公共文件上传服务(){}</ font ></ font >< font ></ font >

  < font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

     //</ font ></ font >< font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

     // 其他方法</ font ></ font >< font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

     //</ font ></ font >< font ></ font >< font style = "vertical-align: inherit;" >< font style = "vertical-align: inherit;" >

}</ font ></ font >< font ></ font >

在上面的代码中,我们使用Program to Interface原理来实例化FileStorageService. 但是,相应的实现仍然是在类中硬编码的。也是validFiles硬编码的。它们都导致了紧耦合对象。

松散耦合对象

让我们稍微更新FileUploadService一下,我们将得到松散耦合的对象。

第 3 行: 变量已声明但未初始化。没有硬编码值。 第 4 行: 仅对FileStorageService类型的引用。没有附加实现。 第 6 行: 所有参数构造函数。

让我们看看User课堂上发生了什么,实际上是FileUploadService.

第 17 行: FileUploadService通过将所有必需的参数传递给构造函数来创建实例。

依赖注入

我们刚刚所做的称为依赖注入。

依赖注入是面向对象编程中使用的一个术语,通过它,对象将专注于执行分配的功能并利用其他对象。对象不会处理必要的配置和初始化。但是,对象将提供一种通过字段分配、字段设置器或构造函数来初始化它们及其依赖关系的方法。这样,外部实体可以初始化事物而不是实际对象。

在基于 Spring 的应用程序中,Inversion of Control Container(IoC 容器)执行依赖注入。我们将在接下来的部分中看到这一点。首先,让我们看看为什么我们甚至需要这样一个容器。

为什么我们需要 IoC 容器?

我已经修改了前面的代码示例。它现在是一个ResumeUploaderService. ACandidate可以将其简历分享给ResumeUploaderService. 该服务应在验证扩展后,将其共享给ResumeStorageService. 根据组织当前的策略,简历被存储在文件系统的机密文件夹中(by

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public class ResumeUploaderService {<font></font>

  <font></font>

     private List<String> validFiles;<font></font>

     private ResumeStorageService service;<font></font>

  <font></font>

     public ResumeUploaderService(List<String> validFiles, ResumeStorageService service) {<font></font>

         this .validFiles = validFiles;<font></font>

         this .service = service;<font></font>

     }<font></font>

}<font></font>

  <font></font>

  <font></font>

class Candidate {<font></font>

     public static void main(String[] ar) {<font></font>

         List<String> validFiles = Arrays.asList( "pdf" , "doc" );<font></font>

  <font></font>

         String filePath = "/Users/app/confidential/storage/resume" ;<font></font>

         ResumeStorageService service = new FileSystemResumeStorageService(filePath);<font></font>

  <font></font>

         ResumeUploaderService fileUploadService = new ResumeUploaderService(validFiles, service);<font></font>

     }<font></font>

}<font></font>

第 4 行: ResumeUploaderService 具有对ResumeStorageService.

第 6 行: 接受并设置ResumeStorageService.

要上传简历,Candidate必须实例化ResumeUploaderService并传递简历。但随着这一切dependency injection,候选人的工作变得困难。候选者不仅要实例化ResumeUploaderService,还要实例化ResumeStorageService. 因为,没有后者,前者就无法实例化。

第 17 行: 候选人决定将简历存储在哪里(我知道……这很有趣!!) 第 18 行: 候选人决定是否使用FileSystemResumeStorageService或AzureBlobStorageService。 第 20 行: 最后,候选实例化ResumeUploaderService.

以下是上面的重要问题

消费者知道的太多了。 消费者,而不是使用服务,也初始化它。 消费者不应该担心如何ResumeUploaderService完成它的工作(缺乏抽象)。 作为最终消费者,我们必须了解所有内容,并且必须初始化系统中的所有内容。

这清楚地表明,我们需要一些可以处理所有配置和初始化的东西。某些东西,其唯一职责是管理初始化。

控制容器反转(IoC 容器)

Spring 提供了一个 IoC Container 来解决这个问题。这个容器实例化了所有的对象,同时它也解决了它们的依赖关系。该类ApplicationContext代表 Spring IOC 容器。应用程序上下文负责实例化、配置和连接 bean。

请记住,Bean 只不过是在 Spring 的应用程序上下文中注册的 Java 对象。

要配置、实例化或编写 bean,应用程序上下文需要一些指令。这些指令可以以 XML 配置、Java 注释或代码的形式提供。

Spring 依赖注入

在Spring中,每个对象都是一个 bean。每个对象都有一个id或name。ApplicationContext跟踪所有这些 bes 和 id 。当消费者请求 bean 时,应用程序上下文返回 bean 的一个实例。查看下面的代码以详细了解 bean 创建和布线。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

import org.springframework.beans.factory.annotation.Value;<font></font>

import org.springframework.stereotype.Component;<font></font>

  <font></font>

@Component ( "resumeStorageService" )<font></font>

public class FileSystemResumeStorageService implements ResumeStorageService {<font></font>

  <font></font>

     @Value ( "${resume.storage.path}" )<font></font>

     private String storagePath;             // Storage path assigned based on properties file<font></font>

  <font></font>

     //<font></font>

     // Skipped methods<font></font>

     //<font></font>

}<font></font>

第 4 行: 告诉 Spring 将此类注册为 Bean 并通过给定名称识别它。如果未提供名称,则将类名视为标识符。 第 8 行: 存储路径现在直接从属性文件中注入。消费者无需通过它。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

import org.springframework.beans.factory.annotation.Autowired;<font></font>

import org.springframework.beans.factory.annotation.Qualifier;<font></font>

import org.springframework.stereotype.Component;<font></font>

  <font></font>

@Component <font></font>

public class ResumeUploaderService {<font></font>

  <font></font>

     @Autowired <font></font>

     @Qualifier ( "resumeStorageService" )<font></font>

     private ResumeStorageService storageService;<font></font>

  <font></font>

  <font></font>

     public ResumeUploaderService(ResumeStorageService storageService) {<font></font>

         this .storageService = storageService;<font></font>

     }<font></font>

  <font></font>

     //<font></font>

     // Skipped methods<font></font>

     //<font></font>

}<font></font>

第 5 行: 将类声明为 Spring Bean,将类名声明为标识符。 第 10 行: 告诉 spring 自动连接ResumeStorageService由"resumeStorageService".

如果我们想附加一个不同的实现,ResumeStorageService则ResumeUploaderService根本不会改变。

?

1

2

3

4

5

6

7

8

9

import org.springframework.beans.factory.annotation.Autowired;<font></font>

  <font></font>

public class Candidate {<font></font>

     @Autowired private ResumeUploaderService resumeUploaderService;<font></font>

  <font></font>

     public void upload(Byte[] resume) {<font></font>

         resumeUploaderService.uploadResume(resume);<font></font>

     }<font></font>

}<font></font>

第 4 行: 要求 Spring 分配resumeUploaderService.

一切都那么干净和专注。没有类正在初始化另一个类或为另一个类设置任何配置。一切都由 Spring 的Inversion of Control Container (IoC Container)管理。

总结

您已经完成了Spring 依赖注入和控制反转指南。您了解了什么是依赖关系以及类如何紧密耦合或松散耦合。我们了解了面向对象编程中的依赖注入和控制反转的概念。您还了解到Spring的控制反转容器(IoC 容器)管理我们 Spring 应用程序中的所有依赖注入。

到此这篇关于Spring依赖注入和控制反转详情的文章就介绍到这了,更多相关Spring依赖注入 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://developer.51cto测试数据/article/707874.html

查看更多关于Spring依赖注入和控制反转详情的详细内容...

  阅读:16次