KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.springframework.beans.BeansException;
20 import org.springframework.context.ApplicationContext;
21 import org.springframework.core.io.ClassPathResource;
22 import org.springframework.core.io.Resource;
23 import org.springframework.util.Assert;
24
25 /**
26  * Standalone XML application context, taking the context definition files
27  * from the class path, interpreting plain paths as class path resource names
28  * that include the package path (e.g. "mypackage/myresource.txt"). Useful for
29  * test harnesses as well as for application contexts embedded within JARs.
30  *
31  * <p>The config location defaults can be overridden via {@link #getConfigLocations},
32  * Config locations can either denote concrete files like "/myfiles/context.xml"
33  * or Ant-style patterns like "/myfiles/*-context.xml" (see the
34  * {@link org.springframework.util.AntPathMatcher} javadoc for pattern details).
35  *
36  * <p>Note: In case of multiple config locations, later bean definitions will
37  * override ones defined in earlier loaded files. This can be leveraged to
38  * deliberately override certain bean definitions via an extra XML file.
39  *
40  * <p><b>This is a simple, one-stop shop convenience ApplicationContext.
41  * Consider using the {@link GenericApplicationContext} class in combination
42  * with an {@link org.springframework.beans.factory.xml.XmlBeanDefinitionReader}
43  * for more flexible context setup.</b>
44  *
45  * @author Rod Johnson
46  * @author Juergen Hoeller
47  * @see #getResource
48  * @see #getResourceByPath
49  * @see GenericApplicationContext
50  */

51 public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext {
52
53     private Resource[] configResources;
54
55     private String JavaDoc[] configLocations;
56
57
58     /**
59      * Create a new ClassPathXmlApplicationContext, loading the definitions
60      * from the given XML file and automatically refreshing the context.
61      * @param configLocation resource location
62      * @throws BeansException if context creation failed
63      */

64     public ClassPathXmlApplicationContext(String JavaDoc configLocation) throws BeansException {
65         this(new String JavaDoc[] {configLocation});
66     }
67
68     /**
69      * Create a new ClassPathXmlApplicationContext, loading the definitions
70      * from the given XML files and automatically refreshing the context.
71      * @param configLocations array of resource locations
72      * @throws BeansException if context creation failed
73      */

74     public ClassPathXmlApplicationContext(String JavaDoc[] configLocations) throws BeansException {
75         this(configLocations, (ApplicationContext) null);
76     }
77
78     /**
79      * Create a new ClassPathXmlApplicationContext with the given parent,
80      * loading the definitions from the given XML files and automatically
81      * refreshing the context.
82      * @param configLocations array of resource locations
83      * @param parent the parent context
84      * @throws BeansException if context creation failed
85      */

86     public ClassPathXmlApplicationContext(String JavaDoc[] configLocations, ApplicationContext parent)
87             throws BeansException {
88
89         super(parent);
90         this.configLocations = configLocations;
91         refresh();
92     }
93
94
95     /**
96      * Create a new ClassPathXmlApplicationContext, loading the definitions
97      * from the given XML files.
98      * @param configLocations array of resource locations
99      * @param refresh whether to automatically refresh the context,
100      * loading all bean definitions and creating all singletons.
101      * Alternatively, call refresh manually after further configuring the context.
102      * @throws BeansException if context creation failed
103      * @see #refresh()
104      */

105     public ClassPathXmlApplicationContext(String JavaDoc[] configLocations, 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 JavaDoc[] configLocations, boolean refresh, ApplicationContext parent)
121             throws BeansException {
122
123         super(parent);
124         this.configLocations = configLocations;
125         if (refresh) {
126             refresh();
127         }
128     }
129
130
131     /**
132      * Create a new ClassPathXmlApplicationContext, loading the definitions
133      * from the given XML file and automatically refreshing the context.
134      * <p>This is a convenience method to load class path resources relative to a
135      * given Class. For full flexibility, consider using a GenericApplicationContext
136      * with an XmlBeanDefinitionReader and a ClassPathResource argument.
137      * @param path relative (or absolute) path within the class path
138      * @param clazz the class to load resources with (basis for the given paths)
139      * @throws BeansException if context creation failed
140      * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
141      * @see org.springframework.context.support.GenericApplicationContext
142      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
143      */

144     public ClassPathXmlApplicationContext(String JavaDoc path, Class JavaDoc clazz) throws BeansException {
145         this(new String JavaDoc[] {path}, clazz);
146     }
147
148     /**
149      * Create a new ClassPathXmlApplicationContext, loading the definitions
150      * from the given XML files and automatically refreshing the context.
151      * @param paths array of relative (or absolute) paths within the class path
152      * @param clazz the class to load resources with (basis for the given paths)
153      * @throws BeansException if context creation failed
154      * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
155      * @see org.springframework.context.support.GenericApplicationContext
156      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
157      */

158     public ClassPathXmlApplicationContext(String JavaDoc[] paths, Class JavaDoc clazz) throws BeansException {
159         this(paths, clazz, null);
160     }
161
162     /**
163      * Create a new ClassPathXmlApplicationContext with the given parent,
164      * loading the definitions from the given XML files and automatically
165      * refreshing the context.
166      * @param paths array of relative (or absolute) paths within the class path
167      * @param clazz the class to load resources with (basis for the given paths)
168      * @param parent the parent context
169      * @throws BeansException if context creation failed
170      * @see org.springframework.core.io.ClassPathResource#ClassPathResource(String, Class)
171      * @see org.springframework.context.support.GenericApplicationContext
172      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
173      */

174     public ClassPathXmlApplicationContext(String JavaDoc[] paths, Class JavaDoc clazz, ApplicationContext parent)
175             throws BeansException {
176
177         super(parent);
178         Assert.notNull(paths, "Path array must not be null");
179         Assert.notNull(clazz, "Class argument must not be null");
180         this.configResources = new Resource[paths.length];
181         for (int i = 0; i < paths.length; i++) {
182             this.configResources[i] = new ClassPathResource(paths[i], clazz);
183         }
184         refresh();
185     }
186
187
188     protected Resource[] getConfigResources() {
189         return this.configResources;
190     }
191
192     protected String JavaDoc[] getConfigLocations() {
193         return this.configLocations;
194     }
195
196 }
197
Popular Tags