Spring series [6, Bean automatic assembly]

6. Bean automatic assembly

  • Autowiring is a way for Spring to satisfy bean dependencies
  • Spring will automatically look for it in the context and automatically assemble properties for the bean

There are three ways of assembly in Spring

1. Configuration displayed in xml

2. Display configuration in java

3. Implicit automatic assembly of beans [important]

.

6.1, automatic assembly of byName and byType

Environment build

  • A person has two pets: people–>cat/dog
public class Cat {
    
    
    public void shout() {
    
    
        System.out.println("喵");
    }
}
public class Dog {
    
    
    public void shout() {
    
    
        System.out.println("汪");
    }
}
public class People {
    
    
    private Cat cat;
    private Dog dog;
    private String name;
}

6.1.1, byName automatic assembly**

    <bean id="cat" class="com.only.pojo.Cat"/>
    <bean id="dog" class="com.only.pojo.Dog"/>
    
<!--    <bean id="people" class="com.only.pojo.People">-->
<!--        <property name="name" value="吴夜"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->

<!--  byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid  -->
    <bean id="people" class="com.only.pojo.People" autowire="byName">
        <property name="name" value="吴夜"/>
    </bean>

6.1.2, byType automatic assembly**

    <bean class="com.only.pojo.Cat"/>
    <bean id="dog111" class="com.only.pojo.Dog"/>
    
<!--    <bean id="people" class="com.only.pojo.People">-->
<!--        <property name="name" value="吴夜"/>-->
<!--        <property name="cat" ref="cat"/>-->
<!--        <property name="dog" ref="dog"/>-->
<!--    </bean>-->

    <!--
      byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
      byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
      -->
    <bean id="people" class="com.only.pojo.People" autowire="byType">
        <property name="name" value="吴夜"/>
    </bean>

summary:

  • byName, it is necessary to ensure that the id of all beans is unique, and this bean needs to be consistent with the value of the set method of the automatically injected property
  • byType, it is necessary to ensure that the class of all beans is unique, and this bean needs to be consistent with the type of the automatically injected property

6.2. Using annotations to realize automatic assembly

Notes on using annotations:

1. Import constraints

xmlns:context="http://www.springframework.org/schema/context"

2. Configuration annotation support 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">
        
    <context:annotation-config/>
    
</beans>

Below, learn about some of these annotations.

6.2.1, @Autowired [commonly used]

  • It can be used directly on the attribute, or it can be used in the set method
  • Using Autowired does not need to write a set method, provided that your autowired property exists in the IOC (Spring) container and conforms to the name byName.
  • Therefore, in 6.1, no longer use byName or byType for automatic assembly, and directly use annotations in the people class for automatic assembly:
public class People {
    
    

    @Autowired
    private Cat cat;
    @Autowired(required = false)
    private Dog dog;
    private String name;
}

Science:

@Nullable If the field is marked with this annotation, it means that the field can be null

6.2.2、@Qualifer

If the environment of @Autowired automatic assembly is complex and automatic assembly cannot be completed through an annotation [@Autowired], we can use Qualifer(value="xxx") to configure the use of @Autowired and specify a unique bean object injection.

public class People {
    
    
    @Autowired
    @Qualifier(value = "cat1")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog2")
    private Dog dog;
    private String name;
}

The configuration content is:

<!-- 开启注解的支持 -->
<context:annotation-config/>

<bean id="cat1" class="com.only.pojo.Cat"/>
<bean id="cat2" class="com.only.pojo.Cat"/>
<bean id="dog1" class="com.only.pojo.Dog"/>
<bean id="dog2" class="com.only.pojo.Dog"/>
<bean id="people" class="com.only.pojo.People"/>

6.2.2、@Resource

  • @Resource is the annotation injection in spring.
  • By default, it is assembled according to the name, and the name can be specified through the name attribute.
  • If the name attribute is not specified, when the annotation is written on the field, the field name is used by default to search by name
  • If the annotation is written on the setter method, the attribute name is used for assembly by default.
  • Wiring is done by type when no bean matching the name is found.
  • But it should be noted that if the name attribute is specified, it will only be assembled according to the name.
public class People {
    
    
    @Resource
    private Cat cat;
    @Resource(name = "dog2")
    private Dog dog;
    private String name;
}
<!-- 开启注解的支持 -->
<context:annotation-config/>

<bean id="cat" class="com.only.pojo.Cat"/>
<bean id="cat2" class="com.only.pojo.Cat"/>
<bean id="dog" class="com.only.pojo.Dog"/>
<bean id="dog2" class="com.only.pojo.Dog"/>
<bean id="people" class="com.only.pojo.People"/>

6.3 Summary

The difference between @Resource and @Autowired:

  • Both are used for automatic assembly, and can be placed on the attribute field
  • @Autowired is implemented by byType, and the object must be required to exist. [commonly used]
  • @Resource is implemented by byName by default, and byType if the name cannot be found. If neither of them can be found, an error will be reported. [commonly used]
  • The execution order is different: @Autowired is implemented by byType. @Resource is implemented by byName by default.

Guess you like

Origin blog.csdn.net/weixin_39379635/article/details/115741363