Spring (05) - automatic injection based on XML configuration (autowire)

5 Automatic injection based on XML configuration (autoWire)

5.1 autowire

Usually, if we need to inject one beaninto another bean, we do it through setmethods or constructors, and we Springalso support beanautomatic injection. When defining bean, we can specify whether it needs to be automatically injected into the current beanelement through the autowireattributes of the element . There are four optional values ​​for the property.beanbeanautowire

  • no: Default value. Indicates that automatic injection is not performed.
  • byName: Automatic injection based on name. IfbeanA there is a setBeanB()method, when it is specified autowire=”byname”, it Springwill automatically find the named through method in the beancontainer and “beanB”inject it automatically .beansetBeanBbeanA
  • byType: Automatic injection based on type. IfbeanA there is a setBeanB(BeanB b)method, when specified autowire=”byType”, it Springwill be automatically injected into the container beanthrough the method of type . But if there are more than two types in the container at this time, an exception will be thrown.BeanBbeansetBeanB()beanABeanBbean
  • constructor: Equivalent to byType, except that when specified autowire=”constructor”, it means that it will be automatically injected according to the type through the constructor.

According to the above introduction, we know that autowirethere are four optional values ​​of the attribute, the default is “no”. where “byname”sum “byType”is for setinjection by method, “constructor”but for injection by constructor. The following example shows that Springat initialization Hellotime, it will automatically look for the type in the container beanto inject through the constructor.Worldbean

public  class  Hello {

	public Hello(World world) {
		
	}
	
}
	<!-- Automatic injection by type by constructor --> 
	< bean  id = " hello "  class = " com.app.Hello "  autowire = " constructor " />

If one beanAneeds to setinject one through a method beanB, we beanAhave autowire=”byType”specified above that it will be automatically injected through the method beanAaccording to the associated beantype set, but at the same time we have beanAdefined 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, Springthe property will not be automatically injected. For this mechanism, if we have a beanAspecified type that needs to be automatically injected, beanAwe need to inject a type BeanBof bean, but at this time beanthere are two types of , in the container, BeanBan exception will be thrown beanat this time . SpringWith the mechanism that the display definition will no longer automatically inject the property, we can beanAspecify which BeanBtype will be injected on the display bean.

5.2 default-autowire

If the behavior in our application is autowiremostly the same, we can default-autowirespecify the default behavior through properties autowire. default-autowireThe optional autowirevalue of is the same as the optional value of , and the default is also “no”, that is, no automatic injection is performed. default-autowireAttributes are defined on beanselements. The following indicates that our global autowirebehavior 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-candidateIt is autowireused in conjunction and is an attribute defined on beanthe above, indicating whether it is a beancandidate for automatic injection. The default is true, that is, all defaults beancan be candidates for the automatic injection mechanism. Sometimes we may not want one beanto be injected into the other through the automatic injection mechanism bean. At this time, we can specify the beanattribute autowire-candidateas false. For example, there is a beanAdefinition that needs to automatically inject a type according to BeanBthe type bean, but there are two types in BeanBthe container bean, respectively beanB1and beanB2, in this case, it Springcannot be automatically injected. In fact, what we want to inject beanAis that , at this time, in addition to injecting beanB1as 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.beanB1beanAbeanB2autowire-candidate=”false”beanB2
autowire-candidateautowire-candidate

5.4 default-autowire-candidates

default-autowire-candidatesThe role is default-autowiresimilar to the role of , and it is also used to define global autowire-candidatebehavior. There are also two optional values , tureand falsethe 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 beanthe configuration of our definitions. On the other hand, it is convenient for us to replace the implementation class without changing the original beandefinition. However, after using automatic injection, our beandefinition will look like a single beanelement 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)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326356829&siteId=291194637