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

24,439

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.

Share:
24,439
AndreaNobili
Author by

AndreaNobili

Updated on July 05, 2022

Comments

  • AndreaNobili
    AndreaNobili almost 2 years

    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?

  • AndreaNobili
    AndreaNobili over 11 years
    mmm I think that I am understanding what you would see to me but I still have some doubt... So in practice, refering to a simple HelloWorld example, you are told me that if I have not enabled the ContextLoaderListener this is a simple Java WebApp that can't use many features made available by Spring framework? So, for example, without it I can't handled the autowired object inside my bean Is it? or I am missing something? Tnx Andrea
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz over 11 years
    @AndreaNobili: exactly! Without ContextLoaderListener (there are more modern ways to bootstrap Spring now, but put aside them) your beans are just Java classes. Not autowiring, transactions, AOP. You could as well just remove Spring JARs from your app.
  • AndreaNobili
    AndreaNobili over 11 years
    so you are sayng me that, in theory, I could delete the ContextLoaderListener declaration from my web.xml file and bootstrap Spring in some other way?
  • Tomasz Nurkiewicz
    Tomasz Nurkiewicz over 11 years
    @AndreaNobili: of course. For example you can start context on first HTTP request or only when user supplies some configuration via simple non-Spring servlet.