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

How to initialize context to build classes in springboot

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

Share

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

In this issue, the editor will bring you about how to initialize the context construction class in springboot. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Preface

Based on the source code of the springboot2.3.x series (most of which take the source code in the jar package as an example), the source code of github may be slightly different from that of the actual release, but the overall process is not much different.

CreateApplicationContext creates contextual applications

Source code:

Protected ConfigurableApplicationContext createApplicationContext () {Class contextClass = this.applicationContextClass; if (contextClass = = null) {try {switch (this.webApplicationType) {case SERVLET: contextClass = Class.forName ("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext"); break Case REACTIVE: contextClass = Class.forName ("org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext"); break; default: contextClass = Class.forName ("org.springframework.context.annotation.AnnotationConfigApplicationContext");}} catch (ClassNotFoundException var3) {throw new IllegalStateException ("Unable create a default ApplicationContext, please specify an ApplicationContextClass", var3) } return (ConfigurableApplicationContext) BeanUtils.instantiateClass (contextClass);}

The default value of webApplicationType is SERVLET, which corresponds to the class object of AnnotationConfigServletWebServerApplicationContext. Instantiating an object through BeanUtils.instantiateClass actually has the same effect created directly through new, and is created directly using new in future versions of the source code. The role of this class is also known by name and is built based on annotated web contextual applications. Corresponding to it is xml mode XmlServletWebServerApplicationContext.

First take a look at the uml diagram of this key class:

The word on the picture here is relatively small. If you can't see it clearly, you can check it through your own idea. The relationship is complex, but you can see several common classes on the diagram. BeanFactory, ResourceLoader, lifecycle, etc. You can see that the top level is all interfaces, and the following implementation classes also intersect each other, and one of its common parents is the ApplicationContext interface.

AnnotationConfigServletWebServerApplicationContext initialization

We need to understand AnnotationConfigServletWebServerApplicationContext itself and its parent class at the beginning, because there are a lot of initialization actions already taking place in the constructor.

Start with yourself:

Public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry {private final AnnotatedBeanDefinitionReader reader; private final ClassPathBeanDefinitionScanner scanner; private final Set

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