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 org.springframework.beans.BeansException;
020: import org.springframework.context.ApplicationContext;
021: import org.springframework.core.io.ClassPathResource;
022: import org.springframework.core.io.Resource;
023: import org.springframework.util.Assert;
024: import org.springframework.util.StringUtils;
025:
026: /**
027: * Standalone XML application context, taking the context definition files
028: * from the class path, interpreting plain paths as class path resource names
029: * that include the package path (e.g. "mypackage/myresource.txt"). Useful for
030: * test harnesses as well as for application contexts embedded within JARs.
031: *
032: * <p>The config location defaults can be overridden via {@link #getConfigLocations},
033: * Config locations can either denote concrete files like "/myfiles/context.xml"
034: * or Ant-style patterns like "/myfiles/*-context.xml" (see the
035: * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details).
036: *
037: * <p>Note: In case of multiple config locations, later bean definitions will
038: * override ones defined in earlier loaded files. This can be leveraged to
039: * deliberately override certain bean definitions via an extra XML file.
040: *
041: * <p><b>This is a simple, one-stop shop convenience ApplicationContext.
042: * Consider using the {@link GenericApplicationContext} class in combination
043: * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}
044: * for more flexible context setup.</b>
045: *
046: * @author Rod Johnson
047: * @author Juergen Hoeller
048: * @see #getResource
049: * @see #getResourceByPath
050: * @see GenericApplicationContext
051: */
052: public class ClassPathXmlApplicationContext extends
053: AbstractXmlApplicationContext {
054:
055: private Resource[] configResources;
056:
057: private String[] configLocations;
058:
059: /**
060: * Create a new ClassPathXmlApplicationContext, loading the definitions
061: * from the given XML file and automatically refreshing the context.
062: * @param configLocation resource location
063: * @throws BeansException if context creation failed
064: */
065: public ClassPathXmlApplicationContext(String configLocation)
066: throws BeansException {
067: this (new String[] { configLocation }, true, null);
068: }
069:
070: /**
071: * Create a new ClassPathXmlApplicationContext, loading the definitions
072: * from the given XML files and automatically refreshing the context.
073: * @param configLocations array of resource locations
074: * @throws BeansException if context creation failed
075: */
076: public ClassPathXmlApplicationContext(String[] configLocations)
077: throws BeansException {
078: this (configLocations, true, null);
079: }
080:
081: /**
082: * Create a new ClassPathXmlApplicationContext with the given parent,
083: * loading the definitions from the given XML files and automatically
084: * refreshing the context.
085: * @param configLocations array of resource locations
086: * @param parent the parent context
087: * @throws BeansException if context creation failed
088: */
089: public ClassPathXmlApplicationContext(String[] configLocations,
090: ApplicationContext parent) throws BeansException {
091: this (configLocations, true, parent);
092: }
093:
094: /**
095: * Create a new ClassPathXmlApplicationContext, loading the definitions
096: * from the given XML files.
097: * @param configLocations array of resource locations
098: * @param refresh whether to automatically refresh the context,
099: * loading all bean definitions and creating all singletons.
100: * Alternatively, call refresh manually after further configuring the context.
101: * @throws BeansException if context creation failed
102: * @see #refresh()
103: */
104: public ClassPathXmlApplicationContext(String[] configLocations,
105: boolean refresh) throws BeansException {
106: this (configLocations, refresh, null);
107: }
108:
109: /**
110: * Create a new ClassPathXmlApplicationContext with the given parent,
111: * loading the definitions from the given XML files.
112: * @param configLocations array of resource locations
113: * @param refresh whether to automatically refresh the context,
114: * loading all bean definitions and creating all singletons.
115: * Alternatively, call refresh manually after further configuring the context.
116: * @param parent the parent context
117: * @throws BeansException if context creation failed
118: * @see #refresh()
119: */
120: public ClassPathXmlApplicationContext(String[] configLocations,
121: boolean refresh, ApplicationContext parent)
122: throws BeansException {
123:
124: super (parent);
125: this .configLocations = StringUtils
126: .trimArrayElements(configLocations);
127: if (refresh) {
128: refresh();
129: }
130: }
131:
132: /**
133: * Create a new ClassPathXmlApplicationContext, loading the definitions
134: * from the given XML file and automatically refreshing the context.
135: * <p>This is a convenience method to load class path resources relative to a
136: * given Class. For full flexibility, consider using a GenericApplicationContext
137: * with an XmlBeanDefinitionReader and a ClassPathResource argument.
138: * @param path relative (or absolute) path within the class path
139: * @param clazz the class to load resources with (basis for the given paths)
140: * @throws BeansException if context creation failed
141: * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
142: * @see org.springframework.context.support.GenericApplicationContext
143: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
144: */
145: public ClassPathXmlApplicationContext(String path, Class clazz)
146: throws BeansException {
147: this (new String[] { path }, clazz);
148: }
149:
150: /**
151: * Create a new ClassPathXmlApplicationContext, loading the definitions
152: * from the given XML files and automatically refreshing the context.
153: * @param paths array of relative (or absolute) paths within the class path
154: * @param clazz the class to load resources with (basis for the given paths)
155: * @throws BeansException if context creation failed
156: * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
157: * @see org.springframework.context.support.GenericApplicationContext
158: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
159: */
160: public ClassPathXmlApplicationContext(String[] paths, Class clazz)
161: throws BeansException {
162: this (paths, clazz, null);
163: }
164:
165: /**
166: * Create a new ClassPathXmlApplicationContext with the given parent,
167: * loading the definitions from the given XML files and automatically
168: * refreshing the context.
169: * @param paths array of relative (or absolute) paths within the class path
170: * @param clazz the class to load resources with (basis for the given paths)
171: * @param parent the parent context
172: * @throws BeansException if context creation failed
173: * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
174: * @see org.springframework.context.support.GenericApplicationContext
175: * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
176: */
177: public ClassPathXmlApplicationContext(String[] paths, Class clazz,
178: ApplicationContext parent) throws BeansException {
179:
180: super (parent);
181: Assert.notNull(paths, "Path array must not be null");
182: Assert.notNull(clazz, "Class argument must not be null");
183: this .configResources = new Resource[paths.length];
184: for (int i = 0; i < paths.length; i++) {
185: this .configResources[i] = new ClassPathResource(paths[i],
186: clazz);
187: }
188: refresh();
189: }
190:
191: protected Resource[] getConfigResources() {
192: return this .configResources;
193: }
194:
195: protected String[] getConfigLocations() {
196: return this.configLocations;
197: }
198:
199: }
|