In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the principle of springboot-autoConfiguration". In daily operation, I believe many people have doubts about the principle of springboot-autoConfiguration. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the principle of springboot-autoConfiguration?" Next, please follow the editor to study!
Springboot-AutoConfiguration principle
Springboot automatic configuration principle, based on the springboot 2.1.5.RELEASE version here is a sample project.
Without gossip, let's take a look at the main category first.
SpringBootApplicationpublic class BootStartMain {public static void main (String [] args) {SpringApplication app = new SpringApplication (BootStartMain.class); app.setBanner (new ResourceBanner (new ClassPathResource ("banner.txt"); app.run (args);}}
The last springboot startup process analyzed the logic of the run method and described the startup process of springboot. Finally, in the case of load resources, the main class was registered with bean in the form of bean, and in the previous section we also mentioned the ConfigurationClassPostProcessor guy used in this section. If you are not familiar with it, go back and take a look.
Now that you have registered as bean, bean is about to be initialized, and things happen when it is initialized, and all events are triggered by the @ SpringBootApplication comment.
@ SpringBootApplication@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan (excludeFilters = {@ Filter (type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @ Filter (type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class)}) public @ interface SpringBootApplication {...}
This guy is actually a composite annotation, and you see what we want, @ EnableAutoConfiguration guy shows up.
@ EnableAutoConfiguration@Inherited@AutoConfigurationPackage@Import (AutoConfigurationImportSelector.class) public @ interface EnableAutoConfiguration {...}
This guy is also a compound comment.
@ AutoConfigurationPackage@Inherited@Import (AutoConfigurationPackages.Registrar.class) public @ interface AutoConfigurationPackage {}
Let's take a look at what is registered with @ Import's AutoConfigurationPackages.Registrar.
Public void registerBeanDefinitions (AnnotationMetadata metadata, BeanDefinitionRegistry registry) {register (registry, new PackageImport (metadata). GetPackageName ();}
Metadata actually contains our main class information.
Let's look at the constructor of PackageImport and initialize packageName, which is the package name where our main class is located.
PackageImport (AnnotationMetadata metadata) {this.packageName = ClassUtils.getPackageName (metadata.getClassName ());
Let's move on to the register method.
Public static void register (BeanDefinitionRegistry registry, String... PackageNames) {/ / check whether org.springframework.boot.autoconfigure.AutoConfigurationPackages if (registry.containsBeanDefinition (BEAN)) {/ / if so, get BeanDefinition beanDefinition = registry.getBeanDefinition (BEAN) directly; ConstructorArgumentValues constructorArguments = beanDefinition .getConstructor ArgumentValues () / / add a new packageName to constructorArguments.addIndexedArgumentValue (0, addBasePackages (constructorArguments, packageNames));} else {/ / if not registered, re-register a GenericBeanDefinition beanDefinition = new GenericBeanDefinition () / / set bean type beanDefinition.setBeanClass (BasePackages.class); / / add packageNames beanDefinition.getConstructorArgumentValues () .addIndexedArgumentValue (0, packageNames); beanDefinition.setRole (BeanDefinition.ROLE_INFRASTRUCTURE); / / register bean registry.registerBeanDefinition (BEAN, beanDefinition) }}
The logic is clear: if you don't register the specified bean, register a new one and add the packageName; if you already have the specified bean, add the packageName directly. This is why our main class can scan our bean without adding @ CompomnentScan, only the main class package and its child package.
The scanning part doesn't explain too much.
ConfigurationClassPostProcessor
Before I introduce AutoConfigurationImportSelector, let me give you a brief introduction to ConfigurationClassPostProcessor.
You can see that this guy has implemented BeanDefinitionRegistryPostProcessor, so I won't explain where this thing is called. Let's go straight to the core approach.
/ / BeanDefinitionRegistryPostProcessor.javapublic void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry) {/ /. ProcessConfigBeanDefinitions (registry);} public void processConfigBeanDefinitions (BeanDefinitionRegistry registry) {/ / because there is too much code, we will not post it, but only introduce the part we need: ConfigurationClassParser parser = new ConfigurationClassParser (...); parser.parse (candidates); / /.}
Continue to look at the parser.parse () method.
Public void parse (Set configCandidates) {for (BeanDefinitionHolder holder: configCandidates) {BeanDefinition bd = holder.getBeanDefinition (); try {if (bd instanceof AnnotatedBeanDefinition) {/ / this place will initialize the deferredImportSelectorHandler, that is, put all the selector into this guy / / the logic is very deep, the following will say where parse (AnnotatedBeanDefinition) bd). GetMetadata (), holder.getBeanName ()) } / /.} this.deferredImportSelectorHandler.process ();}
The logic of the parse method above is very deep, and the logic of our selector adding deferredImportSelectorHandler to this guy is in this place, parse- > processConfigurationClass- > doProcessConfigurationClass- > processImports, and then the this.deferredImportSelectorHandler.handle () method in this method.
The treatment of ImportSelectorHandler
After our selector is added, we're going to deal with selector. Look at the process method of this.deferredImportSelectorHandler.process ();.
/ / ConfigurationClassParser.DeferredImportSelectorHandler.javapublic void process () {/ / assign all selectors List deferredImports = this.deferredImportSelectors; this.deferredImportSelectors = null; try {if (deferredImports! = null) {DeferredImportSelectorGroupingHandler handler = new DeferredImportSelectorGroupingHandler (); deferredImports.sort (DEFERRED_IMPORT_COMPARATOR); / / a pair of deferredImports objects to call handler's register method, that is, to register deferredImports.forEach (handler::register) in handler / / after the ancestral book is in handler, execute the processGroupImports method handler.processGroupImports () of handler;}} finally {/ / initialize deferredImportSelectors this.deferredImportSelectors = new ArrayList ();}}
Register
When calling the register of handler, it will involve a group thing, the main function is to store different selector groups. Let's take a look at this method.
/ / ConfigurationClassParser.DeferredImportSelectorGroupingHandler.javapublic void register (DeferredImportSelectorHolder deferredImport) {/ / the getImportGroup in this place is to call our importSelector to get a group Class to which it belongs
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.