5 Automatic injection based on XML configuration (autoWire)
5.1 autowire
Usually, if we need to inject one bean
into another bean
, we do it through set
methods or constructors, and we Spring
also support bean
automatic injection. When defining bean
, we can specify whether it needs to be automatically injected into the current bean
element through the autowire
attributes of the element . There are four optional values for the property.bean
bean
autowire
- no: Default value. Indicates that automatic injection is not performed.
- byName: Automatic injection based on name. If
beanA
there is asetBeanB()
method, when it is specifiedautowire=”byname”
, itSpring
will automatically find the named through method in thebean
container and“beanB”
inject it automatically .bean
setBeanB
beanA
- byType: Automatic injection based on type. If
beanA
there is asetBeanB(BeanB b)
method, when specifiedautowire=”byType”
, itSpring
will be automatically injected into the containerbean
through the method of type . But if there are more than two types in the container at this time, an exception will be thrown.BeanB
bean
setBeanB()
beanA
BeanB
bean
- constructor: Equivalent to
byType
, except that when specifiedautowire=”constructor”
, it means that it will be automatically injected according to the type through the constructor.
According to the above introduction, we know that autowire
there are four optional values of the attribute, the default is “no”
. where “byname”
sum “byType”
is for set
injection by method, “constructor”
but for injection by constructor. The following example shows that Spring
at initialization Hello
time, it will automatically look for the type in the container bean
to inject through the constructor.World
bean
public class Hello {
public Hello(World world) {
}
}
<!-- Automatic injection by type by constructor -->
< bean id = " hello " class = " com.app.Hello " autowire = " constructor " />
If one beanA
needs to set
inject one through a method beanB
, we beanA
have autowire=”byType”
specified above that it will be automatically injected through the method beanA
according to the associated bean
type set
, but at the same time we have beanA
defined the association shown above beanB
, then the definition shown at this time will have a higher priority. That is, for the definition that needs to be automatically injected beanA
, if one of the properties is injected through the displayed definition, Spring
the property will not be automatically injected. For this mechanism, if we have a beanA
specified type that needs to be automatically injected, beanA
we need to inject a type BeanB
of bean
, but at this time bean
there are two types of , in the container, BeanB
an exception will be thrown bean
at this time . Spring
With the mechanism that the display definition will no longer automatically inject the property, we can beanA
specify which BeanB
type will be injected on the display bean
.
5.2 default-autowire
If the behavior in our application is autowire
mostly the same, we can default-autowire
specify the default behavior through properties autowire
. default-autowire
The optional autowire
value of is the same as the optional value of , and the default is also “no”
, that is, no automatic injection is performed. default-autowire
Attributes are defined on beans
elements. The following indicates that our global autowire
behavior is automatic injection by name.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
</beans>
5.3 autowire-candidate
autowire-candidate
It is autowire
used in conjunction and is an attribute defined on bean
the above, indicating whether it is a bean
candidate for automatic injection. The default is true
, that is, all defaults bean
can be candidates for the automatic injection mechanism. Sometimes we may not want one bean
to be injected into the other through the automatic injection mechanism bean
. At this time, we can specify the bean
attribute autowire-candidate
as false
. For example, there is a beanA
definition that needs to automatically inject a type according to BeanB
the type bean
, but there are two types in BeanB
the container bean
, respectively beanB1
and beanB2
, in this case, it Spring
cannot be automatically injected. In fact, what we want to inject beanA
is that , at this time, in addition to injecting beanB1
as shown before , we can also exclude the candidates from the automatic injection by defining the above. Only works for automatic injection, for manual injection, no matter what the value is, manual injection will work.beanB1
beanA
beanB2
autowire-candidate=”false”
beanB2
autowire-candidate
autowire-candidate
5.4 default-autowire-candidates
default-autowire-candidates
The role is default-autowire
similar to the role of , and it is also used to define global autowire-candidate
behavior. There are also two optional values , ture
and false
the default is true
.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName"
default-autowire-candidates="true">
</beans>
The advantage of using automatic injection is that we can reduce bean
the configuration of our definitions. On the other hand, it is convenient for us to replace the implementation class without changing the original bean
definition. However, after using automatic injection, our bean
definition will look like a single bean
element definition, lacking the displayed injection configuration, which will have a certain impact on future maintenance and configuration readability. Therefore, it must be carefully considered when using it.
(Note: This article is written based on Spring 4.1.0)