Spring bean auto assembly

Posted by phpcharan on Tue, 28 Sep 2021 12:08:13 +0200

automatic assembly

Automatic assembly is a way to use spring to meet bean dependencies. Spring will find the bean that a bean depends on in the application context.

There are three assembly mechanisms for bean s in Spring:

  1. Explicit configuration in xml;
  2. Explicit configuration in java;
  3. Implicit bean discovery mechanism and automatic assembly.

Note: only the third kind is mentioned here!

ByName auto assembly

ByName auto assembly: autowire="byName"

In the process of manually configuring xml, errors such as missing letters and case often occur, which can not be checked, which reduces the development efficiency. Using automatic assembly will avoid these errors and simplify the configuration.

  • All set method names in its class, such as setCat, will be searched to obtain a string with set removed and lowercase, that is, cat. (null pointer if not found)
<bean id="cat" class="POJO.Cat"/>
<bean id="dog" class="POJO.Dog"/>


<bean id="people" class="POJO.People" autowire="byName">
    <property name="name" value="Zhang San"/>
</bean>

ByType auto assembly

ByType auto assembly: autowire="byType"

Using byType first needs to ensure that objects of the same type are unique in the spring container. If it is not unique, a non unique exception will be reported.

Because it is assembled by type, no exception will be reported and the final result will not be affected. Even removing the id attribute does not affect the result.

<bean class="POJO.Cat"/>
<bean class="POJO.Dog"/>


<bean id="people" class="POJO.People" autowire="byType">
    <property name="name" value="Zhang San"/>
</bean>

Annotation auto assembly

jdk1.5 began to support annotations, and spring 2.5 began to fully support annotations.

The postprocessors can be registered as separate bean definitions, but they can also be implicitly registered by including the following tags in the XML based Spring configuration (note that the context namespace is included):

<?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>

Annotation injection is performed before XML injection. Therefore, the XML configuration overrides the comments of the attributes connected through these two methods.

@Autowired

The set method can be removed, provided that the auto assembly attribute exists in the IOC(Spring) container and conforms to the name byName.

It can be used on properties or set methods!

@Autowired
private Dog dog;
@Autowired
private Cat cat;
// If @ Autowired is defined in the display (required = false) 
//required = false: the object can be null
//required = true: the object must be stored and cannot be null
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;

@Qualifier

This annotation cannot be used alone. It needs to be combined with @ Autowired.

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

@Autowired is automatically assembled according to the type. With @ Qualifier, it can be automatically assembled according to byName.

<bean id="cat" class="POJO.Cat"/>
<bean id="dog" class="POJO.Dog"/>
@Autowired
@Qualifier(value = "dog")
private Dog dog;
@Autowired
@Qualifier(value = "cat")
private Cat cat;

@Resource

  • @If the Resource has a specified name attribute, first search the assembly by name according to the attribute;
  • Secondly, assemble in the default byName mode;
  • If none of the above is successful, it will be assembled automatically by byType.
  • If they are not successful, an exception is reported.
<bean id="cat" class="POJO.Cat"/>
<bean id="dog" class="POJO.Dog"/>
@Resource(name = "dog")
private Dog dog;
private Cat cat;

Write at the end

@Similarities and differences between Autowired and @ Resource:

  1. @Both Autowired and @ Resource can be used to assemble bean s. Can be written on the field.
  2. @Autowired is implemented by byName, and this object must exist [common]
  3. @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 is reported [common]

They have the same function. They inject objects by annotation, but the execution order is different@ Autowired byType first, @ Resource byName first.

 

❤️ END ❤️

Topics: Java Spring bean