Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You might need to make a Factory for the MyConnectionFactory, but only if you actually need to vary the parameters passed to MyConnectionFactory at runtime. Otherwise, you can get away with making a <code>Provider</code>—which is a Factory after all, I guess—or its slimmer cousin <a href="https://code.google.com/p/google-guice/wiki/ProvidesMethods" rel="nofollow">the @Provides Method</a>. (I'm guessing the class in question takes unspecific or primitive arguments, or else a <a href="https://code.google.com/p/google-guice/wiki/ToConstructorBindings" rel="nofollow"><code>toConstructor binding</code></a> is yet another possibility.)</p> <p>The Factory would look like this:</p> <pre><code>public class MyConnectionFactoryFactory { @Inject Provider&lt;SomeDependency&gt; someDependencyProvider; public MyConnectionFactory create(String url) { return new MyConnectionFactory(someDependencyProvider.get(), url, 10000); } } </code></pre> <p>The Provider would look identical, except it'd implement <code>Provider&lt;MyConnectionFactory&gt;</code> and <code>create</code> would instead be a zero-arg <code>get()</code>. You can bind that through <code>bind(MyConnectionFactory.class).toProvider(MyConnectionFactoryProvider.class)</code>.</p> <p>However, if you know all your constructor parameters at configuration time, you could equally create a <code>@Provides</code> method in your Module, which would look like this:</p> <pre><code>public class MyModule extends AbstractModule { @Override public void configure() { /* your configuration here */ } /* FYI: a @Provides method by any other name would return as sweet */ @Provides MyConnectionFactory getConnectionFactory(SomeDependency dependency) { return new MyConnectionFactory(dependency, url, 10000); } } </code></pre> <p>...which takes your "wrapper code" boilerplate to three extra non-blank lines. Remember, Guice will automatically bind any combination of <code>X</code>, <code>Provider&lt;X&gt;</code>, or <code>@Provides X</code> to any injection of <code>X</code> or <code>Provider&lt;X&gt;</code> automatically for you, so feel free to bind it however is most convenient.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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