简体   繁体   中英

Some information about Spring web.xml <context-param> and <listener> tag (referred to an Hello World example)

I am quite new in the Spring MVC World. Today I am studing the simple "Hello World" Example generated by STS doing: File ---> Spring Template Project ---> Spring MVC Project

In the web.xml I have the declaration of the DispatcherServlet and the request mapping handled by it...Up to here everything ok

In the web.xml I have also this part of code:

<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Reading the Spring documentation about ContextLoaderListener I read that this class execute the bootstrap of the listener to start up Spring's root WebApplicationContext but...what it means exactly?

An other doubt is about the contextConfigLocation parameter that I am passing to my context...what is exactly? Opening the /WEB-INF/spring/root-context.xml file it seems that it do not contain any configuration...is it a void configuration file created automatically by my template project creation process? what kind of configuration should contain in a Spring project?

I think tath the and the tags are not used in this Hello World project because if I delete these tag the projext still run well....is it right?

ContextLoaderListener is a class that starts Spring container. Basically every Spring application consists of several beans and wiring (declarative description of which beans depend on each other). This description was historically written in XML (these days we have annotations, Java configuration, CLASSPATH scanning, etc.)

Without Spring container your beans are just Java classes and Spring configuration file is just a useless XML document. ContextLoaderListener reads that file, finds your classes, instantiates them and wires. All your beans are then placed inside a container.

Additionally ContextLoaderListener closes the context (shutting down all the beans if they need some cleanup) on application shutdown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM