In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Springboot2.0.6 how to create applications, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
ApplicationContext correlation analysis public ConfigurableApplicationContext run (String...) in the run method of parsing SpringApplication Args) {. / / omit ConfigurableApplicationContext context = null;. / / omit try {. / / omit / / create the corresponding Context container context = createApplicationContext () based on the application type . / omit / / prepare before refreshing Context / / associate important components such as SpringApplicationRunListeners, ConfigurableEnvironment, ApplicationArguments, Banner with context object prepareContext (context, environment, listeners, applicationArguments, printedBanner) . / / omit}. / / omit} catch (Throwable ex) {/ / start error report processing handleRunFailure (context, ex, exceptionReporters, listeners); throw new IllegalStateException (ex);}. / / omit} applicationContext
What is the application context and what does it include? It can be understood as an upgrade of the SpringIoC container, that is, it enriches some advanced features on the basis of the IoC container, and it has a holding relationship to the IoC container. Its beanFactory is the IoC container. It includes running system environment, Java environment, Spring running environment, project configuration, startup instructions and so on. There are three types of applications in SpringBoot, as shown in the following enumeration
Public enum WebApplicationType {/ * applications are not web applications, and web servers should not be used to launch * / NONE, / * applications should run as servlet-based web applications and embedded servlet web (tomcat) servers should be started. * / SERVLET, / * the application should run as a reactive web application and the embedded reactive web server should be started. * / REACTIVE}
Corresponding to different should types, SpringBoot gives three corresponding application contexts.
The class structure of the three corresponding application contexts is as follows
Structurally, they all inherit GenericApplicationContext, while beanFactory is defined in GenericApplicationContext
Public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {private final DefaultListableBeanFactory beanFactory;... Public GenericApplicationContext () {this.beanFactory = new DefaultListableBeanFactory ();}...}
From the above structure, we can see that when we initialize the application context, we also trigger the constructor of the GenericApplicationContext class, thus creating the IoC container. Take a closer look at his constructor and see if you find a familiar class DefaultListableBeanFactory, this DefaultListableBeanFactory is the true face of the IoC container. In the later refresh () method analysis, DefaultListableBeanFactory is ubiquitous.
Create applicationContext
We can call the createApplicationContext method to create a concrete applicationContext based on the application class
Public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot." + "web.servlet.context.AnnotationConfigServletWebServerApplicationContext"; public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework." + "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext"; protected ConfigurableApplicationContext createApplicationContext () {Class contextClass = this.applicationContextClass If (contextClass = = null) {try {switch (this.webApplicationType) {case SERVLET: contextClass = Class.forName (DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break; case REACTIVE: contextClass = Class.forName (DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break Default: contextClass = Class.forName (DEFAULT_CONTEXT_CLASS);}} catch (ClassNotFoundException ex) {throw new IllegalStateException ("Unable create a default ApplicationContext," + "please specify an ApplicationContextClass", ex);}} / / create Application return (ConfigurableApplicationContext) BeanUtils.instantiateClass (contextClass) based on Class;}
Take the web project as an example, its context is AnnotationConfigServletWebServerApplicationContext
Instantiate class public static T instantiateClass (Class clazz) throws BeanInstantiationException {Assert.notNull (clazz, "Class must not be null"); if (clazz.isInterface ()) {throw new BeanInstantiationException (clazz, "Specified class is an interface");} try {/ / take the clazz.getDeclaredConstructor method Constructor ctor = (KotlinDetector.isKotlinType (clazz)? KotlinDelegate.getPrimaryConstructor (clazz): clazz.getDeclaredConstructor (); return instantiateClass (ctor);}. / / omitted} public static T instantiateClass (Constructor ctor, Object...) Args) throws BeanInstantiationException {Assert.notNull (ctor, "Constructor must not be null"); try {ReflectionUtils.makeAccessible (ctor); / / follow the clazz.newInstance method return (KotlinDetector.isKotlinType (ctor.getDeclaringClass ())? KotlinDelegate.instantiateClass (ctor, args): ctor.newInstance (args);}. / / omitted}
The initialization context instance results as follows
After reading the above, have you mastered how springboot2.0.6 creates applications? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.