1

I am trying to have guice call my factory method to create the instance and then fill it in from inspecting it's annotations. I know I have done this before but I can't figure out how to bind the following code....

My bind code

binder.bind(ProcessorFactory.class).asEagerSingleton();

The code I am binding (and I want Guice to fill in SplineProcessor and InvertProcessor with their bindings on creation.

public class ProcessorFactory implements Provider<Processor>{

    public static final ThreadLocal<String> threadLocal = new ThreadLocal<String>();

    private Map<String, Class<?>> nameToClazz = new HashMap<String, Class<?>>();

    public ProcessorFactory() {
        nameToClazz.put("splineV1Beta", SplineProcessor.class);
        nameToClazz.put("invertV1Beta", InvertProcessor.class);
}

    @Override
    public Processor get() {
        String moduleName = threadLocal.get();
        if(moduleName == null)
             throw new IllegalArgumentException("Please call ProcessorFactory.threadLocal.set(moduleName)");
        Class<?> clazz = nameToClazz.get(moduleName);
        if(clazz == null)
             return null;

    try {
        Object newInstance = clazz.newInstance();
        return (Processor) newInstance;
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } finally {
        threadLocal.set(null);
    }
}

}

NOTE: I would STILL like to know how to do this though I also have this open question which is an even better way of doing the same thing guice multibinder with Providers

but unfortunately, I can't get that to work either.

NOTE: What inject code do I use as well. I am using the following code to inject(and tried ProcessorFactory as well).

@Inject
private Provider<ProcessorSetup> processors;

EDIT FOR MORE CLARIY

In a bean I have the following

@Inject
private Provider<ProcessorSetup> processors;

and when I call processors.get(), it steps into guice and then guice correctly invokes ProcessorFactory.get() and I step through that code, and my new enttiy passes through Guice to be returned to the client but is never wired up to anything. This stinks because we have @Inject in those entities we create as well. I know about 2 years ago I had this working on another project related to these posts

https://groups.google.com/forum/?fromgroups=#!searchin/google-guice/dean/google-guice/BZn2cnSeX64/MCRgFPjoHH4J

as I did figure out how to remove the injector at one point finally.

thanks, Dean

4

2 回答 2

2

您是否试图让 Guice 使用您的提供程序来创建 的实例Processor?如果是这样,那么您应该将配置更改为:

bind(Processor.class).toProvider(ProcessorFactory.class);

看起来您也正在使用它ThreadLocal来配置生成的实例。你考虑过辅助注射吗?

于 2013-02-12T16:22:34.123 回答
1

我的新实体通过 Guice 返回给客户端,但从未连接到任何东西。这很臭,因为我们在我们创建的那些实体中也有 @Inject

这是预期的,因为您使用clazz.newInstance(). 如果你想让 Guice 注入它的成员,你需要调用injector.injectMemebers(newInstance). 这将注入所有字段和方法,但显然不是构造函数注入。

我还有第二个 condit 建议,用 AssitedInject 替换 ThreadLocals 以进行参数传递,或者简单地创建一个自定义工厂而不是提供程序。

于 2013-02-15T16:20:23.063 回答