I like constructor-based injection as it allows me to make injected fields final. I also like annotation driven injection as it simplifies my context.xml. I can mark my constructor with @Autowired and everything works fine, as long as I don’t have two parameters of the same type. For example, I have a class:
@Component
public class SomeClass {
@Autowired(required=true)
public SomeClass(OtherClass bean1, OtherClass bean2) {
…
}
}
and an application context with:
<bean id="bean1" class="OtherClass" />
<bean id="bean2" class="OtherClass" />
There should be a way to specify the bean ID on the constructor of the class SomeClass, but I can’t find it in the documentation. Is it possible, or am I dreaming of a solution that does not exist yet?
@Autowiredis by-type (in this case); use@Qualifierto autowire by-name, following the example from spring docs:(below that text is the full example)