Automatic assembly of Beans
Automated Assembly (autowiring) helps reduce or even eliminate configuration <property> elements and <constructor-arg> elements, allowing spring to automatically identify how to assemble bean dependencies.
1.1 Automatic Assembly Bean Properties:
Spring offers four distinct automated assembly strategies:
1. byname-automatically assembles other beans with the same name as the Bean's attributes into the corresponding attributes of the bean. If there is no bean that matches the name of the property, the property is not assembled.
2. bytype-automatically assembles other beans of the same type as the bean's attributes into the corresponding attributes of the bean. If there is no bean that matches the type of the property, the property is not assembled.
3, constructor-The constructor of the bean with the same type of other beans are automatically assembled into the corresponding entry parameters of the Bean constructor.
4, autodetect-first try to use constructor for automatic assembly. If it fails, try using Bytype for automatic assembly.
1.1.1 ByName Automatic assembly:
This is stated before automatic assembly using byname:
<BeanID= "Kenny2"class= "Com.springinaction.springidol.Instrumentalist"> < Propertyname= "Song"value= "Jingle Bells"></ Property> < Propertyname= "instrument"ref="Instrument"></ Property> </Bean>
After automatic assembly using byname, this is done:
<BeanID= "instrument"class= "Com.springinaction.springidol.Saxophone"/> <BeanID= "Kenny2"class= "Com.springinaction.springidol.Instrumentalist"Autowire= "ByName"> < Propertyname= "Song"value= "Jingle Bells"></ Property> </Bean>
ByName Automatic Assembly follows a convention that automatically assembles a bean with the same ID as the property's name. By setting the Autowire property to Byname,spring, the Bean's properties will be searched for the same spring Bean as its name. The disadvantage of using byname is that the name of the bean is the same as the name of the other Bean's properties.
1.1.2 Bytype Automatic assembly:
Bytype Automatic assembly works similar to byname automatic assembly, except that it is no longer the name of the matching attribute but the type of the property. When we try to use Bytype to assemble automatically, spring looks for which Bean's type matches the type of the property.
But Bytype automatic assembly has one limitation: What if Spring finds multiple beans whose type matches the type of attributes that need to be automatically assembled? In this scenario, spring does not guess which Bean is more suitable for automatic assembly, but instead chooses to throw an exception. Therefore, the app allows only one bean to match the type of attribute that needs to be automatically assembled.
To avoid ambiguity caused by the use of Bytype automatic assembly, Spring provides us with two additional options: You can identify a preferred bean for automatic assembly, or you can cancel the candidacy of a bean for automatic assembly.
To identify a preferred bean for automatic assembly, you can use the primary property of the <bean> element. If only the primary property of a candidate bean that is automatically assembled is set to true, the bean will be preferred over other candidate beans.
But the primary property has a very strange point: it is set to True by default. Therefore, in order to use the primary property, we have to set all non-preferred primary properties to false. For example:
<id= "Instrument" class= "Com.springinaction.springidol.Saxophone " primary=" false "/>
The primary property is only meaningful for identifying preferred beans. If we want to exclude certain beans from automatic assembly, we can set the Autowire-candidate property of these beans to false, as follows:
<id= "Instrument" class= "Com.springinaction.springidol.Saxophone " autowire-candidate=" false "/>
1.1.3 Constructor Automatic assembly:
<id= "Duke"class= " Com.springinaction.springidol.PoeticJuggler "autowire=" constructor "/>
Constructor automatic assembly has the same limitations as Bytype automatic assembly. When multiple beans are found to match the arguments of a constructor, spring does not attempt to guess which Bean is more suitable for automatic assembly. In addition, if a class has multiple constructors that meet the conditions of automatic assembly, spring does not attempt to guess which constructor is more appropriate for use.
1.1.4 Best Automatic assembly
If you want to automatically assemble beans, but you cannot decide which type of automatic assembly to use, you can set the Autowire property to AutoDetect, which is determined by spring.
<id= "Duke"class= "Com.springinaction.springidol.PoeticJuggler" autowire= "AutoDetect"/>
When configuring a bean's Autowire property to AutoDetect, spring will first use constructor to assemble automatically, and if no bean matching the constructor is found, spring will attempt to use Bytype automatic assembly.
1.2 Default automatic assembly
If you need to configure the same Autowire property for each bean in the Spring application context (or most of them), you can require spring to apply the same automatic assembly policy to all the beans it creates to simplify configuration, all that is needed is the root element Add a Default-autowire property on <beans>:
<? xml version= "1.0" encoding= "UTF-8" ?> < beans xmlns = "Http://www.springframework.org/schema/beans" xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/ Schema/beans/spring-beans-3.0.xsd " default-autowire = "Bytype" > </ beans >
By default, the Default-autowire property is set to None, indicating that all beans do not use automatic assembly unless the bean itself is configured with the Autowire property. The Default-autowire in a configuration file is applied only to all beans in the specified spring configuration file and does not apply to all beans in the Spring app context. You can define multiple profiles in a spring application context, each of which can have its own default automatic assembly policy. Similarly, it is not possible to configure a default automatic assembly policy, which means that all beans can only use this default automatic assembly policy, and can also use the Autowire property of the <bean> element to override the default automatic assembly policy configured by the <bean> element.
1.3 Mixing with automatic assembly and display assembly
If automatic assembly is selected for a bean, it does not mean that some properties of the bean cannot be displayed for assembly. We can still configure the <property> element for any of the properties, just like we didn't configure Autowire before.
<BeanID= "Kenny"class= "Com.springinaction.springidol.Instrumentalist"Autowire= "Bytype">< Propertyname= "Song"value= "Jingle Bells" />< Propertyname= "instrument"ref= "saxophone" /></Bean>
The combination of automated assembly and manual assembly has been a very successful solution to the problem of assembly uncertainties that may arise from the use of Bytype automated assembly strategies. If there are multiple beans in the spring context that implement the instrument interface, in order to circumvent spring cannot explicitly pick from the beans that implement the instrument interface, the exception is thrown, You can display the assembly instrument properties to override the automatic assembly.
The last consideration when using the Hybrid assembly: When using the constructor auto-assemble strategy, you must let spring automatically assemble all the arguments for the constructor, instead of mixing the constructor auto-assemble policy and the <constructor-arg> element.
Spring actual Bean's automatic assembly (non-annotated mode)