001/* 002 * Copyright 2002-2014 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.support; 018 019import java.io.IOException; 020 021import org.springframework.beans.BeansException; 022import org.springframework.beans.factory.support.DefaultListableBeanFactory; 023import org.springframework.beans.factory.xml.ResourceEntityResolver; 024import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; 025 026/** 027 * {@link org.springframework.web.context.WebApplicationContext} implementation 028 * which takes its configuration from XML documents, understood by an 029 * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}. 030 * This is essentially the equivalent of 031 * {@link org.springframework.context.support.GenericXmlApplicationContext} 032 * for a web environment. 033 * 034 * <p>By default, the configuration will be taken from "/WEB-INF/applicationContext.xml" 035 * for the root context, and "/WEB-INF/test-servlet.xml" for a context with the namespace 036 * "test-servlet" (like for a DispatcherServlet instance with the servlet-name "test"). 037 * 038 * <p>The config location defaults can be overridden via the "contextConfigLocation" 039 * context-param of {@link org.springframework.web.context.ContextLoader} and servlet 040 * init-param of {@link org.springframework.web.servlet.FrameworkServlet}. Config locations 041 * can either denote concrete files like "/WEB-INF/context.xml" or Ant-style patterns 042 * like "/WEB-INF/*-context.xml" (see {@link org.springframework.util.PathMatcher} 043 * javadoc for pattern details). 044 * 045 * <p>Note: In case of multiple config locations, later bean definitions will 046 * override ones defined in earlier loaded files. This can be leveraged to 047 * deliberately override certain bean definitions via an extra XML file. 048 * 049 * <p><b>For a WebApplicationContext that reads in a different bean definition format, 050 * create an analogous subclass of {@link AbstractRefreshableWebApplicationContext}.</b> 051 * Such a context implementation can be specified as "contextClass" context-param 052 * for ContextLoader or "contextClass" init-param for FrameworkServlet. 053 * 054 * @author Rod Johnson 055 * @author Juergen Hoeller 056 * @see #setNamespace 057 * @see #setConfigLocations 058 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 059 * @see org.springframework.web.context.ContextLoader#initWebApplicationContext 060 * @see org.springframework.web.servlet.FrameworkServlet#initWebApplicationContext 061 */ 062public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext { 063 064 /** Default config location for the root context */ 065 public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml"; 066 067 /** Default prefix for building a config location for a namespace */ 068 public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/"; 069 070 /** Default suffix for building a config location for a namespace */ 071 public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml"; 072 073 074 /** 075 * Loads the bean definitions via an XmlBeanDefinitionReader. 076 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 077 * @see #initBeanDefinitionReader 078 * @see #loadBeanDefinitions 079 */ 080 @Override 081 protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException { 082 // Create a new XmlBeanDefinitionReader for the given BeanFactory. 083 XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); 084 085 // Configure the bean definition reader with this context's 086 // resource loading environment. 087 beanDefinitionReader.setEnvironment(getEnvironment()); 088 beanDefinitionReader.setResourceLoader(this); 089 beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this)); 090 091 // Allow a subclass to provide custom initialization of the reader, 092 // then proceed with actually loading the bean definitions. 093 initBeanDefinitionReader(beanDefinitionReader); 094 loadBeanDefinitions(beanDefinitionReader); 095 } 096 097 /** 098 * Initialize the bean definition reader used for loading the bean 099 * definitions of this context. Default implementation is empty. 100 * <p>Can be overridden in subclasses, e.g. for turning off XML validation 101 * or using a different XmlBeanDefinitionParser implementation. 102 * @param beanDefinitionReader the bean definition reader used by this context 103 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setValidationMode 104 * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass 105 */ 106 protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) { 107 } 108 109 /** 110 * Load the bean definitions with the given XmlBeanDefinitionReader. 111 * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method; 112 * therefore this method is just supposed to load and/or register bean definitions. 113 * <p>Delegates to a ResourcePatternResolver for resolving location patterns 114 * into Resource instances. 115 * @throws IOException if the required XML document isn't found 116 * @see #refreshBeanFactory 117 * @see #getConfigLocations 118 * @see #getResources 119 * @see #getResourcePatternResolver 120 */ 121 protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException { 122 String[] configLocations = getConfigLocations(); 123 if (configLocations != null) { 124 for (String configLocation : configLocations) { 125 reader.loadBeanDefinitions(configLocation); 126 } 127 } 128 } 129 130 /** 131 * The default location for the root context is "/WEB-INF/applicationContext.xml", 132 * and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet" 133 * (like for a DispatcherServlet instance with the servlet-name "test"). 134 */ 135 @Override 136 protected String[] getDefaultConfigLocations() { 137 if (getNamespace() != null) { 138 return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX}; 139 } 140 else { 141 return new String[] {DEFAULT_CONFIG_LOCATION}; 142 } 143 } 144 145}