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<
@Autowired
is by-type (in this case); use @Qualifier
to autowire by-name, following the example from spring docs:
public SomeClass(
@Qualifier("bean1") OtherClass bean1,
@Qualifier("bean2") OtherClass bean2) {
...
}
Note: In contrast to @Autowired which is applicable to fields, constructors and multi-argument methods (allowing for narrowing through qualifier annotations at the parameter level), @Resource is only supported for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.
(below that text is the full example)