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.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: import org.springframework.context.ApplicationContext;
026: import org.springframework.core.io.Resource;
027:
028: /**
029: * Convenient base class for {@link org.springframework.context.ApplicationContext}
030: * implementations, drawing configuration from XML documents containing bean definitions
031: * understood by an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
032: *
033: * <p>Subclasses just have to implement the {@link #getConfigResources} and/or
034: * the {@link #getConfigLocations} method. Furthermore, they might override
035: * the {@link #getResourceByPath} hook to interpret relative paths in an
036: * environment-specific fashion, and/or {@link #getResourcePatternResolver}
037: * for extended pattern resolution.
038: *
039: * @author Rod Johnson
040: * @author Juergen Hoeller
041: * @see #getConfigResources
042: * @see #getConfigLocations
043: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
044: */
045: public abstract class AbstractXmlApplicationContext extends
046: AbstractRefreshableApplicationContext {
047:
048: /**
049: * Create a new AbstractXmlApplicationContext with no parent.
050: */
051: public AbstractXmlApplicationContext() {
052: }
053:
054: /**
055: * Create a new AbstractXmlApplicationContext with the given parent context.
056: * @param parent the parent context
057: */
058: public AbstractXmlApplicationContext(ApplicationContext parent) {
059: super (parent);
060: }
061:
062: /**
063: * Loads the bean definitions via an XmlBeanDefinitionReader.
064: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
065: * @see #initBeanDefinitionReader
066: * @see #loadBeanDefinitions
067: */
068: protected void loadBeanDefinitions(
069: DefaultListableBeanFactory beanFactory) throws IOException {
070: // Create a new XmlBeanDefinitionReader for the given BeanFactory.
071: XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(
072: beanFactory);
073:
074: // Configure the bean definition reader with this context's
075: // resource loading environment.
076: beanDefinitionReader.setResourceLoader(this );
077: beanDefinitionReader
078: .setEntityResolver(new ResourceEntityResolver(this ));
079:
080: // Allow a subclass to provide custom initialization of the reader,
081: // then proceed with actually loading the bean definitions.
082: initBeanDefinitionReader(beanDefinitionReader);
083: loadBeanDefinitions(beanDefinitionReader);
084: }
085:
086: /**
087: * Initialize the bean definition reader used for loading the bean
088: * definitions of this context. Default implementation is empty.
089: * <p>Can be overridden in subclasses, e.g. for turning off XML validation
090: * or using a different XmlBeanDefinitionParser implementation.
091: * @param beanDefinitionReader the bean definition reader used by this context
092: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
093: */
094: protected void initBeanDefinitionReader(
095: XmlBeanDefinitionReader beanDefinitionReader) {
096: }
097:
098: /**
099: * Load the bean definitions with the given XmlBeanDefinitionReader.
100: * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}
101: * method; hence this method is just supposed to load and/or register bean definitions.
102: * @param reader the XmlBeanDefinitionReader to use
103: * @throws BeansException in case of bean registration errors
104: * @throws IOException if the required XML document isn't found
105: * @see #refreshBeanFactory
106: * @see #getConfigLocations
107: * @see #getResources
108: * @see #getResourcePatternResolver
109: */
110: protected void loadBeanDefinitions(XmlBeanDefinitionReader reader)
111: throws BeansException, IOException {
112: Resource[] configResources = getConfigResources();
113: if (configResources != null) {
114: reader.loadBeanDefinitions(configResources);
115: }
116: String[] configLocations = getConfigLocations();
117: if (configLocations != null) {
118: reader.loadBeanDefinitions(configLocations);
119: }
120: }
121:
122: /**
123: * Return an array of Resource objects, referring to the XML bean definition
124: * files that this context should be built with.
125: * <p>The default implementation returns <code>null</code>. Subclasses can override
126: * this to provide pre-built Resource objects rather than location Strings.
127: * @return an array of Resource objects, or <code>null</code> if none
128: * @see #getConfigLocations()
129: */
130: protected Resource[] getConfigResources() {
131: return null;
132: }
133:
134: /**
135: * Return an array of resource locations, referring to the XML bean definition
136: * files that this context should be built with. Can also include location
137: * patterns, which will get resolved via a ResourcePatternResolver.
138: * <p>The default implementation returns <code>null</code>. Subclasses can override
139: * this to provide a set of resource locations to load bean definitions from.
140: * @return an array of resource locations, or <code>null</code> if none
141: * @see #getResources
142: * @see #getResourcePatternResolver
143: */
144: protected String[] getConfigLocations() {
145: return null;
146: }
147:
148: }
|