Bean Auto-Wiring in Spring

1. Example: One Person and Two Pets

1.1 Cat class

public class Cat {

    public void Cry() {
        System.out.println("Hiss");
    }

}

1.2 Dog class

public class Dog {
    public void Cry() {
        System.out.println("Bark");
    }

}

1.3 People class

public class People {
    private String name;
    private Cat cat;
    private Dog dog;

    public String getName() {
        return name;
    }

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

    public Cat getCat() {
        return cat;
    }

    public void setCat(Cat cat) {
        this.cat = cat;
    }

    public Dog getDog() {
        return dog;
    }

    public void setDog(Dog dog) {
        this.dog = dog;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", cat=" + cat +
                ", dog=" + dog +
                '}';
    }
}

1.4 beans.xml configuration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="cat" class="com.wjj.pojo.Cat" />
    <bean id="dog" class="com.wjj.pojo.Dog" />

    <bean id="people" class="com.wjj.pojo.People">
        <property name="name" value="577" />
        <property name="cat" ref="cat" />
        <property name="dog" ref="dog" />
    </bean>

</beans>

1.5 Test

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    @org.junit.Test
    public void test() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean(People.class);
        people.getCat().Cry();
        people.getDog().Cry();
    }
}

Image: test result

2. Auto-Wiring by Name vs. by Type

byName auto-wiring

<!-- byName auto-wiring: looks for a bean whose id matches the property name (derived from setter method) -->
<bean id="people" class="com.wjj.pojo.People" autowire="byName">
</bean>

byType auto-wiring

<!-- byType auto-wiring: looks for a bean whose type matches the property type -->
<bean id="people" class="com.wjj.pojo.People" autowire="byType">
</bean>

Summary

  • byName: The bean id must be unique and must exact mattch the name derived from the setter method of the property to be injected.
  • byType: The bean class must be unique and must match the type of the property to be injected.

3. Using Annotations for Auto-Wiring

3.1 Steps

  1. Import the context namespace constraint.
  2. Enable annotation support with <context:annotation-config />.

3.2 @Autowired annotation

  • Can be applied direct on a setter method or a field (using reflection).
  • Spring will look for a bean of the matching type. If multiple beans of the same type exist, it will fall back to matching by name.
  • Use @Qualifier(value = "beanId") to specify a particular bean when ambiguity arises.

Example:

@Autowired
@Qualifier(value = "cat1")
private Cat cat;

@Autowired
@Qualifier(value = "dog1")
private Dog dog;

Note: Using @Autowired on a field allows you to omit the setter method, as Spring uses reflection to inject the dependency directly.

Tags: Spring Auto-Wiring Dependency Injection

Posted on Tue, 12 May 2026 18:10:11 +0000 by frenchpl