Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a couple different ways you could do this, including your way. The only drawback to using a Provider method is that it's essentially a hand-rolled factory that you have to remember to maintain. (And in this specific case, you're also not getting the benefits of constructor injection).</p> <p>Absent a Provider method, you have to use a binding annotation of some kind. If @Named won't work for you, then you'd need to create an annotation for each binding. </p> <pre><code>bindConstant().annotatedWith(FieldName.class).to("aStringValue"); public SomeClass { public void setFieldName(@FieldName String fieldname) {} } </code></pre> <p>In some cases this might require a 1-to-1 annotation per primitive/String instance to be bound. But I try to make my annotations somewhat orthogonal to the actual instance being described, preferring instead to use the annotation to describe the relationship between the bound objects and the injection points.</p> <p>It's not always possible, but a whole group of related primitives could then potentially be described by a single binding annotation, as long as each primitive type is only used once in the set. So, this could hypothetically work:</p> <pre><code> bindConstant().annotatedWith(MyAnnotation.class).to("aStringValue"); bindConstant().annotatedWith(MyAnnotation.class).to(123); </code></pre> <p>Parenthetically, I'm curious why you can't used @Named annotations on the property, but you <em>can</em> use them on the injected bean?</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