001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.context;
018
019import javax.servlet.ServletContextEvent;
020import javax.servlet.ServletContextListener;
021
022/**
023 * Bootstrap listener to start up and shut down Spring's root {@link WebApplicationContext}.
024 * Simply delegates to {@link ContextLoader} as well as to {@link ContextCleanupListener}.
025 *
026 * <p>This listener should be registered after {@link org.springframework.web.util.Log4jConfigListener}
027 * in {@code web.xml}, if the latter is used.
028 *
029 * <p>As of Spring 3.1, {@code ContextLoaderListener} supports injecting the root web
030 * application context via the {@link #ContextLoaderListener(WebApplicationContext)}
031 * constructor, allowing for programmatic configuration in Servlet 3.0+ environments.
032 * See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
033 *
034 * @author Juergen Hoeller
035 * @author Chris Beams
036 * @since 17.02.2003
037 * @see #setContextInitializers
038 * @see org.springframework.web.WebApplicationInitializer
039 * @see org.springframework.web.util.Log4jConfigListener
040 */
041public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
042
043        /**
044         * Create a new {@code ContextLoaderListener} that will create a web application
045         * context based on the "contextClass" and "contextConfigLocation" servlet
046         * context-params. See {@link ContextLoader} superclass documentation for details on
047         * default values for each.
048         * <p>This constructor is typically used when declaring {@code ContextLoaderListener}
049         * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is
050         * required.
051         * <p>The created application context will be registered into the ServletContext under
052         * the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
053         * and the Spring application context will be closed when the {@link #contextDestroyed}
054         * lifecycle method is invoked on this listener.
055         * @see ContextLoader
056         * @see #ContextLoaderListener(WebApplicationContext)
057         * @see #contextInitialized(ServletContextEvent)
058         * @see #contextDestroyed(ServletContextEvent)
059         */
060        public ContextLoaderListener() {
061        }
062
063        /**
064         * Create a new {@code ContextLoaderListener} with the given application context. This
065         * constructor is useful in Servlet 3.0+ environments where instance-based
066         * registration of listeners is possible through the {@link javax.servlet.ServletContext#addListener}
067         * API.
068         * <p>The context may or may not yet be {@linkplain
069         * org.springframework.context.ConfigurableApplicationContext#refresh() refreshed}. If it
070         * (a) is an implementation of {@link ConfigurableWebApplicationContext} and
071         * (b) has <strong>not</strong> already been refreshed (the recommended approach),
072         * then the following will occur:
073         * <ul>
074         * <li>If the given context has not already been assigned an {@linkplain
075         * org.springframework.context.ConfigurableApplicationContext#setId id}, one will be assigned to it</li>
076         * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to
077         * the application context</li>
078         * <li>{@link #customizeContext} will be called</li>
079         * <li>Any {@link org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer}s
080         * specified through the "contextInitializerClasses" init-param will be applied.</li>
081         * <li>{@link org.springframework.context.ConfigurableApplicationContext#refresh refresh()} will be called</li>
082         * </ul>
083         * If the context has already been refreshed or does not implement
084         * {@code ConfigurableWebApplicationContext}, none of the above will occur under the
085         * assumption that the user has performed these actions (or not) per his or her
086         * specific needs.
087         * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
088         * <p>In any case, the given application context will be registered into the
089         * ServletContext under the attribute name {@link
090         * WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring
091         * application context will be closed when the {@link #contextDestroyed} lifecycle
092         * method is invoked on this listener.
093         * @param context the application context to manage
094         * @see #contextInitialized(ServletContextEvent)
095         * @see #contextDestroyed(ServletContextEvent)
096         */
097        public ContextLoaderListener(WebApplicationContext context) {
098                super(context);
099        }
100
101
102        /**
103         * Initialize the root web application context.
104         */
105        @Override
106        public void contextInitialized(ServletContextEvent event) {
107                initWebApplicationContext(event.getServletContext());
108        }
109
110
111        /**
112         * Close the root web application context.
113         */
114        @Override
115        public void contextDestroyed(ServletContextEvent event) {
116                closeWebApplicationContext(event.getServletContext());
117                ContextCleanupListener.cleanupAttributes(event.getServletContext());
118        }
119
120}