Java Reference
In-Depth Information
Spring will throw an UnsatisfiedDependencyException if more than one bean is found for auto-
Exception in thread "main"
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating
bean with name 'sequenceGenerator' defined in class path resource [beans.xml]:
Unsatisfied dependency expressed through bean property 'prefixGenerator': No unique
bean of type [com.apress.springenterpriserecipes.sequence.PrefixGenerator]
is defined:
expected single matching bean but found 2: [datePrefixGenerator,
Auto-Wiring by Name
Another mode of auto-wiring is byName , which can sometimes resolve the problems of auto-wiring by
type. It works very similarly to byType , but in this case, Spring will attempt to wire a bean whose class
name is the same as the property name, rather than with the compatible type. As the bean name is unique
within a container, auto-wiring by name will not cause ambiguity.
<beans ...>
<bean id="sequenceGenerator"
<property name="initial" value="100000" />
<property name="suffix" value="A" />
<bean id=" prefixGenerator"
<property name="pattern" value="yyyyMMdd" />
However, auto-wiring by name will not work in all cases. Sometimes it's not possible for you to
make the name of the target bean the same as your property. In practice, you often need to specify
ambiguous dependencies explicitly, while keeping others auto-wired. That means you employ a mixture
of explicit wiring and auto-wiring.
Auto-Wiring by Constructor
The auto-wiring mode constructor works like byType , but it's rather more complicated. For a bean
with a single constructor, Spring will attempt to wire a bean with a compatible type for each constructor
argument. But for a bean with multiple constructors, the process is more complicated. Spring will first
attempt to find a bean with a compatible type for each argument of each constructor. Then it will pick
the constructor with the most matching arguments.
Suppose that SequenceGenerator has one default constructor and one constructor with an argument
PrefixGenerator .
Search WWH ::

Custom Search