如何避免在使用guice时使用inject.createInstance()全部位置? [英] How to avoid having injector.createInstance() all over the place when using guice?

查看:123
本文介绍了如何避免在使用guice时使用inject.createInstance()全部位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些我不知道的是什么:根据我迄今为止所读到的内容,我应该在我的引导类中使用Injector(在一个独立的应用程序中,这通常是主要的)方法),如下面的示例(取自guice文档):

  public static void main(String [] args ){
/ *
* Guice.createInjector()接收您的模块,并返回一个新的Injector
*实例。大多数应用程序将在
* main()方法中调用此方法一次。
* /
注射器喷射器= Guice.createInjector(新的BillingModule());

/ *
*现在我们有了注入器,我们可以构建对象。
* /
RealBillingService billingService = injector.getInstance(RealBillingService.class);
...
}

但是,如果不是所有我需要的对象可以在启动时创建吗?也许我想在应用程序运行时响应一些用户交互?我不必将注射器保持在某处(例如,作为静态变量),然后在需要创建新对象时调用inject.getInstance(SomeInterface.class)?



当然,将呼叫到Injector.getInstance()遍布整个地方似乎是不可取的。



我在这里遇到什么问题?

解决方案

是的,您基本上只能使用Injector创建获取根对象的实例。其余的应用程序不应接触Guice-Container。如您所知,您仍然需要在需要时创建一些对象。有不同的方法来做,每个都适合不同的需求。



注入提供者
提供者是Guice的界面。它允许您请求一个新的对象实例。该对象将使用Guice创建。例如。

  class MyService {
private Provider< Transaction> transactionProvider;
public MainGui(Provider< Transaction> transactionProvider){
this.transactionProvider = transactionProvider;
}

public void actionStarted(){
事务事务= transactionProvider.get();
}

建立工厂
通常你需要某种工厂。该工厂使用一些注入的服务和一些参数,并为您创建一个新的对象。然后你使用这个工厂的新实例。然后你注入该工厂并使用它。使用 AssistedInject -extension


也有助于此

我认为这两种可能性,您很少需要使用Guice-Injector本身。然而,有时仍然适合使用喷射器本身。然后您可以将注射器注入到组件中。


There's something I just don't get about guice: According to what I've read so far, I'm supposed to use the Injector only in my bootstrapping class (in a standalone application this would typically be in the main() method), like in the example below (taken from the guice documentation):

public static void main(String[] args) {
    /*
     * Guice.createInjector() takes your Modules, and returns a new Injector
     * instance. Most applications will call this method exactly once, in their
     * main() method.
     */
    Injector injector = Guice.createInjector(new BillingModule());

    /*
     * Now that we've got the injector, we can build objects.
     */
    RealBillingService billingService = injector.getInstance(RealBillingService.class);
    ...
  }

But what if not all Objects I ever need can be created during startup? Maybe I want to respond to some user interaction when the application is running? Don't I have to keep my injector around somewhere (e.g. as a static variable) and then call injector.getInstance(SomeInterface.class) when I need to create a new object?

Of course spreading calls to Injector.getInstance() all over the place seems not to be desirable.

What am I getting wrong here?

解决方案

Yes, you basically only should use the Injector to create get the instance for the root-object. The rest of the application shouldn't touch the Guice-Container. As you've noticed, you still need to create some objects when required. There are different approaches for doing that, each suitable for different needs.

Inject a Provider Provider is a interface from Guice. It allows you to request a new instance of a object. That object will be created using Guice. For example.

 class MyService{
     private Provider<Transaction> transactionProvider;
     public MainGui(Provider<Transaction> transactionProvider){
         this.transactionProvider = transactionProvider;
     }

     public void actionStarted(){
         Transaction transaction = transactionProvider.get();
     }

Build a Factory Often you need some kind of factory. This factory uses some injected services and some parameters and creates a new object for you. Then you use this factory for new instances. Then you inject that factory and use it. There also help for this with the AssistedInject-extension

I think with these two possibilities you rarely need to use the Guice-Injector itself. However sometimes is still appropriate to use the injector itself. Then you can inject the Injector to a component.

这篇关于如何避免在使用guice时使用inject.createInstance()全部位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