This is the 11th day of my participation in Gwen Challenge

The autowire mechanism in Spring means that the Spring Ioc container is responsible for automatically finding and assigning the required beans to the properties of the bean being created, without manually setting the properties of the bean.

1. Assembly method of CONFIGURATION bean based on XML:

  • No: The default is not to autowire, and the bean needs to be assembled manually by setting the ref attribute.
  • ByName: Automatic assembly by bean name, if the property of one bean is the same as the name of another bean.
  • ByType: automatic assembly based on the data type of the parameter.
  • Constructor: assembly via constructors, and constructor parameters are assembled via byType.
  • Autodetect: Automatic detection, construct if there is a constructor, otherwise byType is used. (Deprecated)

Way in the definition of AutowireCapableBeanFactory. AUTOWIRE_NO AUTOWIRE_BY_NAME AUTOWIRE_BY_TYPE AUTOWIRE_CONSTRUCTOR AUTOWIRE_AUTODETECT

2. Complete the assembly of beans based on annotations

  • @Autowired, @Resource and @Inject can all realize bean injection. @Autowired is launched by Spring and has the most powerful function. Constructors, setter methods, parameters, member variables, annotations (for custom extended annotations) @Resource is a specification derivation of JSR-250 @Inject is a specification derivation of JSR-330
  • @value can inject configuration information

@autowired, @ Inject, @ the analytical work is of Value in AutowiredAnnotationBeanPostProcessor, how source 1

@ the parsing of a Resource work is within the CommonAnnotationBeanPostProcessor, how to source 2

1, public source AutowiredAnnotationBeanPostProcessor () {this. AutowiredAnnotationTypes. Add (Autowired. Class); this.autowiredAnnotationTypes.add(Value.class); try { this.autowiredAnnotationTypes.add((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); logger.trace("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring"); } catch (ClassNotFoundException ex) {// JSR-330 API not available - simply skip.}} resourceAnnotationTypes.add(Resource.class); . }Copy the code

Xml-based code examples

1. No mode

The Spring configuration file, which uses the ref parameter to inject beans, must have setter methods for the object, in this case the setFr method for Person.

No null pointer error reported because no FR property was injected.

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="no"> <property name="fr" ref="fr"></property> </bean>  <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>Copy the code

Rod bean

package constxiong.interview.assemble; /** ** @author * @date 2019-07-17 09:53:15 */ public class FishingRod {/** ** is used */ public void used() { System. Out.println (" fishing..." ); }}Copy the code

People bean

package constxiong.interview.assemble; /** ** * @author * @date 2019-07-17 09:54:56 */ public class Person {private FishingRod fr; Public void fish() {fr.used(); } public void setFr(FishingRod fr) { this.fr = fr; }}Copy the code

The test code

package constxiong.interview.assemble; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AssembleTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring_assemble.xml"); Person person = (Person)context.getBean("person"); person.fish(); }}Copy the code

ByName also requires a corresponding setter method to be injected

Autowire =”byName”

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="byName"></bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>Copy the code

3, byType also requires a corresponding setter method to inject

Autowire =”byType”

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="byType"></bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>Copy the code

The other is constant

4. Constructor does not need setter methods; instead, it needs to inject beans through constructors

Autowire =”byType”

The Person class removes the setFr method and adds a constructor to set the FR property

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="person" class="constxiong.interview.assemble.Person" autowire="constructor"></bean> <bean id="fr" class="constxiong.interview.assemble.FishingRod"></bean> </beans>Copy the code
package constxiong.interview.assemble; /** ** * @author * @date 2019-07-17 09:54:56 */ public class Person {private FishingRod fr; public Person(FishingRod fr) { this.fr = fr; } public void fish() {fr.used(); }}Copy the code

1, 2, 3, 4 of the test results are consistent, print

Fishing...Copy the code