Spring Bean Autowire byName, byType, constructor and default Example

By Arvind Rai, March 13, 2023
This page will walk through spring bean autowire byName, byType, constructor and default Example. The autowire is a property of <bean> element. This property defines how the autowing should be done. The values of autowire property are byName, byType, constructor, no and default.

byName : Spring container looks for bean name same as property name of the class for autowiring.
byType : Spring container selects the bean by class type for autowiring.
constructor : Spring container uses constructor based autowiring.
no : No Autowiring. Use ref property to resolve dependency.
default : The default autowiring is "no". Default autowiring will inherit parent bean autowiring if nested.

In XML configuration file, we use autowire property as follows.
<bean name="employee" class="com.concretepage.bean.Employee" autowire="byName">
 
The autowire property is using byname value.
<bean name="employee" class="com.concretepage.bean.Employee" autowire="byType">
 
The autowire property is using byType value.
<bean name="employee" class="com.concretepage.bean.Employee" autowire="constructor">
 
The autowire property is using constructor value.
<bean name="employee" class="com.concretepage.bean.Employee" autowire="default">
 
The autowire property is using default value.

byName

In case of byName autowire, spring container looks for bean in XML configuration that name is same as class property name. If there is more than one bean of same class with different bean name in our XML configuration, the autowiring will not conflict and take the matching bean name with class property name. Find the class being used as bean in our example.
Employee.java
package com.concretepage.bean;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
	private String empName;
	@Autowired
	private Address address;
	public String getEmpName() {
		return empName;
	}
	public void setEmpName(String empName) {
		this.empName = empName;
	}
	public Address getAddress() {
		return address;
	}
	public void setAddress(Address address) {
		this.address = address;
	}
}  
The class has two properties, out of which address property has been annotated with @Autowired. Find the XML configuration file where we are defining the beans for autowiring.
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean name="address" class="com.concretepage.bean.Address">
    	<property name="city" value="Varanasi"/>
    	<property name="state" value="Uttar Pradesh"/>
    </bean>   
    <bean name="address2" class="com.concretepage.bean.Address">
    	<property name="city" value="Bhopal"/>
    	<property name="state" value="Madhya Pradesh"/>
    </bean>  	 
    <bean name="employee" class="com.concretepage.bean.Employee" autowire="byName">
	<property name="empName" value="Manohar Parikar"/>
    </bean>    
</beans>  
In our XML configuration file, we have created two beans with name address and address2 which is using same Address class but has been initialized with different property values. In our Employee class, the property name for Address class is address. The bean employee has been defined as autowire="byName". So the autowiring in Employee class, will be performed using address bean. Find the Address class used in our example.
Address.java
package com.concretepage.bean;
public class Address {
	private String city;
	private String state;
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
}  
Find the main class to test the application.
SpringDemo.java
package com.concretepage;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.concretepage.bean.Employee;
public class SpringDemo {
	public static void main(String[] args) {
		AbstractApplicationContext  context = new ClassPathXmlApplicationContext("spring-config.xml");
		Employee employee=(Employee)context.getBean("employee");
		System.out.println(employee.getEmpName());		
		System.out.println(employee.getAddress().getCity());
		System.out.println(employee.getAddress().getState());
                context.close();
    } 
}  
Find the output.
Manohar Parikar
Varanasi
Uttar Pradesh  
In case container does not find any matching bean by name for autowiring, it will not through any error.

byType

In case of byType autowiring, spring container looks for the class type. If in our XML configuration, there is more than one eligible candidate by class type for autowiring, the container will through error. There are three scenarios that may happen in case of by type autowiring.
1. If in the container, there is only one bean of required class type then autowiring is performed.
2. If there is more than one bean of same class type in the container, a fatal error is thrown and autowiring is not performed.
3. If there is no bean of required class type in the container, obviously no autowiring performed and also no error is thrown.

Now find the XML configuration file for byType autowiring example.
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.concretepage.bean.Address">
    	<property name="city" value="Vadodara"/>
    	<property name="state" value="Gujraat"/>
    </bean>   
    <bean name="employee" class="com.concretepage.bean.Employee" autowire="byType">
	<property name="empName" value="Narendra Modi"/>
    </bean>    
