依赖注入(Dependency Injection)

依赖注入(Dependency Injection)详解 依赖注入(DI)是一种重要的设计模式,用于管理代码之间的依赖关系,提高可测试性和可维护性。在前端框架如Vue/Angular中广泛应用。 阅读详情

 

  •  
    • Type 1 or interface injection, in which the exported module provides an interface that its users must implement in order to get the dependencies at runtime (as introduced by Borland Delphi and followed up by C++Builder and JBuilder[citation needed]).
    • Type 2 or setter injection, in which the dependent module exposes a setter method which the framework uses to inject the dependency.
    • Type 3 or constructor injection, in which the dependencies are provided through the class constructor. This is the main form used by PicoContainer, although it also supports setter injection.
    • 类型1 接口注入,外部模块提供一个接口,使用者必须实现此接口以在运行期得到依赖项(Borland Delphi引入,并在C++ Builder和JBuilder中继续运用)。
    • 类型2 设置器注入,使用者暴露一个设置器方法,框架将使用此方法注入依赖项。
    • 类型3 构造器注入,依赖项通过类构造函数提供。这是PicoContainer使用的主要方式(尽管它同时也支持设置器注入)。
  • Dependency injection

    依赖注入

    From Wikipedia, the free encyclopedia

    Jump to: navigation, search

    This article is about article is about the computing process. For other uses, see DI.

    It has been suggested that Dependency inversion principle be merged into this article or section. (Discuss)

    This article or section includes a list of references or external links, but its sources remain unclear because it lacks inline citations.

    You can improve this article by introducing more precise citations where appropriate. (October 2007)

    Dependency injection (DI) in Computer programming refers to the process of supplying an external dependency to a software component. It is a specific form of inversion of control where the concern being inverted is the process of obtaining the needed dependency.

    计算机程序设计中的依赖注入(DI)指的是将一个外部依赖项提供给一个软件组件的过程。这是一种特殊形式的控制反转,其中反转的关注点是获得需要的依赖项的处理。

     

    Conventionally, if an object needs to gain access to a particular service, the object takes responsibility to get hold of that service: either it holds a direct reference to the location of that service, or it goes to a known 'service locator' and requests that it be passed back a reference to an implementation of a specified type of service. By contrast, using dependency injection, the object simply provides a property that can hold a reference to that type of service; and when the object is created a reference to an implementation of that type of service will automatically be injected into that property - by an external mechanism.

    通常,如果一个对象要获得对某个特定服务的访问,对象自己将负责获得这个服务:要么此对象有对这个服务位置的直接引用,要么此对象向一个已知的‘服务定位器’发出请求以得到一个对于特定类型服务实现的引用。与之相对,使用依赖注入,对象只需要简单的提供一个类型,这个类型包含找到某种服务的引用,当对象被创建时,这个引用就可以被一个外部机制自动注入此对象的这个属性里.

     

    When the dependency injection technique is used to decouple high-level modules from low-level services, the resulting design guideline is called the Dependency inversion principle.

    在依赖注入技术开始被用于将高层模块与底层服务解耦的过程中,产生了被称为“依赖倒置原则”的设计准则

     

    The dependency injection approach offers more flexibility because it becomes easier to create alternative implementations of a given service type, and then to specify which implementation is to be used via a configuration file, without any change to the objects that use the service. This is especially useful in unit testing, because it is easy to inject a mock implementation of a service into the object being tested. On the other hand, excessive use of dependency injection can make applications more complex and harder to maintain: in order to understand the application's behaviour the developer needs to look at the configuration as well as the code, and the configuration is "invisible" to IDE-supported reference analysis and refactoring unless the IDE specifically supports the dependency injection framework.

    依赖注入使设计更加灵活。我们可以更容易创建给定服务类型的可选实现,并无需改动使用此服务的对象,仅通过配置文件就可以指定使用哪一个实现。由于可以非常容易的把一个模仿实现注入到被测试对象中,依赖注入技术在单元测试中非常有用。但是,过多的使用依赖注入将会令应用程序更加复杂而难于维护:为了理解应用程序的行为,除了阅读代码,开发人员还必须察看配置文件,然而,配置文件往往对于IDE提供的引用分析和重构机制不可见(除非IDE对依赖注入框架提供了特别的支持)。

    Contents

    目录

    [hide]

    A code illustration using Java

    JAVA代码示例

     

    Suppose that IFoo is an interface:

    假设IFoo是一个接口

    public interface IFoo
    {
          void bar(); // Perform bar
          void baz(); // Perform baz
    }

    There exist also a number of implementation classes, each of them implementing IFoo in some way:

    还有一些实现类,每一个都用不同方式实现了IFoo:

    public class DatabaseFoo
          implements IFoo
    {
          void bar()
          {
              Database.selectBar().execute(); // Use the database to do bar
          }
     
          void baz()
          {
              Database.selectBaz().run(); // Use the database to do baz
          }
    }

    public class PixieDustFoo
          implements IFoo
    {
          void bar()
          {
              Spell.cast("bar"); // Magic!
          }
     
          void baz()
          {
              Spell.cast("baz"); // Magic!
          }
    }

    public class EnterpriseFoo
          implements IFoo
    {
          void bar()
          {
                EnterpriseFactoryObserverFactoryCreator efofc = new EnterpriseFactoryObserverFactoryCreator("bar");
                efofc.creatify();
                efofc.preparify();
                efofc.configurise();
                efofc.makeAwardWinning();
                efofc.opportunities.leverage();
          }
     
          void baz()
          {
                EnterpriseFactoryObserverFactoryCreator efofc = new EnterpriseFactoryObserverFactoryCreator("baz");
                efofc.creatify();
                efofc.preparify();
                efofc.configurise();
                efofc.makeAwardWinning();
                efofc.opportunities.leverage();
          }
    }

    IFoo only specifies the operations available in its interface, but doesn't itself provide any implementation, instead leaving that to other implementer classes. This way a user wishing to use the IFoo functionality can use any implementation, not knowing anything more about them than that they conform to the Foo interface.

    IFoo仅仅指定了这个接口可以进行的操作,但并没有提供任何实现,而是留给了其它实现者的类。这样当一个用户希望使用IFoo功能,就可以使用任何一个实现,除了知道它们都遵守IFoo接口外,不需要事前知道任何更多的细节。

     

    An object needing the services defined by IFoo needs to get an instance of a class that implements IFoo:

    一个需要使用IFoo定义的服务的对象,需要得到一个实现IFoo的类的实例:

     

    public class ImportantClass {
          IFoo foo;
     
          public ImportantClass()
          {
                this.foo = new EnterpriseFoo();
          }
     
          void doReallyImportantStuff()
          {
                this.foo.bar();
          }
    }

    However, this defeats the entire point of using an interface instead of a concrete implementation. To fix that, it's enough to let the outside caller provide the desired implementation:

    但是,这完全破坏了使用接口而不是一个具体实现的目标。为修正这个问题,让外部调用者提供所需的实现就足可以满足目标了。

     

    public ImportantClass(IFoo foo)
    {
        this.foo = foo;
    }

    When using dependency injection there is usually a configuration mechanism or architecture for deciding which implementation gets injected into an object.

    当使用依赖注入时,通常会有一个配置机制或是某个特定架构,以决定哪个实现将被注入到一个对象中。

     

    Forms of Dependency Injection

    依赖注入的几种形式

     

    Martin Fowler identifies three ways in which an object can get a reference to an external module, according to the pattern used to provide the dependency:

    依照提供依赖项时使用的模式,Martin Fowler定义了三种对象可以从得到一个外部模块引用的方式:

     

     

    The open source Yan Container[1] provides support for arbitrary types of injection[2] besides the common ones defined above.

    除了前列的几种普通形式外,开源项目Yan Container还对任意形式的注入提供了支持。

     

    Existing frameworks

    现有框架

     

    Dependency injection frameworks exist for a number of platforms and languages including:

    在一些平台和语言中都有依赖注入框架,包括:

    ActionScript

    C++

    ColdFusion

    Delphi

    Java

    Java 2 Micro Edition

    .NET

    PHP4

    PHP5

    Perl

    Python

    Ruby

    See also

    参见

     

    External links

    外部链接

    Retrieved from "http://en.wikipedia.org/wiki/Dependency_injection"

    Categories: Software components | Software architecture

    Hidden categories: Articles to be merged since June 2008 | Articles lacking in-text citations | All articles with unsourced statements | Articles with unsourced statements since July 2008

     

    源文档 <http://en.wikipedia.org/wiki/Dependency_injection>

     

