Bean assembly method

Posted by neo0506 on Thu, 03 Feb 2022 05:54:20 +0100

There are three assembly methods in Spring:

  1. Explicit configuration in xml
  2. Explicit configuration in java
  3. Implicit auto assembly bean

Explicit configuration in xml

quote How spring IOC creates objects

Configuring object properties in an xml file

Explicit configuration in java

quote Derivation of spring IOC theory

When actually calling the dao layer, you can display which dao interface is selected for the configuration

Implicit auto assembly bean

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

byName auto assembly

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

<!--
byName:It will automatically find and own objects in the container context set The value after the method corresponds to beanid!
-->
<bean id="people" class="com.sunfl.pojo.People" autowire="byName">
    <property name="name" value="Sunflower"/>
</bean>

byType auto assembly

<bean id="cat" class="com.sunfl.pojo.Cat"/>
<bean id="dog111" class="com.sunfl.pojo.Dog"/>

<!--
byType:It will automatically find the object with the same attribute type as its own object in the container context bean!
-->
<bean id="people" class="com.sunfl.pojo.People" autowire="byType">
    <property name="name" value="Sunflower"/>
</bean>

Summary:

  • When byName is used, the id of all beans needs to be unique, and the bean needs to be consistent with the value of the automatically injected property set method
  • When byType, you need to ensure that the class of all beans is unique, and the bean needs to be consistent with the attribute type automatically injected

Automatic assembly using annotations

jdk1.5 support annotations, spring 2 5 supporting notes

  1. Import constraint: context constraint
  2. Configuration annotation support:

    <?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"
        xmlns:aop="http://www.springframework.org/schema/aop"
        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
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd">
    
     <bean id="cat" class="com.sunfl.pojo.Cat"/>
     <bean id="dog" class="com.sunfl.pojo.Dog"/>
     <bean id="people" class="com.sunfl.pojo.People"/>
    
     <!--Enable annotation support-->
     <context:annotation-config/>
    
    </beans>
    

@Autowired
It can be used directly on attributes or set methods
Using Autowired, we don't need to write the Set method. The premise is that your auto assembled attribute exists in the IOC(Spring) container and conforms to the name byName

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

public class People {

    @Autowired
    @Qualifier(value = "cat111")
    private Cat cat;
    @Autowired
    @Qualifier(value = "dog222")
    private Dog dog;
    private String name;
}

@Resource annotation

public class People {

    @Resource(name = "cat2")
    private Cat cat;

    @Resource
    private Dog dog;
    private String name;
}

Summary:
@Difference between Resource and @ Autowired:

  • They are used for automatic assembly and can be placed in the attribute field
  • @Autowired is implemented by byType [common]
  • @Resource is implemented by byName by default. If the name cannot be found, it is implemented by byType

Topics: Spring ioc bean