KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > context > support > XmlWebApplicationContext


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.web.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
26 /**
27  * {@link org.springframework.web.context.WebApplicationContext} implementation
28  * which takes its configuration from XML documents, understood by an
29  * {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}.
30  * This is essentially the equivalent of
31  * {@link org.springframework.context.support.AbstractXmlApplicationContext}
32  * for a web environment.
33  *
34  * <p>By default, the configuration will be taken from "/WEB-INF/applicationContext.xml"
35  * for the root context, and "/WEB-INF/test-servlet.xml" for a context with the namespace
36  * "test-servlet" (like for a DispatcherServlet instance with the servlet-name "test").
37  *
38  * <p>The config location defaults can be overridden via the "contextConfigLocation"
39  * context-param of {@link org.springframework.web.context.ContextLoader} and servlet
40  * init-param of {@link org.springframework.web.servlet.FrameworkServlet}. Config locations
41  * can either denote concrete files like "/WEB-INF/context.xml" or Ant-style patterns
42  * like "/WEB-INF/*-context.xml" (see {@link org.springframework.util.PathMatcher}
43  * javadoc for pattern details).
44  *
45  * <p>Note: In case of multiple config locations, later bean definitions will
46  * override ones defined in earlier loaded files. This can be leveraged to
47  * deliberately override certain bean definitions via an extra XML file.
48  *
49  * <p><b>For a WebApplicationContext that reads in a different bean definition format,
50  * create an analogous subclass of {@link AbstractRefreshableWebApplicationContext}.</b>
51  * Such a context implementation can be specified as "contextClass" context-param
52  * for ContextLoader or "contextClass" init-param for FrameworkServlet.
53  *
54  * @author Rod Johnson
55  * @author Juergen Hoeller
56  * @see #setNamespace
57  * @see #setConfigLocations
58  * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
59  * @see org.springframework.web.context.ContextLoader#initWebApplicationContext
60  * @see org.springframework.web.servlet.FrameworkServlet#initWebApplicationContext
61  */

62 public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
63
64     /** Default config location for the root context */
65     public static final String JavaDoc DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
66
67     /** Default prefix for building a config location for a namespace */
68     public static final String JavaDoc DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
69
70     /** Default suffix for building a config location for a namespace */
71     public static final String JavaDoc DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
72
73
74     /**
75      * Loads the bean definitions via an XmlBeanDefinitionReader.
76      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
77      * @see #initBeanDefinitionReader
78      * @see #loadBeanDefinitions
79      */

80     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws IOException JavaDoc {
81         // Create a new XmlBeanDefinitionReader for the given BeanFactory.
82
XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
83
84         // Configure the bean definition reader with this context's
85
// resource loading environment.
86
beanDefinitionReader.setResourceLoader(this);
87         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
88
89         // Allow a subclass to provide custom initialization of the reader,
90
// then proceed with actually loading the bean definitions.
91
initBeanDefinitionReader(beanDefinitionReader);
92         loadBeanDefinitions(beanDefinitionReader);
93     }
94
95     /**
96      * Initialize the bean definition reader used for loading the bean
97      * definitions of this context. Default implementation is empty.
98      * <p>Can be overridden in subclasses, e.g. for turning off XML validation
99      * or using a different XmlBeanDefinitionParser implementation.
100      * @param beanDefinitionReader the bean definition reader used by this context
101      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setValidationMode
102      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader#setDocumentReaderClass
103      */

104     protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
105     }
106
107     /**
108      * Load the bean definitions with the given XmlBeanDefinitionReader.
109      * <p>The lifecycle of the bean factory is handled by the refreshBeanFactory method;
110      * therefore this method is just supposed to load and/or register bean definitions.
111      * <p>Delegates to a ResourcePatternResolver for resolving location patterns
112      * into Resource instances.
113      * @throws org.springframework.beans.BeansException in case of bean registration errors
114      * @throws java.io.IOException if the required XML document isn't found
115      * @see #refreshBeanFactory
116      * @see #getConfigLocations
117      * @see #getResources
118      * @see #getResourcePatternResolver
119      */

120     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException JavaDoc {
121         String JavaDoc[] configLocations = getConfigLocations();
122         if (configLocations != null) {
123             for (int i = 0; i < configLocations.length; i++) {
124                 reader.loadBeanDefinitions(configLocations[i]);
125             }
126         }
127     }
128
129     /**
130      * The default location for the root context is "/WEB-INF/applicationContext.xml",
131      * and "/WEB-INF/test-servlet.xml" for a context with the namespace "test-servlet"
132      * (like for a DispatcherServlet instance with the servlet-name "test").
133      */

134     protected String JavaDoc[] getDefaultConfigLocations() {
135         if (getNamespace() != null) {
136             return new String JavaDoc[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
137         }
138         else {
139             return new String JavaDoc[] {DEFAULT_CONFIG_LOCATION};
140         }
141     }
142
143 }
144
Popular Tags