Spring provides certain rules to autowire beans. <bean>
The autowire
attribute can specify a strategy for autowiring.
Strategy | illustrate |
---|---|
byName | Match by name. |
byType | Match by type. Assuming that Book has a property of type Author, if a bean of type Author is found in the container, it will be automatically assembled to the Author property of Book. |
constructor | It is also matched according to the type, but it refers to the case of the constructor. Suppose Book has a constructor, which has an input parameter of type Author; if a bean of type Author is found in the container, it will be automatically assembled; if not found, an exception will be thrown. |
autodetect | If the Bean provides a default constructor, the byType method is used; if not, the constructor method is used. |
<beans>
The element has the default-autowire attribute, which can set the global autowiring type; if it is no, it means that autowiring is not enabled; there are also these values: byName, byType, constructor and autodetect.
In practice, the autowiring function is rarely enabled in XML, and the byType autowiring strategy is adopted by default for the annotation-based configuration method.