不过,像Guice的作者一样,我更喜欢构造函数注入。
However, I prefer constructor injection, as do Guice's authors. Here's a quick analysis of the three styles.
构造函数注入利用构造函数判断要返回的具体对象类型。
Constructor injection USES a constructor to determine what type of concrete object to return.
根据Guice的最佳实践指南,构造函数注入是询问依赖项的首选方式。
According to the Guice best practices guide, constructor injection is the preferred way to ask for your dependencies.
Spring中的构造函数注入允许您通过类构造函数注入依赖关系。
Constructor injection in Spring lets you inject your dependencies through class constructors.
这个行为在DI世界中称作构造函数注入,因为实例是通过一个构造函数传递的。
This behavior is referred to in the DI world as constructor injection, because the instance is passed through one of the constructors.
清单5显示的GBean被定义成使用构造函数注入来提供Customer引用。
Listing 5 shows a GBean that's defined to use constructor injection to provide a Customer reference.
围绕着如何执行DI,主要有两派思想:构造函数注入和setter注入。
Two major schools of thought revolve around how to perform DI: constructor injection and setter injection.
HiveMind用户通常优先选择setter注入,但该容器还支持构造函数注入。
HiveMind users typically prefer setter injection, but the container also supports constructor injection.
接下来,将看到构造函数注入的示例,以及Geronimo的部署计划定义容器注入代码的方式。
Next, you see an example of constructor injection and how Geronimo's deployment plans define how the container injects code.
许多DI的支持者愿意采用构造函数注入,因为这意味着注入的类永远不会出现依赖项不存在的情况。
Many proponents of DI prefer constructor injection, because it means that the injected class never exists in a state where its dependencies aren't present.
PicoContainer 流行的使用模型是构造函数注入,但它也支持setter 注入。
PicoContainer's prevailing usage model is constructor injection, although it also supports setter injection.
当Springbean拥有单个构造函数时,这个针对sca的特性包目前只支持构造函数注入。
The feature pack for SCA currently supports only constructor injection when the Spring bean has a single constructor.
如果您有一个需要某些依赖项的超类,也可以使用方法注入(构造函数注入会使这种情况变得很复杂)。
You can also use it if you have a superclass that needs some dependencies. (Constructor injection makes this difficult.)
正如在本系列的第一篇文章中解释过的,构造函数注入表明框架会提供类在初始化时需要的构造函数参数。
As explained in the first article of this series, constructor injection implies that the framework supplies constructor parameters required by the class upon instantiation.
这种实例化涉及到要调用的类的默认构造函数,随后还要注入依赖关系。
This instantiation involves the default constructor for the class being called, with the injection of dependencies happening afterwards.
例如,假设您想要使用构造函数为calculatorbean注入必需的依赖关系(必需的SCA引用)。
For example, assume you're trying to inject the required dependencies (the required SCA references) for the calculator bean using the constructor.
Pico程序员首先依靠构造函数进行注入。
Pico programmers rely first on constructors to do injection.
Spring支持三种依赖注入——setter、构造函数和方法注入——但总的来说,最流行的模型是setter注入。
Spring supports three types of dependency injection — setter, constructor, and method injection — but overall, the prevailing model is setter injection.
该依赖关系将通过构造函数被注入。
The dependency will be injected through the constructor. You instantiate a container.
这是Springbean构造函数中的SCA引用和属性的注入。
This is the injection of SCA references and properties within Spring bean constructors.
mock对象被注入到被测系统的构造函数中,通过MockControl对象的验证方法,验证交互行为从而得出结论。
The mock object is injected into the constructor of the system under test and the test concludes by verifying this interaction via the verify method of the MockControl object.
类似Spring,Pico也支持setter注入,但主要使用构造函数。
Like Spring, Pico also supports setter injection, but the emphasis is strongly on constructors.
在使用该方法时,我们必须记住该机制只支持访问器注入,不支持基于构造函数参数的注入。
While using this approach we have to remember that this mechanism supports only accessor-based injection, constructor argument based injection is not supported.
Unity是一个轻量级,可扩展的依赖注入容器,支持构造函数,属性和方法调用注入。
Unity is a lightweight, extensible dependency injection container with support for constructor, property, and method call injection.
更多关于为构造函数提供参数(如果有必要的话)的机制和构造对象之后设置对象实例属性的细节,请看”依赖注入”。
For details about the mechanism for supplying arguments to the constructor (if required) and setting object instance properties after the object is constructed, see Injecting Dependencies.
Reflexil是能够重命名,删除或注入类,接口,结构,枚举,事件,字段,方法,构造函数,财产或程序集的引用。
Reflexil is able to rename, delete or inject class, interface, struct, enum, event, field, method, constructor, property or assembly reference.
Reflexil是能够重命名,删除或注入类,接口,结构,枚举,事件,字段,方法,构造函数,财产或程序集的引用。
Reflexil is able to rename, delete or inject class, interface, struct, enum, event, field, method, constructor, property or assembly reference.
应用推荐