Automatic assembly of beans

Time:2021-12-6
  • 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]

test

Create three entity classes cat, dog and people

public class Cat {
    public void shout(){
        System.out.println("miao~");
    }
}
public class Dog {
  public void shout(){
  System.out.println("wang~");
  }
}
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="http://www.springframework.org/schema/beans"
       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="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"/>
    </bean>
</beans>

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"/>
    </bean>

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.

Summary:

  • 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="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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- Enable annotation support -- >
    <context:annotation-config/>
    
    <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"/>
    </beans>

Annotations on entity classes

public class People {
    @Autowired
    @Qualifier(value = "cat111")
    //The above two effects are equal to @ resource (value = "cat111")
    private Cat cat;
    @Autowired
    @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);
        people.getDog().shout();
        people.getCat().shout();
  • @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