Autowire byName in Spring | Spring Bean Autowire byName Example

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn about Spring bean autowire byName. Wiring in spring is nothing but configuring the dependencies of a bean in the XML file.
Generally, if we want to inject the dependencies into a bean, we need to wire the dependencies explicitly by using the ref attribute. like below
<bean id="travelBean" class="Travel">
  <property name="car" ref="carBean" />
</bean>
<bean id="carBean" class="Car">
  <property name="carName" value="BMW"></property>
</bean>
By making use of the autowire mechanism in spring, we no need to wire the dependencies explicitly. Just by using the autowire strategies, spring will automatically inject the dependencies.
Spring has given an autowire property in the <bean> tag to wire the dependencies. We have 4 different types of values associated with the autowire attribute. Those are autowire byName, byType, constructor, and auto-detect.
Let’s understand the autowire byName.
In spring, autowire byName is one of the strategies in bean autowiring strategies. In this strategy, while injecting the properties spring container verifies whether a property name of bean class and bean id in XML are matched or not. If matched then that property will be injected.
Spring IOC always uses the setter injection in case of autowire byName.
Create a Car Bean :
public class Car {
    private String carName;
    private String carType;

    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public String getCarType() {
        return carType;
    }

    public void setCarType(String carType) {
        this.carType = carType;
    }

}
Create a Travel Bean :
public class Travel {
    String name;
    String type;
    private Car car;

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
Spring XML configuration :
spring.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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <bean id="travelBean" class="Travel" autowire="byName">
        <property name="name" value="Agarwal Travels" />
        <property name="type" value="International" />
    </bean>
    <bean id="car" class="Car">
    <property name="carName" value="Audi"/>
    <property name="carType" value="Suv"/>
    </bean>
</beans>
On the above XML in line number 6, we declare a travel bean with autowire="byName" and we inject name and type property values. But we didn’t inject any property for car bean in travel <bean>.
But, since it is declared as autowire byName, the container checks whether the property name car is mating with any bean id in XML, if it matches, that bean will be injected automatically. In this case, the car is injected to Travel bean, since the property name in Travel class car is matched with bean id in the car <bean> in XML.
If we remove the autowire="byName" attribute from travel <bean>. We should get NullPointerException since it is not defined.
Run the Application :
Main.java
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public final class Main {

    public static final void main(String[] args) {
        BeanFactory factory = new XmlBeanFactory(new ClassPathResource("spring.xml"));
        Travel travel = (Travel)factory.getBean("travelBean");
        String carName = travel.getCar().getCarName();

        System.out.println("Travel Name : "+travel.getName());
        System.out.println("Travel Type : "+travel.getType());
        System.out.println("Car Name : "+carName);
    }
}
Output :
Travel Name : Agarwal Travels
Travel Type : International
Car Name : Audi
Complete Example is available for download : Spring Bean Autowire byName Example
Happy Learning 🙂


Spring Bean Autowire byName Example

File size: 9 KB
Downloads: 673


Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByType Example
Types of Spring Bean Scopes Example
@Autowired,@Qualifier,@Value annotations in Spring
@Qualifier annotation example in Spring
Spring Collection Dependency List Example
Spring Java Configuration Example
Spring Collection Map Dependency Example
Spring AOP Around Advice Example XML
spring expression language example
Spring AOP Example Before and After Advice XML
Spring JdbcTemplate CRUD Application
Spring Hibernate Example
Spring AOP ThrowsAdvice Example XML
Dependency Injection (IoC) in spring with Example

Spring Bean Autowire By Constructor Example
Spring Bean Autowire ByType Example
Types of Spring Bean Scopes Example
@Autowired,@Qualifier,@Value annotations in Spring
@Qualifier annotation example in Spring
Spring Collection Dependency List Example
Spring Java Configuration Example
Spring Collection Map Dependency Example
Spring AOP Around Advice Example XML
spring expression language example
Spring AOP Example Before and After Advice XML
Spring JdbcTemplate CRUD Application
Spring Hibernate Example
Spring AOP ThrowsAdvice Example XML
Dependency Injection (IoC) in spring with Example