QInjection:一个简单的Qt5依赖注入库 Qt依赖注入库 快速开始 想象一下,我们有两个名为MyClass1和MyClass2的类,我们在应用程序中定期使用它们。 有很多方法可以做到这一点: 在需要的任何地方创建它们! 创建一个实例,并将对象创建和函数调用传递到不同的级别! 使用依赖注入 您正在访问的此存储库是解决问题的一种简单方法 该库具有一种将对象存储到Pool并在需要时检索它的机制。 让我们看一个简单的例子: 第一:将对象添加到依赖池中。 在main方法中,我们将MyClass1和MyClass2的实例添加到Pool中。 #include "myclass1.h" #include "myclass2.h" #include "dependencyinjection.h" #include <QCoreApplication> int main(int argc, char *argv[]) { QCor 立即下载

相关推荐

C++ 依赖注入

软件设计是控制依赖关系的方法和方式。其中依赖注入起很重的作用。依赖注入是简单,但非常强大的设计模式。本文介绍依赖注入的概念,并介绍简单且实用的C++依赖注入容器的一种实现方法。

探索injeqt:Qt的简单依赖注入框架

探索injeqt:Qt的简单依赖注入框架 项目介绍 injeqt 是一个专为Qt设计的简单依赖注入(Dependency Injection, DI)框架。依赖注入是一种设计模式,它允许对象的依赖关系在运行时动态注入,而不是在编译时硬编码。这种模式可以显著提高代码的可维护性和可测试性,尤其是在大型项目中。 injeqt通过提供一个轻量级的、易于使用的API,使得在Qt应用程序中实现依赖注入变得非常...

