Java Reference
In-Depth Information
<beans ...>
...
<bean id="datePrefixGenerator"
class="com.apress.springenterpriserecipes.sequence. DatePrefixGenerator">
<property name="pattern" value="yyyyMMdd" />
</bean>
<bean id="yearPrefixGenerator"
class="com.apress.springenterpriserecipes.sequence. DatePrefixGenerator">
<property name="pattern" value="yyyy" />
</bean>
</beans>
In a similar way, you can apply the @Autowired annotation to a type-safe collection. Spring can read
the type information of this collection and auto-wire all the beans whose type is compatible.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator {
@Autowired
private List<PrefixGenerator> prefixGenerators;
...
}
If Spring notices that the @Autowired annotation is applied to a type-safe java.util.Map with strings
as the keys, it will add all the beans of the compatible type, with the bean names as the keys, to this map.
package com.apress.springenterpriserecipes.sequence;
import org.springframework.beans.factory.annotation.Autowired;
public class SequenceGenerator {
@Autowired
private Map<String, PrefixGenerator> prefixGenerators;
...
}
Auto-Wiring by Type with Qualifiers
By default, auto-wiring by type will not work when there is more than one bean with the compatible type
in the IoC container. However, Spring allows you to specify a candidate bean by providing its name in
the @Qualifier annotation.
Search WWH ::




Custom Search