Tuesday, March 1, 2011

What do you mean by Auto Wiring


What do you mean by Auto Wiring
The Spring container is able to auto wire relationships between collaborating beans. This means that it is possible to automatically let Spring collaborate your beans by inspecting the contents of the BeanFactory. The auto wiring functionality has five modes.
  • no – No auto-wiring. This is the default mode; you have to wire your bean explicitly by using the ‘ref’ attribute.
  • byName – Auto-wire a bean whose name is same as the property.
  • byType – Auto-wire a bean whose data type is compatible with the property. The problem is sometimes there will be more than one beans are match with ‘byType’ criteria. In this case, Spring will throw an UnsatisfiedDependencyException exception.
  • constructor – Auto-wire a bean whose date type is compatible with the property constructor argument. The ‘constructor’ mode is facing the same problem with ‘byType’ auto-wire mode
  • autodetect – If a default constructor with no argument is found, it will auto-wire by data type. Otherwise, it will auto-wire by constructor. It has the same problem with ‘byType’ and ‘constructor’,
It’s always good to combine the ‘auto-wire’ and ‘dependency-check’ feature together to make sure the property is auto-wire successfully.
e.g.      <bean id="CustomerBean" class="com.mkyong.common.Customer" 
                                    autowire="autodetect" dependency-check="objects">
                    <property name="action" value="buy" />
                    <property name="type" value="1" />
           </bean>
           <bean id="PersonBean" class="com.mkyong.common.Person">
                    <property name="name" value="mkyong" />
                    <property name="address" value="address 123" />
                    <property name="age" value="28" />
            </bean>

1 comment:

  1. you didn't explain shit..you just copied the autowire properties description from Spring.., please explain what wiring means, give examples, then explain what autowire means, how does this help me, and why should I use it..I don't have the answer, that's why I'm asking :)

    ReplyDelete