0

I understand what is the purpose of the ContextLoaderListener and the DispatcherServlet.

What I do not understand is why my Sprin-MVC application will start if I DO NOT specify the ContextLoaderListener class in the web.xml file.

I would expect to see an error saying that context is missing or something similar.

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

Web.xml

<display-name>Camel Routes</display-name>

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

dispatcher-servlet.xml

<aop:aspectj-autoproxy/>

<context:component-scan base-package="com.crmProject"/>

<mvc:annotation-driven/>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
      destroy-method="close">
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql://mysql:3306/web_customer_tracker"/>
    <property name="user" value="root"/>
    <property name="password" value="root"/>

    <property name="initialPoolSize" value="5"/>
    <property name="minPoolSize" value="5"/>
    <property name="maxPoolSize" value="20"/>
    <property name="maxIdleTime" value="30000"/>
</bean>

<bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="myDataSource"/>

    <property name="packagesToScan" value="com.crmProject.entity"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<bean id="myTransactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<tx:annotation-driven transaction-manager="myTransactionManager"/>

<mvc:resources location="/resources/" mapping="/resources/**"/>
IonKat
  • 436
  • 6
  • 12
  • 1
    Read this : https://stackoverflow.com/questions/11815339/role-purpose-of-contextloaderlistener-in-spring – Anish B. Feb 29 '20 at 16:01
  • Read this to learn more : [Spring Web Contexts](https://www.baeldung.com/spring-web-contexts) – Andreas Feb 29 '20 at 16:14

2 Answers2

0

Since your context definitions seem to be very conventional and complete you do not need a ContextLoaderListener. Spring can handle all your configurations out of the box.

A ContextLoaderListener comes into play if you have some weird initialization needs which cannot be handled by spring. Implement your own descendant of ContextLoaderListener and configure it (either in the xml file or by annotation). Your implementation is free to do what ever is needed to be done (e.g. fetch configurations from a non standard source in order to inject it into the spring context).

Heri
  • 4,368
  • 1
  • 31
  • 51
0

First of all: context loaded by DispatcherServlet and context loaded by ContextLoaderListener are two different contexts.

DispatcherServlet loads its context on its own. By default it looks for config file using template [servletName]-servlet.xml. So for DispatcherServlet with name dispatcher it's gonna be dispatcher-servlet.xml. But you can specify your own file name using contextConfigLocation servlet paramter:

<servlet>
   <servlet-name>dispatcher</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:custom-name-servlet.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
</servlet>

You can have multiple instances of DispatcherServlet in your application. Every instance represents separate MVC application. And every instance has its own isolated context. DispatcherServlet instances can't access contexts of each other.

ContextLoaderListener loads so called root context. You can have just one root context in your application (or not to have at all). And every instance of DispatcherServlet has access to this context. So you can consider this context as a parent context while context of DispatcherServlet is a child context. Every time your application needs to get an instance of bean, it will look for the bean in child context first. If application doesn't find bean in child context it goes to look for in root context.

You can find more details in Spring documentation. Chapter Context Hierarchy.

Ken Bekov
  • 13,696
  • 3
  • 36
  • 44