Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to have guice use my factory for creating new instances
    text
    copied!<p>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....</p> <p>My bind code</p> <pre><code>binder.bind(ProcessorFactory.class).asEagerSingleton(); </code></pre> <p>The code I am binding (and I want Guice to fill in SplineProcessor and InvertProcessor with their bindings on creation.</p> <pre><code>public class ProcessorFactory implements Provider&lt;Processor&gt;{ public static final ThreadLocal&lt;String&gt; threadLocal = new ThreadLocal&lt;String&gt;(); private Map&lt;String, Class&lt;?&gt;&gt; nameToClazz = new HashMap&lt;String, Class&lt;?&gt;&gt;(); 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&lt;?&gt; 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); } } } </code></pre> <p>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 <a href="https://stackoverflow.com/questions/14833777/guice-multibinder-with-providers">guice multibinder with Providers</a></p> <p>but unfortunately, I can't get that to work either.</p> <p>NOTE: What inject code do I use as well. I am using the following code to inject(and tried ProcessorFactory as well).</p> <pre><code>@Inject private Provider&lt;ProcessorSetup&gt; processors; </code></pre> <p>EDIT FOR MORE CLARIY</p> <p>In a bean I have the following</p> <pre><code>@Inject private Provider&lt;ProcessorSetup&gt; processors; </code></pre> <p>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</p> <p><a href="https://groups.google.com/forum/?fromgroups=#!searchin/google-guice/dean/google-guice/BZn2cnSeX64/MCRgFPjoHH4J" rel="nofollow noreferrer">https://groups.google.com/forum/?fromgroups=#!searchin/google-guice/dean/google-guice/BZn2cnSeX64/MCRgFPjoHH4J</a></p> <p>as I did figure out how to remove the injector at one point finally.</p> <p>thanks, Dean</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload