English 中文(简体)
在什么地方保留ice子?
原标题:Where to keep guice injectors?

你们的建议是什么?

I found most suitable for me solution - keep injectors and modules in enumeration classes. Advantages:

  1. injectors and modules created once,
  2. injectors can be used from different classes while running application (not only at bootstrap),
  3. injectors kept in one place and can be easily found.

例:

import static ru.package.Modules.*;

public enum Injectors {

FOO_INJECTOR(BarModule.module()),

FOO2_INJECTOR(FOO_INJECTOR.injector(),
        Bar2Module.module(), FooModule.module());

private final Injector m_injector;

Injectors (Module... modules) {
    m_injector = Guice.createInjector(modules);
}

Injectors (Injector parentInjector, Module... modules) {
    m_injector = parentInjector.createChildInjector(modules);
}

public Injector injector() {
    return m_injector;
}
}
问题回答

你似乎从根本上误解了依赖注射是如何运作的。 如果你试图使用<条码>Injector,在贵国法典中,除你boot陷申请的地方外,在任何地方都使用<条码>,你不再使用依赖性注射,而是将依赖性注射作为服务提供者。 如果你需要测试一个班级和你的班级,那么你就不得不编制一个<条码>Injector,在建筑商中确切说明他们所依赖的是什么(因为谁知道他们从<条码>中抽出什么)。 轨道在某些方法中,如果已经或能够参考。 实际使用<代码>enum 正如你在这里描述的那样,情况甚至更糟:你根本无法改变组合,甚至无法进行测试,因为你的模块难以输入。

由于依赖性注射,各年级仅宣布其依赖性,并允许<代码>Injector,以透明方式工作(在最初要求获得基本申请标语之后),提供所有依赖者。 这使得你们的法典中理解、测试和改变功能相对容易。 不管怎么说,我建议更多地了解如何利用DI和Guice......你真的不应该这样做。

更大的问题是为什么?

没有必要将<代码>Injector <>/code>紧凑起来,因为一旦注入,应当做到并且应当消失。

但是,如果你真的需要<编码>Injector,你就只能:

@Inject
private Injector injector;

这一应用网络是不是独立的吗?





相关问题
How to Inject Open Connections with an IoC

First, my scenario. I have a service, BillingService, has a constructor like this: BillingService(IInstallmentService, IBillingRepository) The InstallmentService has a constructor that looks like ...

Using the Ninject kernel as a Unit of Work object factory

So I m starting to use Ninject for dependency injection and I m wondering what people think of using a kernel as an object factory for Unit of Work type objects like Linq2Sql Datacontexts. I would ...

array dependency injection in spring?

is there a way to use dependency injection to inject all available implementations of a specific interface in spring? This is kind of the same thing as asked here for .NET. Though my aim is to use @...

Unity constructors

I have setup unity in my project and it is working for objects that don t have constructor injection implemented on them. The issue is now I do have an object which requires a custom object as a ...

Grails Packaging and Naming Conventions

Packaging Controllers, Services,etc. i.e. - com.company.controllers - com.company.services Is this a good practice or should be avoided by all means?? Another worth mentioning problem I encountered ...

ASP.NET MVP Injecting Service Dependency

I have an ASP.NET page that implements my view and creates the presenter in the page constuctor. Phil Haack s post providing was used as the starting point, and I ll just the examples from the post ...

Authorization and Windsor

I m trying to implement my custom authorize attribute like: public class MyCustomAuth : AuthorizeAttribute { private readonly IUserService _userService; public MyCustomAuth(IUserService ...

热门标签