The four core

1. Automatic assembly: Can run projects with simple configuration or even zero configuration. 2

Beginners experience

I remember when I first learned about autowiring, there were two interesting annotations: @autoWired and @Resource. Remember that @autoWired default assembly is byType, you can use @qualifile for byName, @resource default assembly is byName, you can’t find automatic byType. Then remember to add the @Component annotation or its derivatives to the assembly. Today, we are going to focus on how our Bean is loaded into the container.

Get to the point

To explore the source code for autoloading, start with the @SpringBootApplication annotation.



@SpringBootApplication is A composite annotation. The three annotations are meta-annotations provided by Java. @Inherited refers to Inherited. B then inherits from A, so B also has the annotation function). The important notes are the following three@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan.

@SpringBootConfiguration



The @springBootConfiguration annotation, which looks like a fancy annotation, inherits @Configuration. In other words, it is a Configuration annotation, which indicates that a class is a Configuration class.

@ComponentScan

By default, this annotation scans for all classes in the package that are annotated by the @Component annotation or its derivatives.

@EnableAutoConfiguration

This note is really cool, it’s the master switch of autowerassembly, which means to turn on autowerassembly.



So if I click in there, I have an annotation that I haven’t seen before, @autoConfigurationPackage, so what does that do? Autoconfiguration package, something like that, means that the package in which the annotation is added is managed as an autoconfiguration package. Point in!



Click on it, and there’s one in there@Import({Registrar.class})



Click on it, and you finally see the code. You can roughly see that this is used to register the bean, so let’s focus on thatregisterBeanDefinitionsMethod, which has one parameter in itBeanDefinitionRegistry registryThat’s what it sounds like,The key to theThe packageName is set through a constructornew PackageImports(metadata).getPackageNames()What I’m going to do, I think you already know, is I’m going to click on this constructor.



Register (); register (); register ()register(registry, new PackageImports(metadata).getPackageNames().toArray(new String[0]));The construction method of the point in (noble people forget things).



The else method completes the injection of the bean from the package into the container.



Interpretation of the finished@AutoConfigurationPackageNotes. Now look at this@Import(AutoConfigurationImportSelector.class), this is the ImportSelector class that imports auto-assembly.

AutoConfigurationImportSelector



You can see that he implements the DeferredImportSelector interface



And if I go ahead, you can see that he inherits the ImportSelector interface.



There’s one method in ImportSelector, which is selectImports



Can be seen clearly, AutoConfigurationImportSelector implements the method



Here the first is to judge the automatic assembly of the switch



Then get the beans you want to assemble



And this one right heregetCandidateConfigurationsThe method is to read meta-INF /spring.factories



Under AutoConfigurationImportSelector, there is an important part of the static inner class and initialized in the constructor of the static inner class reads the meta-inf/spring – autoconfigure – metadata. Properties.



Demo ends

These are the two files that we read above



Go to spring-autoconfigure-metadata.properties, where you can see some metadata for auto-assembly.



Points in the spring. Factories



Click on one and you’ll find that it’s all written beans, just waiting to be loaded!



We can also define our own starter and wrap our bean in a configuration class, as long as we write spring.Factories according to the convention! Today’s share is here, what write bad place welcome private message, let us discuss together, study together!

Wechat search a search [Hehe learning programming] pay attention to this different programmers, want to learn what welcome private message, let us discuss together, learn together ~