V
V
Valentine2016-02-17 20:27:46
Java
Valentine, 2016-02-17 20:27:46

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>

By the way, I would like to clarify one point right away: If my mvc-dispatcher is found in WEB-INF/ , then you can not explicitly specify contextConfigLocation for it, if it also matches the file name, without the -servlet suffix?
Let's get back to the main question: I saw that many people specify in web.xml, additional parameters, in which my application works exactly the same:
<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>

Unfortunately, I could not google a normal explanation of what a listener is and what it serves for, I will be very grateful if you open its meaning. I also want to understand what exactly the initialization of the contextConfigLocation parameter refers to.
From my assumptions, only that this is some kind of logical separation that is convenient for the developer, that is, in one part, write some beans (Just which ones), and in the other, for the web, where it should be, let's say InternalResourceViewResolver is described.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
angry_cellophane, 2016-02-18
@Waynes

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.

E
Eugene, 2016-02-18
@zolt85

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 question

Ask a Question

731 491 924 answers to any question