I was wondering if it possible to use the @Resource
annotation on a constructor.
My use case is that I want to wire a final field called bar
.
public class Foo implements FooBar {
private final Bar bar;
@javax.annotation.Resource(name="myname")
public Foo(Bar bar) {
this.bar = bar;
}
}
I get a message that the @Resource
is not allowed on this location. Is there any other way I could wire the final field?
From the source of @Resource
:
@Target({TYPE, FIELD, METHOD})
@Retention(RUNTIME)
public @interface Resource {
//...
}
This line:
@Target({TYPE, FIELD, METHOD})
means that this annotation can only be placed on Classes, Fields and Methods. CONSTRUCTOR
is missing.