Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the startup process of the Spring container

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "what is the startup process of Spring container". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Source code parsing

Considering that looking at the source code directly is a very boring process, and the code design of Spring is very excellent and standard, which will lead to frequent jumps between classes when opening the source code, and students who are not familiar with it may get dizzy directly, so I will prepare a flow chart before every important process. I suggest that you first understand the overall steps through the flow chart, and then shake the code hard. This can reduce a lot of difficulty.

I believe every student who has used Spring technology knows that there is a very important step in the initialization process of Spring, that is, the refresh of the Spring container. This step is important, but the initialization process before refresh is also very important.

This article divides the whole startup process into two parts, namely, the initialization and refresh of the container.

Initialization process

Process analysis

Since the source code is analyzed based on java-config technology, the entry here is AnnotationConfigApplicationContext. If you use xml analysis, then the entry is ClassPathXmlApplicationContext. The common feature of both of them is that they inherit the AbstractApplicationContext class, and the famous refresh method is defined in this class. Now it is no longer spoiled. Let's go on to analyze the AnnotationConfigApplicationContext class and draw the following flow chart:

After reading the flowchart, we should think about it: if you were asked to design an IOC container, what would you do? First of all, I will certainly provide an AnnotationConfigApplicationContext for users to use, and then I need to initialize a series of tool components:

①: if I want to generate bean objects, I need a beanFactory factory (DefaultListableBeanFactory)

②: if I want to read and convert classes with specific annotations (such as @ Service, @ Repository) into BeanDefinition objects (BeanDefinition is an extremely important concept in Spring, it stores all the characteristic information of bean objects, such as singleton, lazy loading, factoryBeanName, etc.), then I need an annotation configuration reader (AnnotatedBeanDefinitionReader)

③: if I want to scan the user-specified package directory for bean objects, I also need a path scanner (ClassPathBeanDefinitionScanner).

Through the above thinking, is it easy to understand the picture above?

Ps: the yellow remarks in the figure can be ignored, but it clearly shows when and where some of the built-in components of Spring were added to the container. The role of the components will be analyzed in detail in a later series of articles.

Core code analysis

Considering that if all the code is parsed, the article will be too long, so here we only analyze the core content at the source code level. All the steps marked with the words ①, ②, ③ and so on in the figure can be understood as a more important step, so let's start with a detailed analysis.

Org.springframework.context.annotation.AnnotationConfigUtils#registerAnnotationConfigProcessors

According to the above figure, when the code runs here, the Spring container has been constructed, so you can add some built-in components to the container. The most important components are ConfigurationClassPostProcessor and AutowiredAnnotationBeanPostProcessor, the former is a beanFactory post processor used to complete bean scanning and injection, and the latter is a bean post processor used to complete @ AutoWired automatic injection.

Public static Set registerAnnotationConfigProcessors (BeanDefinitionRegistry registry, @ Nullable Object source) {DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory (registry); if (beanFactory! = null) {if (! (beanFactory.getDependencyComparator () instanceof AnnotationAwareOrderComparator)) {beanFactory.setDependencyComparator (AnnotationAwareOrderComparator.INSTANCE);} if (! (beanFactory.getAutowireCandidateResolver () instanceof ContextAnnotationAutowireCandidateResolver)) {beanFactory.setAutowireCandidateResolver (new ContextAnnotationAutowireCandidateResolver ());}} Set beanDefs = new LinkedHashSet (8) / / register [BeanFactoryPostProcessor] with beanDefinitionMap: [ConfigurationClassPostProcessor] if (! registry.containsBeanDefinition (CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {RootBeanDefinition def = new RootBeanDefinition (ConfigurationClassPostProcessor.class); def.setSource (source); beanDefs.add (registerPostProcessor (registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));} / / register [BeanPostProcessor]: [AutowiredAnnotationBeanPostProcessor] if (! registry.containsBeanDefinition (AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME) {RootBeanDefinition def = new RootBeanDefinition (AutowiredAnnotationBeanPostProcessor.class)) with beanDefinitionMap Def.setSource (source); beanDefs.add (registerPostProcessor (registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));} / / Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor. Register [BeanPostProcessor] with beanDefinitionMap: [CommonAnnotationBeanPostProcessor] if (jsr250Present & &! registry.containsBeanDefinition (COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {RootBeanDefinition def = new RootBeanDefinition (CommonAnnotationBeanPostProcessor.class); def.setSource (source); beanDefs.add (registerPostProcessor (registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));} / / Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor. / register [BeanPostProcessor]: [PersistenceAnnotationBeanPostProcessor] with beanDefinitionMap, provided that if (jpaPresent & &! registry.containsBeanDefinition (PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {RootBeanDefinition def = new RootBeanDefinition (); try {def.setBeanClass (ClassUtils.forName (PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, AnnotationConfigUtils.class.getClassLoader () in jpa environment } catch (ClassNotFoundException ex) {throw new IllegalStateException ("Cannot load optional framework class:" + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);} def.setSource (source); beanDefs.add (registerPostProcessor (registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));} / / register [BeanFactoryPostProcessor]: [EventListenerMethodProcessor] if (! registry.containsBeanDefinition (EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {RootBeanDefinition def = new RootBeanDefinition (EventListenerMethodProcessor.class) with beanDefinitionMap Def.setSource (source); beanDefs.add (registerPostProcessor (registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));} / / register components with beanDefinitionMap: [DefaultEventListenerFactory] if (! registry.containsBeanDefinition (EVENT_LISTENER_FACTORY_BEAN_NAME)) {RootBeanDefinition def = new RootBeanDefinition (DefaultEventListenerFactory.class); def.setSource (source); beanDefs.add (registerPostProcessor (registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME);} return beanDefs;}

Org.springframework.context.annotation.AnnotatedBeanDefinitionReader#doRegisterBean

This step is mainly used to parse the Spring configuration class passed in by the user. In fact, it is also parsed into a BeanDefinition and registered in the container. There is nothing to say.

Void doRegisterBean (Class annotatedClass, @ Nullable Supplier instanceSupplier, @ Nullable String name, @ Nullable Class)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report