Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Autowire in Spring Framework
Previous Home Next

Introduction:An autowiring is the facility provided by framework to satisfied by dependency of manage bean without configuration information. It means that the autowiring solved problem to write very long configuration information.

Type of method to Autowire

The following four method are used by IOC container to autowire bean dependency.

  1. Autowire by Type
  2. Autowire by Name
  3. Autowire by Constructor
  4. Autowire by autodetect

Autowire by Type: In autowiring by type , The IOC container used the bean property by type to identified the beans to be injected i.e in order to used autowiring by type we need to simply configure one bean for each property of the target bean.

Note: Facility of autowiring are used only with object type dependency i.e primitive dependency are note autowire.

Syntax :

 <bean id="x" class="org.r4r.A"/>
<bean id="b" class="org.r4r.B" autowire="byType"/>
Disadvantage of Autowire
  1. Optional dependency are specified using by type because dependency are injected by setter method.
  2. Autowiring by type can not be used if there are more than one beans. Configured for a properties type.

Autowire by Name: In this case IOC container uses the name of a bean property to fined out the bean for injecting i.e in this approach the configuration bean to be used for satisfied the dependency of the bean must have the same as the bean property.

Syntax:

<bean id="a" class="org.r4r.A"/>
<bean id="y" class="org.r4r.A"/>
<bean id="b" class="org.r4r.B" autowire="byName"/>

Disadvantage of Autowire

  1. Only optional dependency can be autowire for this approach.
  2. This approach would work of a bean of different type is configure using the same name as the property of a bean which uses autowiring.

Autowire by Constructor: In this approach type of parameter of the constructor are used to identified the bean to be injected in case of overloaded constructed has a maximum parameter will choose. This approach is same as autowiring by type accept dependency are injected through constructor in it.

Syntax:

 <bean id="a" class="org.r4r.A"/>
<bean id="b" class="org.r4r.B" autowire="constructor"/>

Autowire by Autodetect:This approach is a combination of autowiring by constructor and autowiring by type in it dependency is injected using the type of property if target bean has a default constructor and dependency are injected using constructor injection if target bean has a parameterize constructor.

Note: This is the default strategy of the autowiring.

Syntax:

<bean id="x" class="org.r4r.A"/>
<bean id="b" class="org.r4r.B" autowire="autodetect"/>

Limitation of Autowiring: Major drawback of autowiring is understandability is loosed.

Previous Home Next