gitblog_00434的博客 630

QuteContainer:C ++ Qt框架的IoC和依赖注入容器-开源

QuteContainer是用于依赖项注入的轻型控制反转(IOC)容器。 QuteContainer使用C ++ Qt框架构建并用于C ++ Qt框架。 QuteContainer可以使用构造函数参数注入来自动解析对象依赖树。

再聊:依赖注入(Dependency injection)

依赖注入是解决一个对象如何获得它依赖的其他对象的技术。例如,一个对象A可以通过注入器(Injector)获得它所依赖的其他对象B、C。其中B、C对象是A对象的依赖(Dependencies)被传入到A对象中,一般被称为Service,A作为接收对象一般叫Client,而负责将依赖传递入接收对象的代码叫做Injector。 因此我们可以看到Client具体使用什么Service是由Injector去指定的,并传入到Client中,而不是Client自己去指定或创建的,这一点也是依赖注入的基本要求。 ** 依赖

weixin_40763897的博客 1635

从谱峰搜索到多项式求根:Root-MUSIC算法原理与MATLAB实践

本文深入解析Root-MUSIC算法在阵列信号处理中的原理与应用,通过多项式求根替代传统MUSIC算法的谱峰搜索,显著提升计算效率和精度。文章详细介绍了算法核心思想、MATLAB实现步骤及工程优化技巧,帮助读者掌握这一高效的DOA估计方法。

weixin_30832143的博客 87

Hcaptcha验证码破解深度解析:从环境检测到n值算法的攻防实战

本文深入解析Hcaptcha验证码的攻防技术,从环境检测到n值算法的全面突破策略。详细介绍了浏览器指纹模拟、图像识别工程化解决方案及协议层逆向分析,为安全研究人员提供高效破解Hcaptcha的实战指南。

weixin_42563415的博客 389

Injeqt 依赖注入框架使用教程

Injeqt 依赖注入框架使用教程 1. 项目介绍 Injeqt 是一个基于 Qt 的依赖注入框架,旨在通过 Qt 的元对象编译器(MOC)实现强大且可靠的依赖注入功能。Injeqt 的设计目标是简化 Qt 应用程序中的依赖管理,使得代码更加模块化和易于维护。 Injeqt 的核心功能包括: 通过元对象系统实现依赖注入。 支持模块化设计,便于扩展和维护。 提供简洁的 API,易于集成到现有项目中...

gitblog_01029的博客 896

phaser3适配微信小游戏

phaser3适配微信小游戏

SilenzioX的博客 1809

打包Qt程序,自动添加依赖的库和文件(详细步骤)

讲解如何自动添加qt所依赖的动态库。

bigger_belief的博客 1881

利用PLECS实现Boost变换器的闭环控制仿真与优化

本文详细介绍了如何利用PLECS仿真软件实现Boost变换器的闭环控制与优化。从基础原理到开环系统搭建,再到闭环控制设计与动态优化,提供了完整的仿真实践指南。通过PLECS的仿真功能,工程师可以有效诊断问题、优化参数,并实现高性能的DC-DC变换器设计。

weixin_42527401的博客 329

SpringCloud2025+SpringBoot3.5.0动态路由503问题排查与负载均衡依赖解析

本文详细排查了SpringCloud 2025与SpringBoot 3.5.0环境下,Spring Cloud Gateway动态路由返回503错误的根本原因。核心问题在于新版网关未默认集成负载均衡器,导致无法解析lb://服务名。通过对比静态路由与动态路由,定位到需显式引入spring-cloud-starter-loadbalancer依赖,并提供了完整的解决方案与最佳实践配置。

s3t4u的博客 506

DL(Dependency Injection) 依赖注入

DL 依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。 为什么需要DL? 我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。 ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。 那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spri...

qq_38009970的博客 560

Tessent MBIST SDC

