001: /*
002: * Copyright 2002-2007 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: * http://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:
017: package org.springframework.web.context.support;
018:
019: import java.io.IOException;
020:
021: import org.springframework.beans.BeansException;
022: import org.springframework.beans.factory.support.DefaultListableBeanFactory;
023: import org.springframework.beans.factory.xml.ResourceEntityResolver;
024: import 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.AbstractXmlApplicationContext}
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: */
062: public class XmlWebApplicationContext extends
063: AbstractRefreshableWebApplicationContext {
064:
065: /** Default config location for the root context */
066: public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
067:
068: /** Default prefix for building a config location for a namespace */
069: public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
070:
071: /** Default suffix for building a config location for a namespace */
072: public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
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: protected void loadBeanDefinitions(
081: DefaultListableBeanFactory beanFactory) throws IOException {
082: // Create a new XmlBeanDefinitionReader for the given BeanFactory.
083: XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
084: beanFactory);
085:
086: // Configure the bean definition reader with this context's
087: // resource loading environment.
088: beanDefinitionReader.setResourceLoader(this );
089: beanDefinitionReader
090: .setEntityResolver(new ResourceEntityResolver(this ));
091:
092: // Allow a subclass to provide custom initialization of the reader,
093: // then proceed with actually loading the bean definitions.
094: initBeanDefinitionReader(beanDefinitionReader);
095: loadBeanDefinitions(beanDefinitionReader);
096: }
097:
098: /**
099: * Initialize the bean definition reader used for loading the bean
100: * definitions of this context. Default implementation is empty.
101: * <p>Can be overridden in subclasses, e.g. for turning off XML validation
102: * or using a different XmlBeanDefinitionParser implementation.
103: * @param beanDefinitionReader the bean definition reader used by this context
104: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setValidationMode
105: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
106: */
107: protected void initBeanDefinitionReader(
108: XmlBeanDefinitionReader beanDefinitionReader) {
109: }
110:
111: /**
112: * Load the bean definitions with the given XmlBeanDefinitionReader.
113: * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
114: * therefore this method is just supposed to load and/or register bean definitions.
115: * <p>Delegates to a ResourcePatternResolver for resolving location patterns
116: * into Resource instances.
117: * @throws org.springframework.beans.BeansException in case of bean registration errors
118: * @throws java.io.IOException if the required XML document isn't found
119: * @see #refreshBeanFactory
120: * @see #getConfigLocations
121: * @see #getResources
122: * @see #getResourcePatternResolver
123: */
124: protected void loadBeanDefinitions(XmlBeanDefinitionReader reader)
125: throws BeansException, IOException {
126: String[] configLocations = getConfigLocations();
127: if (configLocations != null) {
128: for (int i = 0; i < configLocations.length; i++) {
129: reader.loadBeanDefinitions(configLocations[i]);
130: }
131: }
132: }
133:
134: /**
135: * The default location for the root context is "/WEB-INF/applicationContext.xml",
136: * and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet"
137: * (like for a DispatcherServlet instance with the servlet-name "test").
138: */
139: protected String[] getDefaultConfigLocations() {
140: if (getNamespace() != null) {
141: return new String[] { DEFAULT_CONFIG_LOCATION_PREFIX
142: + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX };
143: } else {
144: return new String[] { DEFAULT_CONFIG_LOCATION };
145: }
146: }
147:
148: }
|