Different Modes of Autowiring
Spring supports four modes for autowiring: byName, byType, constructor, default, and no (which is the
default). When using byName autowiring, Spring attempts to wire each property to a bean of the same
name. So, if the target bean has a property named foo and a foo bean is defined in the
ApplicationContext, the foo bean is assigned to the foo property of the target.
When using byType autowiring, Spring attempts to wire each of the properties on the target bean
automatically using a bean of the same type in the ApplicationContext. So, if you have a property of type
String on the target bean and a bean of type String in the ApplicationContext, then Spring wires the
String bean to the target bean's String property. If you have more than one bean of the same type, in
this case String, in the same ApplicationContext, then Spring is unable to decide which one to use for
the autowiring and throws an exception (of type
org.springframework.beans.factory.NoSuchBeanDefinitionException).
The constructor autowiring mode functions just like byType wiring, except that it uses constructors
rather than setters to perform the injection. Spring attempts to match the greatest numbers of arguments
it can in the constructor. So, if your bean has two constructors, one that accepts a String and one that
accepts a String and an Integer, and you have both a String and an Integer bean in your
ApplicationContext, Spring uses the two-argument constructor.
In default mode, Spring will choose between constructor and byType modes automatically. If your
bean has a default (no-arguments) constructor, then Spring uses byType; otherwise, it uses constructor.
Listing 4-74 shows a simple configuration that autowires three beans of the same type using each of
the different modes (autowiring.xml).
Listing 4-74. Configuring Autowiring
<bean id="foo" class="com.apress.prospring3.ch4.autowiring.Foo"/>
<bean id="bar1" class="com.apress.prospring3.ch4.autowiring.Bar"/>
<bean id="targetByName" autowire="byName"
class="com.apress.prospring3.ch4.autowiring.Target"
lazy-init="true"/>
<bean id="targetByType" autowire="byType"
class="com.apress.prospring3.ch4.autowiring.Target"
lazy-init="true"/>
<bean id="targetConstructor" autowire="constructor"
class="com.apress.prospring3.ch4.autowiring.Target"
lazy-init="true"/>
This configuration should look very familiar to you now. Foo and Bar are empty classes. Notice that
each of the Target beans has a different value for the autowire attribute. Moreover, the lazy-init
attribute was set to true to inform Spring to instantiate the bean only when it is first requested, rather
than at startup, so that we can output the result in the correct place in the testing program. Listing 4-75
shows a simple Java application that retrieves each of the Target beans from the ApplicationContext.
Listing 4-75. Autowiring Collaborators
package com.apress.prospring3.ch4.autowiring;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Target {
private Foo foo;
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home