Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Spring Beans is automatically assembled


May 14, 2021 Spring


Table of contents


Beans is automatically assembled

You've <bean> how to declare a bean using the elements and inject them by using the <constructor-arg> in the XML profile, and the elements in <property> profile.

Spring containers can automatically <constructor-arg> the relationship between collaborative <property> without using the elements, which helps reduce the number of XML configurations that write a large Spring-based application.

Auto-assembly mode

The following auto-assembly modes can be used to indicate that the Spring container is dependent on the use of auto-assembly. You can <bean> definition using the autowire property of the element.

Mode Describe
no This is the default setting, which means that there is no automatic assembly, and you should use an explicit bean reference to connect. Y ou don't have to do anything special to connect. You've seen this in the Dependency Injection section.
byName Auto-assembled by property name. T he Spring container sees that the auto-assembled properties of the bean in the XML profile are set to byName. Then try to match and connect its properties to the properties of beans defined as the same name in the profile.
byType Auto-assembled by property data type. T he Spring container sees that the auto-assembled properties of the bean in the XML profile are set to byType. T hen if its type matches an exact bean name in the profile, it tries to match and connect the type of the property. If there is more than one such bean, a fatal exception will be thrown.
constructor Similar to byType, but this type applies to constructor argument types. If there is no constructor argument type bean in the container, a fatal error will occur.
autodetect (version 3.0 does not support) Spring first tries to connect by using auto-assembly, and if it doesn't, Spring tries to assemble automatically by byType.

You can use byType or constructor auto-assembly mode to connect arrays and other types of collections.

Limitations of automatic assembly

Automatic assembly works best when it is always used in the same project. I f automatic assembly is not typically used, it can confuse developers by using it to connect only one or two bean definitions. H owever, auto-assembly can significantly reduce the need to specify properties or constructor parameters, but you should consider the limitations and disadvantages of auto-assembly before using them.

Limit Describe
The possibility of rewriting You can specify dependencies by always rewriting the auto-assembled settings of slt;constructor-arg> and slt;property?gt;
The original data type You can't automatically assemble so-called simple types including basic types, strings, and classes.
the nature of chaos Automatic assembly is not as precise as explicit assembly, so use explicit assembly where possible.