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 does Springboot get the context ApplicationContext

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

What this article shares with you is about how Springboot obtains the context ApplicationContext. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Springboot gets context ApplicationContext

A scenario is encountered in the project, that is, by getting the context and then getting a specific bean. I encountered a big hole here, so I left this article and made a record.

Import org.springframework.beans.BeansException;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; @ Componentpublic class SpringContextUtil implements ApplicationContextAware {/ * contextual object instance * / private static ApplicationContext applicationContext; @ Autowired public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext } / * get applicationContext * @ return * / public static ApplicationContext getApplicationContext () {return applicationContext;} / * get Bean through name. * @ param name * @ return * / public static Object getBean (String name) {return getApplicationContext () .getBean (name);} / * obtain Bean through class. * @ param clazz * @ param * @ return * / public static T getBean (Class clazz) {return getApplicationContext () .getBean (clazz) } / * return the specified Bean * @ param name * @ param clazz * @ param * @ return * / public static T getBean (String name,Class clazz) {return getApplicationContext (). GetBean (name, clazz);}} via name and Clazz

Looking at the above code, you can see that a static variable of type ApplicationContext is declared in the tool class at the beginning, but because the static variable cannot be managed by the Spring container, a specific bean cannot be obtained by using normal getter and setter methods at first. It has been proved that @ Autowired annotation is needed on the setter method of this variable and the static keyword in the setter method is removed. To achieve the injection of a specific bean.

Application context of springboot

There are two springboot contexts

ServletWebServerApplicationContext

AnnotationConfigServletWebServerApplicationContext (inherits the above)

ServletWebServerApplicationContext

This class belongs to spring-boot-2.1.1Release.jar, is derived from springboot, and is a subclass of Application in the application context of spring framework.

It's no use talking too much, show me code.

Extended featur

First of all, let's take a look at what this class has done and what is the value of its existence.

Private volatile WebServer webServer; @ Override protected void onRefresh () {super.onRefresh (); try {createWebServer ();} catch (Throwable ex) {throw new ApplicationContextException ("Unable to start web server", ex);}}

There is a WebServer member variable in this class, which we should know with our toes. This is actually a web service object, and it can basically be guessed that it is related to tomcat (of course, it can also be other web servers, such as jetty).

Then we found the onRefresh method, which I believe is no stranger to us. This is one of the hook methods of the spring core refresh method (that is, indicating that all the configured bean has been loaded at this time) to create the WebServer object.

@ Override protected void finishRefresh () {super.finishRefresh (); WebServer webServer = startWebServer (); if (webServer! = null) {publishEvent (new ServletWebServerInitializedEvent (webServer, this));}}

We also found that there is a finishRefresh in this class. Think about it, this is also a hook method in the spring core # refresh method (but this is special because it is the last step in the refresh method, that is, all beandefinition objects in the spring container are de-instantiated)

First of all, praise one, this is very clever, called super.finishRefresh (), and did not discard the logic function of the parent class (this point in polymorphism, I believe someone will still make a mistake, originally is to extend the function, but directly rewrite, discard the methods of the parent class, of course, spring framework developers will not make such a mistake, right! )

The second point is that startWebServer, that is, after spring is instantiated, starts the web service.

AnnotationConfigServletWebServerApplicationContext

First of all, this class is the implementation class that springboot launches to run run () to create ApplicationContext, but unfortunately, this class does not have a strong substantial extension.

The only function is to have the function of loading configuration classes through annotations, that is, the same as AnnotationConfigApplication, except that the running startup of springboot is already loading bean classes through annotations * *

Although it is a chicken rib, it is also in line with spring's consistent style of creating classes, that is, each class is highly cohesive, that is, each class has other extended functions in addition to the functions of the parent class. Even if it is abandoned before it is used, it still can't stop spring developers from creating this class, )

This is how Springboot obtains the context ApplicationContext. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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