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

て烟熏妆下的殇ゞ 提交于 2019-12-09 04:19:30

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/13650695/some-information-about-spring-web-xml-context-param-and-listener-tag-referr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!