spring default boot location and contextConfigLocation settings for source resolution

Looking at the spring source these days, which covers where spring starts, let's see where spring actually started loading.This article uses spring3.0M3

The first spring load will go directly to the web.xml file with the help of a listener, ContextLoaderListener

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  • 1
  • 2
  • 3
  • 4

We usually manage loading locations in a unified way

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/conf/spring/**/*.xml
        </param-value>
</context-param>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

This org.springframework.web.context.ContextLoaderListener type is the listener for the original loading context in the springframework.
Usually we customize a Listener to inherit ContextLoaderListener and implement additional interfaces that we need to initialize (usually we choose to implement some to manage session s)

public class FrameServletContextListener extends ContextLoaderListener implements ServletContextListener,HttpSessionAttributeListener,HttpSessionListener {
    //
    private ServletContext initPath(ServletContextEvent event) {

    }

        public synchronized void contextDestroyed(ServletContextEvent event) {
    //
    }

    ...
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

When the listener is set up, the Launch web Container listener starts the ContextLoaderListenerl
Method contextInitialized() in class

    /**
     * Initialize the root web application context.
     */
    public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = createContextLoader();
        if (this.contextLoader == null) {
            this.contextLoader = this;
        }
        this.contextLoader.initWebApplicationContext(event.getServletContext());
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

this.contextLoader.initWebApplicationContext(event.getServletContext());ContextLoaderListener
The ContextLoader class is used to initiate a spring's application context in the context of a container.

We see a piece of static code like this when ContextLoader is initialized

    static {
        // Load default strategy implementations from properties file.
        // This is currently strictly internal and not meant to be customized
        // by application developers.
        try {
            //This sentence loads the value of a property file (ContextLoader.properties) under this package.
            ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class);
            defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
        }
        catch (IOException ex) {
            throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage());
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

This is defined in the properties file
Quote
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
This allows us to reflect an XmlWebApplicationContext context based on the definition in the properties file

However, we see the following variables in the XmlWebApplicationContext

    /** Default config location for the root context */
    public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
  • 1
  • 2

So far we know where to start loading spring files by default, and when we look at the ContextLoader class, we will see the legendary parameter contextConfigLocation

    public static final String CONFIG_LOCATION_PARAM = "contextConfigLocation";
  • 1

The XmlWebApplicationContext object invokes this parameter to set the boot location

    wac.setConfigLocation(servletContext.getInitParameter(CONFIG_LOCATION_PARAM));
  • 1

Look further up at the setConfigLocation method in the AbstractRefreshableConfigApplicationContext class inherited by the XmlWebApplicationContext populating the String[] configLocations value in this abstract class

And in the AbstractRefreshableConfigApplicationContext class we see spring support for default boot file location and configuration boot file location

    protected String[] getConfigLocations() {
        return (this.configLocations != null ? this.configLocations : getDefaultConfigLocations());
    } 
  • 1
  • 2
  • 3

Now we know where spring will load from and which files will be loaded.

Keywords: Programming Spring xml Session

Added by qazwsx on Sun, 08 Dec 2019 19:51:54 +0200