java 如何使用 Google Guice 创建需要参数的对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/996300/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:44:06  来源:igfitidea点击:

How to use Google Guice to create objects that require parameters?

javadependency-injectionguice

提问by Daniel Schneller

Maybe I am just blind, but I do not see how to use Guice (just starting with it) to replace the newcall in this method:

也许我只是瞎了眼,但我不知道如何使用 Guice(刚开始使用它)来替换new此方法中的调用:

public boolean myMethod(String anInputValue) {
    Processor proc = new ProcessorImpl(anInputValue);
    return proc.isEnabled();
}

For testing there might be a different implementation of the Processor, so I'd like to avoid the newcall and in the course of that get rid of the dependency on the implementation.

为了测试,处理器可能有不同的实现,所以我想避免new调用,并在此过程中摆脱对实现的依赖。

If my class could just remember an instance of Processor I could inject it via the constructor, but as the Processors are designed to be immutable I need a new one every time.

如果我的班级只记得 Processor 的一个实例,我可以通过构造函数注入它,但是由于 Processor 被设计为不可变的,我每次都需要一个新的。

How would I go about and achieve that with Guice (2.0) ?

我将如何使用 Guice (2.0) 实现这一目标?

回答by waxwing

There is some time since I used Guice now, but I remember something called "assisted injection". It allows you to define a factory method where some parameters are supplied and some are injected. Instead of injecting the Processor you inject a processor factory, that has a factory method that takes the anInputValueparameter.

自从我现在使用 Guice 以来已经有一段时间了,但我记得有一种叫做“辅助注射”的东西。它允许您定义一个工厂方法,其中提供一些参数并注入一些参数。不是注入处理器,而是注入处理器工厂,该工厂具有接受anInputValue参数的工厂方法。

I point you to the javadoc of the FactoryProvider. I believe it should be usable for you.

我将您指向FactoryProviderjavadoc。我相信它应该对你有用。

回答by Dave W. Smith

You can get the effect you want by injecting a "Provider", which can by asked at runtime to give you a Processor. Providers provide a way to defer the construction of an object until requested.

你可以通过注入一个“Provider”来获得你想要的效果,它可以在运行时被要求给你一个处理器。提供者提供了一种方法来推迟对象的构建,直到被请求。

They're covered in the Guice Docs hereand here.

他们在此处此处的 Guice 文档中有所介绍。

The provider will look something like

提供者看起来像

public class ProcessorProvider implements Provider<Processor> {
    public Processor get() {
        // construct and return a Processor
    }
}

Since Providers are constructed and injected by Guice, they can themselves have bits injected.

由于提供者是由 Guice 构建和注入的,因此它们本身可以注入位。

Your code will look something like

你的代码看起来像

@Inject
public MyClass(ProcessorProvider processorProvider) {
    this.processorProvider = processorProvider;
}

public boolean myMethod(String anInputValue) {
    return processorProvider.get().isEnabled(anInputValue);
}

回答by tddmonkey

Does your Processor need access to anInputValuefor its entire lifecycle? If not, could the value be passed in for the method call you're using, something like:

您的处理器是否需要anInputValue在其整个生命周期内访问?如果没有,是否可以为您正在使用的方法调用传递该值,例如:

@Inject
public MyClass(Processor processor) {
    this.processor = processor;
}

public boolean myMethod(String anInputValue) {
    return processor.isEnabled(anInputValue);
}