Automatic assembly of beans

  • Automatic assembly is a way for spring to meet bean dependencies!
  • Spring will automatically find it in the context and automatically assemble properties for the bean

There are three ways to assemble in spring

  • Configuration shown in XML
  • Display configuration in Java
  • Implicit auto assembly bean [important]


Create three entity classes cat, dog and people

public class Cat {
    public void shout(){
public class Dog {
  public void shout(){
public class People {
    private Cat cat;
    private Dog dog;
    private String name;

Originally, our configuration is the configuration displayed in XML, that is, the first method mentioned above

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
       xmlns:xsi=""   xsi:schemaLocation="">

    <bean id="catt" class="com.hudu.pojo.Cat"/>
    <bean id="dogg" class="com.hudu.pojo.Dog"/>

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

However, our cat and dog use these two entity classes. How can spring assemble automatically without manual assembly? Here we talk about the third method above. We can use autowire. There are two implementation methods, one is bytype and the other is byname. We can see the difference between them from the name.

    Byname: the beanid corresponding to the value after the set method of your own object will be found in the container context!
    Bytype: the bean with the same property type as its own object will be found in the container context!
    <bean id="people" class="com.hudu.pojo.People" autowire="byName">
    <!--<bean id="people" class="com.hudu.pojo.People" autowire="byType">-->
        <property name="name" value="hudu"/>

However, both have disadvantages. For byname, if the bean ID configuration name is inconsistent with the method name behind the set method, it cannot be assembled automatically. For bytype, you don’t need to configure bean ID, but if you configure the same bean of multiple object properties, it will not be assembled automatically.


  • Byname, you need to ensure that the IDs of all beans are unique, and the bean needs to be consistent with the value of the set method of the automatically injected attribute
  • When bytype, you need to ensure that the classes of all beans are unique, and the types of mature attributes that need to be automatically injected are consistent

Automatic assembly using annotations

One way to realize automatic assembly has been mentioned above, and the following is another way to realize automatic assembly through annotation.
JDK1.5 supports annotations. Srping supports annotations from 2.5
To use notes:

  • Import constraint, context constraint

  • Configure annotation support, i.e. < context: annotation config / >

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    <!-- Enable annotation support -- >
    <bean id="cat1" class="com.hudu.pojo.Cat"/>
    <bean id="cat111" class="com.hudu.pojo.Cat"/>
    <bean id="dog1" class="com.hudu.pojo.Dog"/>
    <bean id="dog222" class="com.hudu.pojo.Dog"/>
    <bean id="people" class="com.hudu.pojo.People"/>

Annotations on entity classes

public class People {
    @Qualifier(value = "cat111")
    //The above two effects are equal to @ resource (value = "cat111")
    private Cat cat;
    @Qualifier(value = "dog222")
    private Dog dog;
    private String name;
If Autowired cannot uniquely auto assemble attributes, you need to pass @ qualifier (value = "XXX")

@The nullable field marks this annotation, indicating that this field can be null

Similar to @ Autowired, the Java Native annotation @ resource will be searched first by name and then by type. You can specify the name @ resource (name = "cat2")

Test class

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        People people = context.getBean("people", People.class);
  • @Autowired
    It can be used directly on attributes or in set mode
    Using Autowired, we don’t need to write the set method. The premise is that your auto assembled attribute exists in the i0c (spring) container and conforms to the name byname!

Summary: @ resource and @ Autowired are different:

  • They are used for automatic assembly and can be placed in the attribute field
  • @Autowired is implemented by bytype, and this object must exist! [common]
  • @Resource is implemented by byname by default. If the name cannot be found, it is implemented by bytype! If both cannot be found, an error will be reported! [common]
  • Different execution order: @ Autowired is implemented by [email protected] Resource is implemented by byname by default.

This work adoptsCC agreement, reprint must indicate the author and the link to this article