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