</beans>  
Here we have two beans one for Address and one for Employee class. The Employee has been set by type autowiring using attribute autowire="byType". Autowiring for address of Employee is performed.

With constructor

constructor autowiring is the analogous to byType autowiring. In case of constructor autowiring, spring container fulfills constructor argument autowiring only.
1. If in spring container, only one bean of autowiring candidate for constructor argument is present, constructor based dependency is performed.
2. If there is more than one bean of same class type, autowiring will not be performed and fatal error is thrown.
3. If there is no bean of required class type for constructor autowiring, error is thrown.

Find the class used in our example for constructor based dependency injection.
Employee.java
package com.concretepage.bean;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee {
	private String empName;
	private Address address;
	@Autowired
	public Employee (Address address, String empName) {
		this.address = address;
		this.empName = empName;
	}
	public String getEmpName() {
		return empName;
	}
	public Address getAddress() {
		return address;
	}
}  
The constructor of the class has been autowired. Now find the XML configuration for constructor autowiring.
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean class="com.concretepage.bean.Address">
    	<property name="city" value="Varanasi"/>
    	<property name="state" value="Uttar Pradesh"/>
    </bean>   
    <bean name="employee" class="com.concretepage.bean.Employee" autowire="constructor">
	<constructor-arg name="empName" value="Manohar Parikar"/>
    </bean>     
</beans>  
In XML configuration, we have two beans, one for Address and one for Employee class. The bean for Employee has been configured with autowire="constructor". As the constructor of Employee class has two arguments, Address type will be autowired and second argument has been configured with bean definition.

no and default

no autowire means dependency injection will not be achieved using @Autowired annotation, it will be achieved using ref attribute. "default" autowiring is "no". But when the bean is nested bean, then the default autowiring is parent bean autowiring. Suppose we have Employee class as follows.
Employee.java
package com.concretepage.bean;
public class Employee {
	private String empName;
	private Address address;
	public Employee (Address address, String empName) {
		this.address = address;
		this.empName = empName;
	}
	public String getEmpName() {
		return empName;
	}
	public Address getAddress() {
		return address;
	}
}  
Using XML configuration, we resolve dependency injection as follows.
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="address" class="com.concretepage.bean.Address">
    	<property name="city" value="Varanasi"/>
    	<property name="state" value="Uttar Pradesh"/>
    </bean>   
    <bean id="employee" class="com.concretepage.bean.Employee">
	<constructor-arg name="address" ref="address"/>
	<constructor-arg name="empName" value="Manohar Parikar"/>
    </bean>    
</beans>  
In Employee class constructor, the reference for address argument has been achieved by using ref.

In case of "no" autowiring settings if we want to use @Autowired in Employee class, use the following tag in XML configuration.
<context:annotation-config/>
 
XML configuration file will look like as below.
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
    <context:annotation-config/>
    <bean id="address" class="com.concretepage.bean.Address">
    	<property name="city" value="Varanasi"/>
    	<property name="state" value="Uttar Pradesh"/>
    </bean>   
    <bean id="employee" class="com.concretepage.bean.Employee">
	<constructor-arg name="empName" value="Manohar Parikar"/>
    </bean>    
</beans>  

default-autowire Property

default-autowire is used at parent <beans> tag level. Using default-autowire we change the default autowire of bean in our XML configuration. We can change the default autowire of a particular bean by using autowire property at bean tag level. Find the XML configuration.
spring-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byType">

    <bean name="address"  class="com.concretepage.bean.Address" autowire="byName">
    	<property name="city" value="Varanasi"/>
    	<property name="state" value="Uttar Pradesh"/>
    </bean>   
    <bean name="employee" class="com.concretepage.bean.Employee">
	<property name="empName" value="Manohar Parikar"/>
    </bean>    
</beans>  
In the above XML file, parent <beans> tag is using default-autowire="byType". So all the child <bean> tag has default autowiring byType. The address bean is using autowire="byName", so its autowiring type is byName and employee bean autowiring type is byType as default.

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us