Home >Java >javaTutorial >spring-: why-spring-cannot-match-by-name-for-injection
based on type , not names. When encountering multiple types of the same type (such as TenantDataSource), it will not automatically return to match according to the parameter name. This is because Spring cannot always guarantee the parameter name in the method to match the Bean name.
Java By default,will not retain the parameter name in the compiled bytecode. If there is no special configuration, the parameter names (for example, TenantadataSource and TenantbDataSource) will be cleared during the compilation and replaced them with universal names, such as ARG0 and ARG1. Therefore, Spring cannot know that TenanTadataSource refers to the bean called "Tenanta-Datasource".
Solve the parameter name retention problem
-parameters
<code>javac -parameters MyClass.java</code>
Spring avoids assumptions to prevent accidents. For example, if the parameter name is accidentally exchanged or named, or the developers expect different mappings, what will happen? If there is no clear guidance (such as
), Spring cannot determine the intention of the developer, and choose to throw an error instead of injecting the wrong bean.
@Qualifier
Spring follows the principle of "explicitly better than hidden". Dependent injection should be predictable, and does not depend on the assumptions of parameter names to the matching of Bean name matching. Spring is consistent in different environments and frameworks. Some languages or frameworks (such as KOTLIN) retain the parameter name by default, while other languages are not retained. Therefore, Spring avoids dependent parameter names to achieve key functions.
When can the name match can work automatically?
Spring 4.3 Parameter name Discover
Example:
-parameters
<code>@Autowired public TenantService(TenantDataSource tenantADataSource, TenantDataSource tenantBDataSource) { this.tenantADataSource = tenantADataSource; this.tenantBDataSource = tenantBDataSource; }</code>Why is Spring more inclined to use @qualifier?
-parameters
Use to ensure definition and eliminate ambiguity, so that both developers and frameworks can clearly intention. @Qualifier
Some people may think that a framework like Spring should be more intelligent, and the Bean name is automatically matched with the parameter name when there is ambiguity. But:
@Qualifier
Developers use meaningful bean names and parameter names, but they also prefer to prefer
@Qualifier
For multi -example scenes, Spring first injects by type
@Qualifier
: By default, the parameter name will not be kept in the Java bytecode, so Spring cannot use them to match the parameter with the parameters. Even if the parameter name (-parameters
or Spring's design concept
: Misty errors can prevent accidents and ensure that developers fully control the dependence injection.@Qualifier
@Primary
The above is the detailed content of spring-: why-spring-cannot-match-by-name-for-injection. For more information, please follow other related articles on the PHP Chinese website!