Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between autowire byName and byType in Spring

    • 0
    • 1
    • 1
    • 1
    • 3
    • 0
    • 0
    • 0
    • 8.39k
    Comment on it

    Spring framework provides the way to inject the bean dependencies using the autowire functionality. By default autowire functionality is disabled in spring framework. A developer explicitly define the autowiring configuration in spring configuration xml file. And this point is to remember that autowiring is only supported for object dependencies.

    The difference between byType and byName autowiring is as follows :

    1. Autowire byType will search for a bean in configuration file, whose id match with the property type to be wired whereas autowire byName will search for a bean whose id is matching with the property name to be wired.
    2. As a syntax wise difference is as follows:

      package com.evon;

      public class Company { 
         private Employee emp; 
         public String setEmp(Employee emp) { 
          this .emp = emp; 
          }          
      }
      

    in configuration file :

    <bean id="beanId1" class="com.evon.Company " autowire="byType"/>
    <bean id="employee" class="com.evon.Employee ">
        <property name="empName" value="Rahul" />
    </bean>
    

    In the above example container will search for a employee bean in the configuration xml file which should be of type Employee.

    And following is the example of autowire byName where container will search for a bean which should be register with the id name same as filed name ('emp') in configuration file. Like

    <bean id="beanId2" class="com.evon.Company" autowire="byName"/>
    <bean id="emp" class="com.evon.Employee ">
        <property name="empName" value="Rahul" />
    </bean>
    

 3 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: