Java Reference
In-Depth Information
How It Works
Auto-Wiring by Type
You can set the autowire attribute of the sequenceGenerator bean to byType and leave the
prefixGenerator property unset. Then Spring will attempt to wire a bean whose type is compatible
with PrefixGenerator . In this case, the datePrefixGenerator bean will be wired automatically.
<beans ...>
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator"
autowire="byType">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>
<bean id="datePrefixGenerator"
class="com.apress.springenterpriserecipes.sequence. DatePrefixGenerator ">
<property name="pattern" value="yyyyMMdd" />
</bean>
</beans>
The main problem of auto-wiring by type is that sometimes there will be more than one bean in the
IoC container compatible with the target type. In this case, Spring will not be able to decide which bean
is most suitable for the property, and hence cannot perform auto-wiring. For example, if you have
another prefix generator generating the current year as the prefix, auto-wiring by type will be broken
immediately.
<beans ...>
<bean id="sequenceGenerator"
class="com.apress.springenterpriserecipes.sequence.SequenceGenerator"
autowire="byType">
<property name="initial" value="100000" />
<property name="suffix" value="A" />
</bean>
<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>
Search WWH ::




Custom Search