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 do ContextLoaderListener Analysis

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

Share

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

How to carry out ContextLoaderListener analysis, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

In every project that integrates the spring framework, it is inevitable to add such a configuration to the web.xml.

ContextConfigLocation

Classpath*:conf/applicationContext.xml

Org.springframework.web.context.ContextLoaderListener

But many students don't know what it does.

Configuration file for web.xml, which automatically assembles Spring applicationContext.xml when the Web container is started

Configuration information for the

Let's take a look at the ContextLoaderListener source.

ContextLoaderListener inherits from ContextLoader and implements the ServletContextListener interface. Public class ContextLoaderListener extends ContextLoader implements

ServletContextListener {

Public ContextLoaderListener () {}

Public ContextLoaderListener (WebApplicationContext context) {super (context);} public void contextInitialized (ServletContextEvent event) {this.initWebApplicationContext (event.getServletContext ());} public void contextDestroyed (ServletContextEvent event) {this.closeWebApplicationContext (event.getServletContext ()); ContextCleanupListener.cleanupAttributes (event.getServletContext ());}

}

ContextLoaderListener can specify that the Ioc container be loaded when the Web application starts, precisely through the

ContextLoader to achieve, ContextLoader to complete the actual WebApplicationContext

That is, the initialization of the Ioc container.

The function of ContextLoaderListener is to start the Web container, read the xml file defined in contextConfigLocation, automatically assemble the configuration information of ApplicationContext, and generate a WebApplicationContext object, and then place this object in ServletContext

So that we can get the WebApplicationContext object as long as we get the Servlet, and use this object to access the spring container-managed bean. To put it simply, the above configuration provides spring support for the project and initializes the Ioc container.

The method called when the context is created is public void contextInitialized (ServletContextEvent event). It calls the initWebApplicationContext method of ContextLoader by passing it to the servlet context it receives (getting event.getServletContext () from the event parameter). The first thing the initWebApplicationContext method does is check to see if another root context exists. If at least another one exists, IllegalStateException is thrown and initialization fails. Otherwise, it continues to initialize

Org.springframework.web.context.WebApplicationContext instance. If the initialized instance implements the ConfigurableWebApplicationContext interface, the loader performs some setup services (parent context, application context, servlet context, etc.) and prepares the bean through the context's refresh () method before setting the current application context

The second method we need to focus on in ContextLoaderListener is public void contextDestroyed (ServletContextEvent event). It is called whenever the context of the loader is closed. This method does two things:

It closes the application context through closeWebApplicationContext () in ContextLoader. Context closure is done through the ConfigurableWebApplicationContext close () method. The process of destroying the context is actually destroying the bean and closing the bean factory, refer to the source code in org.springframework.context.support.AbstractApplicationContext here.

Call

ContextCleanupListener.cleanupAttributes (event.getServletContext ()), which looks for all objects that implement the org.springframework.beans.factory.DisposableBean interface in the current servlet context. After that, their destroy () method is called to destroy the bean that is no longer in use.

ServletContextListener is the listener of ServletContext, if ServletContext

Changes occur, such as when the ServletContext is created when the server is started and when the server is shut down

ServletContext is about to be destroyed.

ServletContext is used by Servlet programs to communicate with Web containers. For example, write a log and forward requests. Each Web application contains a Context, which is shared by various programs within the Web application. Because Context can be used to save resources and share them, all I know about ServletContext

The biggest application of Web cache-reads content that changes infrequently into memory, so the server does not need to do slow disk iCandle O when responding to requests.

In the JSP file, application is an instance of ServletContext and is created by default by the JSP container. Call the getServletContext () method in Servlet to get an instance of ServletContext.

The way we use caching is probably:

1. When the server starts, the contextInitialized () method of ServletContextListener is called, so the cache is created in it. You can read the cache content from the file or from the database to generate the class, and use the ervletContext.setAttribute () method to save the cache class in an instance of ServletContext.

two。 The program uses ServletContext.getAttribute () to read the cache. If it is JSP, use a pplication.getAttribute (). If it is Servlet, use getServletContext () .getAttribute (). If the cache changes (such as access count), you can change the cache and the file / database at the same time. Or you can wait until the changes are accumulated into a certain program and then save them, or you can save them in the next step.

3. When the server is about to shut down, the contextDestroyed () method of ServletContextListener is called, so the cached changes are saved in it. Save the changed cache back to the file or database and update the original content.

After reading the above, have you mastered the method of how to conduct ContextLoaderListener analysis? 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.

Share To

Internet Technology

Wechat

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

12
Report