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 of the context of Spring MVC in the Web container

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces how the context of Spring MVC starts in the Web container, the content is very detailed, interested friends can refer to it, I hope it can be helpful to you.

MVC in Web Environment

Spring IoC is a stand-alone module and cannot play a role directly in the Web container.

So to use the IoC container in the Web container, you need to design a startup process for Spring IoC and import the IoC container

The startup process of the Web container handles the startup of the Web container on the one hand, and loads the IoC container into the web environment and initializes it on the other hand.

Spring MVC is based on the IoC container, and the MVC can be established only after the IoC container is imported.

Context startup process in the Web container

In a common web.xml, you need to configure a servlet of type DispatcherServlet and a listener of type ContextLoaderListener.

Spring MVC establishes the MVC in the Web container through these two classes, and puts the created container into the ServletContext

ContextLoaderListener is used to start Spring IoC, creating an IoC container as a "root container"

DispatcherServlet creates another IoC container and builds parent containers with the root container to complete the establishment of MVC

ContextLoaderListener calls the method contextInitialized () to start the IoC container.

DispatcherServlet calls the init () method of the parent class to create the IoC container and build the parent container, and build the MVC based on the built IoC container.

Create a root context

This initWebApplicationContext () method is implemented in the parent class ContextLoader of ContextLoaderListener

Public WebApplicationContext initWebApplicationContext (ServletContext servletContext) {/ / if the root container already exists in ServletContext, you do not need to execute the following process if (servletContext.getAttribute (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)! = null) {throw...} try {if (this.context = = null) {/ / create the container this.context = createWebApplicationContext (servletContext) } if (this.context instanceof ConfigurableWebApplicationContext) {ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context / / if the container has not been initialized, execute the following process if (! cwac.isActive ()) {if (cwac.getParent () = = null) {/ / Spring5 this method directly returns a null value ApplicationContext parent = loadParentContext (servletContext); cwac.setParent (parent) } / / execute the refresh () method of the container to refresh the container configureAndRefreshWebApplicationContext (cwac, servletContext);}} / / use the container as the root container. Add it to ServletContext with the specified constant as key and container as value servletContext.setAttribute (WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); ClassLoader ccl = Thread.currentThread (). GetContextClassLoader (); if (ccl = = ContextLoader.class.getClassLoader ()) {currentContext = this.context;} else if (ccl! = null) {currentContextPerThread.put (ccl, this.context) } return this.context;} catch (Error err) {throw err;}} create an IoC container in DispatcherServlet

Init ()-initServletBean ()-initWebApplicationContext ()

This initWebApplicationContext () method is implemented in the parent class FrameworkServlet of DispatcherServlet

Protected WebApplicationContext initWebApplicationContext () {/ / get the root container WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext (getServletContext ()) from ServletContext; WebApplicationContext wac = null; / / default this.webApplicationContext==null if (this.webApplicationContext! = null) {wac = this.webApplicationContext; if (wac instanceof ConfigurableWebApplicationContext) {ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac If (! cwac.isActive ()) {if (cwac.getParent () = = null) {/ / use the root container as the parent container cwac.setParent (rootContext);} / start the container configureAndRefreshWebApplicationContext (cwac) } if (wac = = null) {wac = findWebApplicationContext ();} if (wac = = null) {/ / if wac is still null, create a container and set the root container as the parent container wac = createWebApplicationContext (rootContext) } if (! this.refreshEventReceived) {/ / build MVC to initialize the nine components of Spring MVC onRefresh (wac);} / / get the container name created by DispatcherServlet and put it in ServletContext in kv way if (this.publishContext) {String attrName = getServletContextAttributeName (); getServletContext (). SetAttribute (attrName, wac);} return wac } protected WebApplicationContext createWebApplicationContext (@ Nullable ApplicationContext parent) {Class contextClass = getContextClass (); if (! ConfigurableWebApplicationContext.class.isAssignableFrom (contextClass)) {throw.} ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass (contextClass); wac.setEnvironment (getEnvironment ()); / / set parent container wac.setParent (parent); String configLocation = getContextConfigLocation () If (configLocation! = null) {wac.setConfigLocation (configLocation);} / * * configure and refresh the container * when the container is refreshed, a BeanFactory object is created for the container. * when calling the constructor of DefaultListableBeanFactory, an attempt is made to get the parent container and use the parent container itself or the BeanFactory held by the parent container as the parentBeanFactory of the BeanFactory. This parentBeanFactory will be used in the getBean (before getting bean, try to get the bean from the parent factory) * / configureAndRefreshWebApplicationContext (wac); return wac } this is the end of how the context of Spring MVC starts in the Web container. I hope the above content can be of some help and learn more. If you think the article is good, you can share it for more people to see.

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

Internet Technology

Wechat

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

12
Report