java Spring 中的依赖注入是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3725515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does dependency injection work in Spring?
提问by Dead Programmer
I want to know how spring does dependency injection. I want the low level logic used.
我想知道spring是如何进行依赖注入的。我想要使用低级逻辑。
Updates:
更新:
I want to know how the object references are injected to the constructors or setter methods, is it through Reflection or some byte code level.
我想知道如何将对象引用注入到构造函数或 setter 方法中,是通过反射还是某些字节码级别。
采纳答案by Henryk Konsek
Configuration of dependencies are read from XML, annotations or Java DSL (JavaConfig). Then Spring DI engine wires the dependencies based on the metadata from the configuration using the Java reflection API.
从 XML、注释或 Java DSL (JavaConfig) 读取依赖项的配置。然后 Spring DI 引擎使用 Java 反射 API 根据来自配置的元数据连接依赖项。
回答by Jigar Joshi
Java components / classes should be as independent as possible of other Java classes. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing). To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.
Class A has a dependency to class B if class A uses class B as a variable.
If dependency injection is used then the class B is given to class A via the constructor of the class A - this is then called construction injection; or via a setter - this is then called setter injection
The general concept of dependency injection is called Inversion of Control. A class should not configure itself but should be configured from outside.
A design based on independent classes / components increases the re-usability and possibility to test the software. For example if a class A expects a Dao (Data Access Object) for receiving the data from a database you can easily create another test object which mocks the database connection and inject this object into A to test A without having an actual database connection.
A software design based on dependency injection is possible with standard Java.
Spring just adds some simplifications in using dependency injection by providing a standard way of providing the configuration and by managing the reference to the created objects.
Java 组件/类应该尽可能独立于其他 Java 类。这增加了重用这些类并独立于其他类测试它们的可能性(单元测试)。为了将 Java 组件与其他 Java 组件分离,对某个其他类的依赖应该注入到它们中,而不是类本身创建/找到这个对象。
如果 A 类使用 B 类作为变量,则 A 类依赖于 B 类。
如果使用依赖注入,则通过类 A 的构造函数将类 B 提供给类 A - 这称为构造注入;或通过 setter - 这被称为 setter 注入
依赖注入的一般概念称为控制反转。一个类不应该配置自己,而应该从外部配置。
基于独立类/组件的设计增加了可重用性和测试软件的可能性。例如,如果类 A 需要 Dao(数据访问对象)来接收来自数据库的数据,您可以轻松创建另一个模拟数据库连接的测试对象,并将此对象注入 A 以测试 A,而无需实际的数据库连接。
使用标准 Java 可以实现基于依赖注入的软件设计。
Spring 只是通过提供提供配置的标准方法和管理对创建的对象的引用,在使用依赖注入方面进行了一些简化。
For more read this
欲了解更多,请阅读本文
Edit1:
编辑1:
When Spring initializes its context it creates all the beans defined eager in Spring application context.xml file. Now suppose your Bean A has dependency of B then the Obj of B is already with Spring as it has been created successfully while Spring initialization. Then Spring will search for setter method in class A and will set B's Obj there.
当 Spring 初始化它的上下文时,它会创建所有在 Spring 应用程序 context.xml 文件中定义的 bean。现在假设您的 Bean A 依赖于 B,那么 B 的 Obj 已经与 Spring 一起使用,因为它在 Spring 初始化时已成功创建。然后 Spring 将在类 A 中搜索 setter 方法并将 B 的 Obj 设置在那里。
Edit2:
编辑2: