Friday, 5 January 2018

What is Autowiring in Spring ? Explain Autowiring modes and limitations with examples

       In Previous post, we discussed about Spring IOC Containers and differences between them, today we will discuss about Autowiring and it's modes and limitations in Spring.
 
       Usually we provide bean configuration details in the spring bean configuration file  and we also specify the beans that will be injected in other beans using ref attribute.  But Spring framework provides autowiring features too where we don’t need to provide bean injection details explicitly.

        Autowiring is to injects the dependencies without having to specify those explicitly. Spring inspects bean factory contents and establishes relationships amongst collaborating beans.

Spring can provide five modes of Autowiring as follows,

Spring autowiring modes

  • no:

It is default which define no autowiring, set it manually via “ref” attribute.

Ex: Using "ref" attribute

<bean id="chat" class="com.test.Chat">
   <property name="messageType" ref="message">
   </property>
</bean>
<bean id="message" class="com.test.Message" >
</bean>


  • byName:

Auto wiring by property name. If the name of a bean is same as the name of other bean property, auto wire it.

Ex:
 Below code, the name of “message” bean is same with the name of the “chat” bean’s property message. So, Spring will auto wired it via setter method – “setMessage(Message message)”.

<bean class="com.test.Chat" id="chat" autowire="byName">
</bean>
<bean class="com.test.Message" id="message">
</bean>

Chat.java,

public class Chat{

    private Message message;

    // other properties

    public void setMessage(Message message){

       this.message = message;

    }

}


  • byType:

Auto wiring by property data type. If data type of a bean is compatible with the data type of other bean property, auto wire it.

Ex:- Below code, the data type of “message” bean is same with the data type of “chat” bean’s property message. So, Spring will auto wired it via setter method – “setMessage(Message message)”.

Configuration file,

<bean class="com.test.Chat" id="chat" autowire="byType">
</bean>
<bean class="com.test.Message" id="message">
</bean>

Chat.java,

public class Chat{

    private Message message;

    // other properties

    public void setMessage(Message message){

       this.message = message;

    }

}


  • constructor:

byType mode in constructor argument.

 Ex: - In below example, the data type of “chat” bean is same as the constructor argument data type in
“message” bean’s property (Message message), so, Spring auto wired it via constructor method – “public Chat(Message message)”.

<bean class="com.test.Chat" id="chat" autowire="constructor">
</bean>
<bean class="com.test.Message" id="message">
</bean>

Chat.java,

public class Chat{

    private Message message;

    // other properties

    public Chat(Message message){

       this.message = message;

    }

}


  • autodetect:
If a default constructor is found, use “autowired by constructor”; Otherwise, use “autowire by type”.

Limitations with Autowiring:--
  1. Explicit dependencies in constructor-argument and property settings always override autowiring. You cannot autowire simple properties such as primitives, Strings, and Classes.
  2. Overriding possibilities: We can define dependencies using property or constructor-args tag which will always override autowiring.
  3. Primitive data type: We have to define primitive data types String or Integer using property or constructor-args tag.  You cannot autowire these tags.
  4. Confusing Nature: If you have lot of dependency in a program, then it’s hard to find using autowire attribute of bean.

Related Posts:--

No comments:

Post a Comment