Spring Learning Notes - Auto-assemble Bean

Spring Learning Notes - Auto-assemble Bean

  • Auto-assembly is one way Spring meets bean dependencies

  • Spring automatically finds and assembles properties for bean s in the context

There are three ways to assemble in Spring

  1. Explicit Configuration in xml
  2. Explicit Configuration in Java
  3. Implicit automatic assembly of bean s

ByName

Auto-assemble by parameter name, beanId corresponding to the value following the set method of your object is automatically found in the container context when the container finds that the Autowire property has been changed to byName

Disadvantage: beanId cannot be assembled if it does not match the name after the set method

<bean id="human" class="com.alb.pojo.Human" autowire="byName">
    <property name="name" value="Alb"/>
</bean>

ByType

Auto-assembly by parameter type, when the container finds that the Autowire property has been changed to byType, it automatically looks for the same bean in the container context as its own object type property

Disadvantage: If there are multiple bean s of the same type, an exception will be thrown

<bean id="human" class="com.alb.pojo.Human" autowire="byType">
    <property name="name" value="Alb"/>
</bean>

Auto-assembly using annotations

Prerequisites for using notes:

  • Import Constraints: context Constraints
  • Support for configuration annotations: <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
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

@Autowired

It can be used directly on attributes or on set Methods

setter methods are not required after using @Autowired, but only if this auto-assembled property exists in the IOC(Spring) container and matches the name byType

<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

    <bean id="cat" class="com.alb.pojo.Cat"/>
    <bean id="dog" class="com.alb.pojo.Dog"/>
    <bean id="human" class="com.alb.pojo.Human" autowire="byType"/>

</beans>

Test class:

public class MyTest {
    @Test
    public void test(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Human human = context.getBean("human", Human.class);

        human.getCat().bark();
        human.getDog().bark();
    }
}

Expand:

  • If the require property of AutoWired is explicitly defined as false, the object can be null, otherwise it cannot be empty
  • @Nullable: If the field is marked with this comment, the field can be empty
  • If the @Autowired auto-assembly environment is complex, such as multiple beans of the same type with different id s, and auto-assembly cannot be accomplished by one annotation (@Autowired), we can use @Qualifier(value = "xxx(beanId)) to coordinate the use of @Autowired, specifying a unique bean object injection

The difference between @Resource and @Autowired:

  • They are used for automatic assembly and can be placed on property fields

  • @Autowired is implemented by byType and must require object existence to be changed

  • @Resource is implemented by default by byName, byType if no name is found, and error if neither is found

  • Execution order is different

Keywords: Spring bean

Added by cello on Mon, 24 Jan 2022 23:01:39 +0200