Answer the question
In order to leave comments, you need to log in
What does listener-class mean in web.xml and what role does contextConfigLocation play?
Good day to all! I'm interested in a question related to understanding the meaning of configuring Spring MVC options, in a deployment descriptor file (web.xml). I can usually deploy a spring web application running on Tomcat by configuring only the DispatcherServlet in the web.xml:
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Answer the question
In order to leave comments, you need to log in
Both the listener and the dispatcher raise an ApplicationContext. First raises the lisener, then the dispatcher raises the child context. Thus, in the bins loaded by the dispatcher, you can make references to the bins loaded by the listener.
The main question is why?
For security. Spring security configuration is written in the licener, and beans from spring mvc in the dispatcher. That. beans from the application can use beans for security, but not vice versa.
You can read more in some book on spring security, for example Spring Security 3.1.
The very name listener, as it were, hints that this is a kind of listener. Those. it is designed to listen to certain events. Spring has its own implementation of the event model, as far as I remember. Regarding the config, yes, you can not specify it, but in order not to force the classpath when the application starts, you can directly specify the configuration file. Those. it's just a set of such rules, following which, you are guaranteed the correct start and operation of the application.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question