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.ServletContext;
020import javax.servlet.ServletException;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.springframework.context.ApplicationContextInitializer;
026import org.springframework.web.WebApplicationInitializer;
027
028/**
029 * Convenient base class for {@link WebApplicationInitializer} implementations
030 * that register a {@link ContextLoaderListener} in the servlet context.
031 *
032 * <p>The only method required to be implemented by subclasses is
033 * {@link #createRootApplicationContext()}, which gets invoked from
034 * {@link #registerContextLoaderListener(ServletContext)}.
035 *
036 * @author Arjen Poutsma
037 * @author Chris Beams
038 * @author Juergen Hoeller
039 * @since 3.2
040 */
041public abstract class AbstractContextLoaderInitializer implements WebApplicationInitializer {
042
043        /** Logger available to subclasses */
044        protected final Log logger = LogFactory.getLog(getClass());
045
046
047        @Override
048        public void onStartup(ServletContext servletContext) throws ServletException {
049                registerContextLoaderListener(servletContext);
050        }
051
052        /**
053         * Register a {@link ContextLoaderListener} against the given servlet context. The
054         * {@code ContextLoaderListener} is initialized with the application context returned
055         * from the {@link #createRootApplicationContext()} template method.
056         * @param servletContext the servlet context to register the listener against
057         */
058        protected void registerContextLoaderListener(ServletContext servletContext) {
059                WebApplicationContext rootAppContext = createRootApplicationContext();
060                if (rootAppContext != null) {
061                        ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);
062                        listener.setContextInitializers(getRootApplicationContextInitializers());
063                        servletContext.addListener(listener);
064                }
065                else {
066                        logger.debug("No ContextLoaderListener registered, as " +
067                                        "createRootApplicationContext() did not return an application context");
068                }
069        }
070
071        /**
072         * Create the "<strong>root</strong>" application context to be provided to the
073         * {@code ContextLoaderListener}.
074         * <p>The returned context is delegated to
075         * {@link ContextLoaderListener#ContextLoaderListener(WebApplicationContext)} and will
076         * be established as the parent context for any {@code DispatcherServlet} application
077         * contexts. As such, it typically contains middle-tier services, data sources, etc.
078         * @return the root application context, or {@code null} if a root context is not
079         * desired
080         * @see org.springframework.web.servlet.support.AbstractDispatcherServletInitializer
081         */
082        protected abstract WebApplicationContext createRootApplicationContext();
083
084        /**
085         * Specify application context initializers to be applied to the root application
086         * context that the {@code ContextLoaderListener} is being created with.
087         * @since 4.2
088         * @see #createRootApplicationContext()
089         * @see ContextLoaderListener#setContextInitializers
090         */
091        protected ApplicationContextInitializer<?>[] getRootApplicationContextInitializers() {
092                return null;
093        }
094
095}