Get the App
SLTechnology News&Howtos  ›  Development  › 

How to initialize context to build classes in springboot

Shulou Source: shulou.com Published: 2022-06-03 08:57:07 11月15日 Update

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

Tags: Annotations source code names functions methods support interfaces patterns proxies context properties factories objects types resources processes times conditions loops Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Huawei vpn Shulou Tech Info