KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > context > support > AbstractXmlApplicationContext


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.context.support;
18
19 import java.io.IOException JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
23 import org.springframework.beans.factory.xml.ResourceEntityResolver;
24 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
25 import org.springframework.context.ApplicationContext;
26 import org.springframework.core.io.Resource;
27
28 /**
29  * Convenient base class for {@link org.springframework.context.ApplicationContext}
30  * implementations, drawing configuration from XML documents containing bean definitions
31  * understood by an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
32  *
33  * <p>Subclasses just have to implement the {@link #getConfigResources} and/or
34  * the {@link #getConfigLocations} method. Furthermore, they might override
35  * the {@link #getResourceByPath} hook to interpret relative paths in an
36  * environment-specific fashion, and/or {@link #getResourcePatternResolver}
37  * for extended pattern resolution.
38  *
39  * @author Rod Johnson
40  * @author Juergen Hoeller
41  * @see #getConfigResources
42  * @see #getConfigLocations
43  * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
44  */

45 public abstract class AbstractXmlApplicationContext extends AbstractRefreshableApplicationContext {
46
47     /**
48      * Create a new AbstractXmlApplicationContext with no parent.
49      */

50     public AbstractXmlApplicationContext() {
51     }
52
53     /**
54      * Create a new AbstractXmlApplicationContext with the given parent context.
55      * @param parent the parent context
56      */

57     public AbstractXmlApplicationContext(ApplicationContext parent) {
58         super(parent);
59     }
60
61     /**
62      * Loads the bean definitions via an XmlBeanDefinitionReader.
63      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
64      * @see #initBeanDefinitionReader
65      * @see #loadBeanDefinitions
66      */

67     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException JavaDoc {
68         // Create a new XmlBeanDefinitionReader for the given BeanFactory.
69
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
70
71         // Configure the bean definition reader with this context's
72
// resource loading environment.
73
beanDefinitionReader.setResourceLoader(this);
74         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
75
76         // Allow a subclass to provide custom initialization of the reader,
77
// then proceed with actually loading the bean definitions.
78
initBeanDefinitionReader(beanDefinitionReader);
79         loadBeanDefinitions(beanDefinitionReader);
80     }
81
82     /**
83      * Initialize the bean definition reader used for loading the bean
84      * definitions of this context. Default implementation is empty.
85      * <p>Can be overridden in subclasses, e.g. for turning off XML validation
86      * or using a different XmlBeanDefinitionParser implementation.
87      * @param beanDefinitionReader the bean definition reader used by this context
88      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
89      */

90     protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
91     }
92
93     /**
94      * Load the bean definitions with the given XmlBeanDefinitionReader.
95      * <p>The lifecycle of the bean factory is handled by the {@link #refreshBeanFactory}
96      * method; hence this method is just supposed to load and/or register bean definitions.
97      * @param reader the XmlBeanDefinitionReader to use
98      * @throws BeansException in case of bean registration errors
99      * @throws IOException if the required XML document isn't found
100      * @see #refreshBeanFactory
101      * @see #getConfigLocations
102      * @see #getResources
103      * @see #getResourcePatternResolver
104      */

105     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException JavaDoc {
106         Resource[] configResources = getConfigResources();
107         if (configResources != null) {
108             reader.loadBeanDefinitions(configResources);
109         }
110         String JavaDoc[] configLocations = getConfigLocations();
111         if (configLocations != null) {
112             reader.loadBeanDefinitions(configLocations);
113         }
114     }
115
116     /**
117      * Return an array of Resource objects, referring to the XML bean definition
118      * files that this context should be built with.
119      * <p>The default implementation returns <code>null</code>. Subclasses can override
120      * this to provide pre-built Resource objects rather than location Strings.
121      * @return an array of Resource objects, or <code>null</code> if none
122      * @see #getConfigLocations()
123      */

124     protected Resource[] getConfigResources() {
125         return null;
126     }
127
128     /**
129      * Return an array of resource locations, referring to the XML bean definition
130      * files that this context should be built with. Can also include location
131      * patterns, which will get resolved via a ResourcePatternResolver.
132      * <p>The default implementation returns <code>null</code>. Subclasses can override
133      * this to provide a set of resource locations to load bean definitions from.
134      * @return an array of resource locations, or <code>null</code> if none
135      * @see #getResources
136      * @see #getResourcePatternResolver
137      */

138     protected String JavaDoc[] getConfigLocations() {
139         return null;
140     }
141
142 }
143
Popular Tags