001/*
002 * Copyright 2002-2010 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 *      https://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
017package org.springframework.context.support;
018
019import org.springframework.beans.BeansException;
020import org.springframework.context.ApplicationContext;
021import org.springframework.core.io.ClassPathResource;
022import org.springframework.core.io.Resource;
023import 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 */
051public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
052
053        private Resource[] configResources;
054
055
056        /**
057         * Create a new ClassPathXmlApplicationContext for bean-style configuration.
058         * @see #setConfigLocation
059         * @see #setConfigLocations
060         * @see #afterPropertiesSet()
061         */
062        public ClassPathXmlApplicationContext() {
063        }
064
065        /**
066         * Create a new ClassPathXmlApplicationContext for bean-style configuration.
067         * @param parent the parent context
068         * @see #setConfigLocation
069         * @see #setConfigLocations
070         * @see #afterPropertiesSet()
071         */
072        public ClassPathXmlApplicationContext(ApplicationContext parent) {
073                super(parent);
074        }
075
076        /**
077         * Create a new ClassPathXmlApplicationContext, loading the definitions
078         * from the given XML file and automatically refreshing the context.
079         * @param configLocation resource location
080         * @throws BeansException if context creation failed
081         */
082        public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
083                this(new String[] {configLocation}, true, null);
084        }
085
086        /**
087         * Create a new ClassPathXmlApplicationContext, loading the definitions
088         * from the given XML files and automatically refreshing the context.
089         * @param configLocations array of resource locations
090         * @throws BeansException if context creation failed
091         */
092        public ClassPathXmlApplicationContext(String... configLocations) throws BeansException {
093                this(configLocations, true, null);
094        }
095
096        /**
097         * Create a new ClassPathXmlApplicationContext with the given parent,
098         * loading the definitions from the given XML files and automatically
099         * refreshing the context.
100         * @param configLocations array of resource locations
101         * @param parent the parent context
102         * @throws BeansException if context creation failed
103         */
104        public ClassPathXmlApplicationContext(String[] configLocations, ApplicationContext parent) throws BeansException {
105                this(configLocations, true, parent);
106        }
107
108        /**
109         * Create a new ClassPathXmlApplicationContext, loading the definitions
110         * from the given XML files.
111         * @param configLocations array of resource locations
112         * @param refresh whether to automatically refresh the context,
113         * loading all bean definitions and creating all singletons.
114         * Alternatively, call refresh manually after further configuring the context.
115         * @throws BeansException if context creation failed
116         * @see #refresh()
117         */
118        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh) throws BeansException {
119                this(configLocations, refresh, null);
120        }
121
122        /**
123         * Create a new ClassPathXmlApplicationContext with the given parent,
124         * loading the definitions from the given XML files.
125         * @param configLocations array of resource locations
126         * @param refresh whether to automatically refresh the context,
127         * loading all bean definitions and creating all singletons.
128         * Alternatively, call refresh manually after further configuring the context.
129         * @param parent the parent context
130         * @throws BeansException if context creation failed
131         * @see #refresh()
132         */
133        public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
134                        throws BeansException {
135
136                super(parent);
137                setConfigLocations(configLocations);
138                if (refresh) {
139                        refresh();
140                }
141        }
142
143
144        /**
145         * Create a new ClassPathXmlApplicationContext, loading the definitions
146         * from the given XML file and automatically refreshing the context.
147         * <p>This is a convenience method to load class path resources relative to a
148         * given Class. For full flexibility, consider using a GenericApplicationContext
149         * with an XmlBeanDefinitionReader and a ClassPathResource argument.
150         * @param path relative (or absolute) path within the class path
151         * @param clazz the class to load resources with (basis for the given paths)
152         * @throws BeansException if context creation failed
153         * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
154         * @see org.springframework.context.support.GenericApplicationContext
155         * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
156         */
157        public ClassPathXmlApplicationContext(String path, Class<?> clazz) throws BeansException {
158                this(new String[] {path}, clazz);
159        }
160
161        /**
162         * Create a new ClassPathXmlApplicationContext, loading the definitions
163         * from the given XML files and automatically refreshing the context.
164         * @param paths array of relative (or absolute) paths within the class path
165         * @param clazz the class to load resources with (basis for the given paths)
166         * @throws BeansException if context creation failed
167         * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
168         * @see org.springframework.context.support.GenericApplicationContext
169         * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
170         */
171        public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz) throws BeansException {
172                this(paths, clazz, null);
173        }
174
175        /**
176         * Create a new ClassPathXmlApplicationContext with the given parent,
177         * loading the definitions from the given XML files and automatically
178         * refreshing the context.
179         * @param paths array of relative (or absolute) paths within the class path
180         * @param clazz the class to load resources with (basis for the given paths)
181         * @param parent the parent context
182         * @throws BeansException if context creation failed
183         * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
184         * @see org.springframework.context.support.GenericApplicationContext
185         * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
186         */
187        public ClassPathXmlApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent)
188                        throws BeansException {
189
190                super(parent);
191                Assert.notNull(paths, "Path array must not be null");
192                Assert.notNull(clazz, "Class argument must not be null");
193                this.configResources = new Resource[paths.length];
194                for (int i = 0; i < paths.length; i++) {
195                        this.configResources[i] = new ClassPathResource(paths[i], clazz);
196                }
197                refresh();
198        }
199
200
201        @Override
202        protected Resource[] getConfigResources() {
203                return this.configResources;
204        }
205
206}