主要是两个PROC,tessent_constrain_${design_name}_mentor_ijtag_non_modal和tessent_constrain_${design_name}_mentor_memory_bist_modal。其中,Memory/MBIST controller/MEM Interface可以从func_clk_1来(用func_clk_1和func_clk_2的高频时钟),如图中红色线。其中,SIB和BAP用ijtag_tck的clock,如图绿色线。

po_poo的博客 2000

Dependency Injection 依赖注入

Dependency Injection 依赖注入IoC 控制反转

LYZ 503

大模型多智能体协作面试题及参考答案

在多智能体系统里,代理定义自身目标或效用函数的过程与系统架构、应用场景紧密相关。从本质上讲,目标是代理行为的导向,而效用函数则是对目标达成程度的量化评估。代理通常依据自身角色、系统全局需求以及与其他代理的交互关系来确立目标。比如在合作型 MAS 中,代理目标可能需要与全局目标对齐,像多个物流机器人协作完成货物配送时,每个机器人的目标会围绕 “高效完成分配任务”“避免路径冲突” 等展开,其效用函数可能涉及任务完成时间、能耗等指标的加权求和。目标定义的方式存在多种类型。

大模型大数据攻城狮的专栏 1029

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

Dependency Injection pattern 依赖注入模式

In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common patter...

勿在浮沙专栏 682

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【5】依赖注入DI(Dependency Injection)

6、依赖注入 概念 依赖注入(Dependency Injection,DI)。 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 . 构造器注入 之前已经讲过 Set 注入 (重点) 要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型 , 没有set方法 , 是 is . 测试pojo类 : Address.java package com.k

Kuber 611

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

依赖注入Dependency Injection,DI

依赖注入Dependency Injection,DI @AutoWired,他注入的机制最基本的一条是:根据类型(by type),根据类型从IOC容器中获取bean。 使用AutoWired进行依赖注入的时候,如果注入的接口有多于一个的实现类,可一根据变量名称,从IOC容器中获取对象。 Animal接口 两个实现类:dog、cat @AutoWired Animal animal; // 报错...

情怀中的释然~ 702

Spring第四篇_依赖注入(Dependency Injection)

4. 依赖注入 Spring中的依赖注入: 依赖注入: Dependency Injection IOC的作用: 降低程序间的依赖耦合(依赖关系) 依赖关系的管理: 以后都交给Spring来维护 在当前类需要用到其它类的对象的时候, 由Spring为我们提供, 我们只需要在配置文件中说明 依赖关系的维护, 就称之为依赖注入 依赖注入: 能注入的数据 有三类: 基本类型和String 其他bea...

weixin_44086832的博客 596

依赖注入 (Dependency Injection)

乍听依赖注入,似乎是什么很高级的东西,其实不然,其只是名字高大上,我们实际上在不知不觉间就经常用它了。依赖注入是软件工程中的一个名词,不止存在于JAVA语言中,在多种语言中都有体现。本文只针对JAVA语言举例。从软件工程的角度来看,依赖注入(Dependency Injection,简称DI)是一种设计模式,旨在减少软件组件之间的紧耦合,并促进代码的重用性和可测试性。

Ljj_fishhG的博客 1357

Python中的依赖注入(Dependency Injection)

flyfish

二分掌柜的 642

Dependency injection python的依赖注入框架

python依赖注入

Good_Luck_Kevin2018的博客 2762

dependency injection php,依赖注入模式(Dependency Injection)

2.6.1. 目的用松散耦合的方式来更好的实现可测试、可维护和可扩展的代码。依赖注入模式:依赖注入(Dependency Injection)是控制反转(Inversion of Control)的一种实现方式。要实现控制反转,通常的解决方案是将创建被调用者实例的工作交由 IoC 容器来完成,然后在调用者中注入被调用者(通过构造器/方法注入实现),这样我们就实现了调用者与被调用者的解耦,该过程被称...

weixin_33681561的博客 172

依赖注入&控制反转 oC 容器和Dependency Injection 模式

撰文/Martin Fowler 编译/透明Java 社群近来掀起了一阵轻量级容器的热潮,这些容器能够帮助开发者将来自不同项目的组件组装成为一个内聚的应用程序。在它们的背后有着同一个模式,这个模式决定了这些容器进行组件装配的方式。人们用一个大而化之的名字来称呼这个模式:“控制反转”(Inversion of Control,IoC)。在本文中,我将深入探索这个模式的工作原理,给它一个更能描述其

二进制天堂 6351
上一篇: 在Windows下编译 Boost 1.35、Lua 5.1和LuaBind 0.7
下一篇: C++中实现静态初始化代码段
BalonFan
博客等级 码龄26年 47粉丝 41原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值