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