{"id":4661,"date":"2014-03-30T14:21:46","date_gmt":"2014-03-30T14:21:46","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/configuring-several-injections-in-guice-collection-of-common-programming-errors\/"},"modified":"2014-03-30T14:21:46","modified_gmt":"2014-03-30T14:21:46","slug":"configuring-several-injections-in-guice-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/configuring-several-injections-in-guice-collection-of-common-programming-errors\/","title":{"rendered":"Configuring several injections in Guice-Collection of common programming errors"},"content":{"rendered":"<p>You could use a binding annotation to let the user specify which one they wanted: http:\/\/code.google.com\/p\/google-guice\/wiki\/BindingAnnotations<\/p>\n<p>You&#8217;d create the annotations like this:<\/p>\n<pre><code>import com.google.inject.BindingAnnotation;\nimport java.lang.annotation.Target;\nimport java.lang.annotation.Retention;\nimport static java.lang.annotation.RetentionPolicy.RUNTIME;\nimport static java.lang.annotation.ElementType.PARAMETER;\nimport static java.lang.annotation.ElementType.FIELD;\nimport static java.lang.annotation.ElementType.METHOD;\n\n@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)\npublic @interface WithClassA{}\n@BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)\npublic @interface WithClassB{}\n...\n<\/code><\/pre>\n<p>Then your user&#8217;s constructor looks like<\/p>\n<pre><code>@Inject\npublic MyConstructor(@WithClassA Panel thePanel) {...}\n<\/code><\/pre>\n<p>Then when you do the binding, you would use .annotatedWith:<\/p>\n<pre><code>bind(Panel.class)\n    .annotatedWith(WithClassA.class)\n    .toProvider(ProviderThatReturnsPanelConstructedWithA.class);\nbind(Panel.class)\n    .annotatedWith(WithClassB.class)\n    .toProvider(ProviderThatReturnsPanelConstructedWithB.class);\n<\/code><\/pre>\n<p>Just in case, here&#8217;s the documentation on how to set up providers: http:\/\/code.google.com\/p\/google-guice\/wiki\/ProviderBindings<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You could use a binding annotation to let the user specify which one they wanted: http:\/\/code.google.com\/p\/google-guice\/wiki\/BindingAnnotations You&#8217;d create the annotations like this: import com.google.inject.BindingAnnotation; import java.lang.annotation.Target; import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME) public @interface WithClassA{} @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4661","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4661","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4661"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4661\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}