English 中文(简体)
我怎么能够改写这部法典,以便我不会以人工/主动的方式援引注射器?
原标题:How could I rewrite this code snippet so that I m not manually/explicitly invoking the injector?

在阅读谷歌古冰的许多密码例子后,我看到许多这样的法典:

public class MyModule implements Module {
    public void configure(Binder binder) {
        binder.bind(Service.class).to(ServiceImpl.class);
    }
}

public class Widget {
    private Injector injector = Guice.createInjector(new MyModule());

    public void doSomething() {
        Service service = injector.getInstance(Service.class);
        service.run();
    }
}

我的理解是,明确称之为国际电离辐射机制(例如injector.getInstance(Class<T>) in Guice或appContext.getBean(String) in Spring)的守则确实是国际奥C anti-pattern

我注意到,在这些例子中,没有提及@Inject@ImplementedBy的附加说明,而I believe是该名反赞助人的Guice s workarounds。

www.un.org/Depts/DGACM/index_spanish.htm 我怎么能够重写上述法典,以便我不会人工/主动援引注射器? 如同所配置的所有材料一样,通过执行时间进入<条形码>方法,我们使用的应用已经是“知识”的<条码>。

提前感谢!

最佳回答

• 在需要时使用说明,在模块中添加斜线。

您需要在其构造中添加<条码>服务:

public class Widget {

    private final Service service;

    @Inject
    public Widget(Service service) {
        this.service = service;
    }
}
问题回答

I m newbie with Guice, but why not injecting the service to Widget?

@Inject
public Widget (Service service) {
    this.service = service;
    ...
}
public void doSomething() {
    service.run();
    ...
}

或者,注入一个提供服务的供应商。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签