KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
20
21 import org.springframework.beans.BeansException;
22 import org.springframework.beans.factory.config.BeanDefinition;
23 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
24 import org.springframework.beans.factory.support.BeanDefinitionRegistry;
25 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
26 import org.springframework.context.ApplicationContext;
27 import org.springframework.core.io.Resource;
28 import org.springframework.core.io.ResourceLoader;
29 import org.springframework.core.io.support.ResourcePatternResolver;
30 import org.springframework.util.Assert;
31
32 /**
33  * Generic ApplicationContext implementation that holds a single internal
34  * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory}
35  * instance and does not assume a specific bean definition format. Implements
36  * the {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}
37  * interface in order to allow for applying any bean definition readers to it.
38  *
39  * <p>Typical usage is to register a variety of bean definitions via the
40  * {@link org.springframework.beans.factory.support.BeanDefinitionRegistry}
41  * interface and then call {@link #refresh()} to initialize those beans
42  * with application context semantics (handling
43  * {@link org.springframework.context.ApplicationContextAware}, auto-detecting
44  * {@link org.springframework.beans.factory.config.BeanFactoryPostProcessor BeanFactoryPostProcessors},
45  * etc).
46  *
47  * <p>In contrast to other ApplicationContext implementations that create a new
48  * internal BeanFactory instance for each refresh, the internal BeanFactory of
49  * this context is available right from the start, to be able to register bean
50  * definitions on it. {@link #refresh()} may only be called once.
51  *
52  * <p>Usage example:
53  *
54  * <pre>
55  * GenericApplicationContext ctx = new GenericApplicationContext();
56  * XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(ctx);
57  * xmlReader.loadBeanDefinitions(new ClassPathResource("applicationContext.xml"));
58  * PropertiesBeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(ctx);
59  * propReader.loadBeanDefinitions(new ClassPathResource("otherBeans.properties"));
60  * ctx.refresh();
61  *
62  * MyBean myBean = (MyBean) ctx.getBean("myBean");
63  * ...</pre>
64  *
65  * For the typical case of XML bean definitions, simply use
66  * {@link ClassPathXmlApplicationContext} or {@link FileSystemXmlApplicationContext},
67  * which are easier to set up - but less flexible, since you can just use standard
68  * resource locations for XML bean definitions, rather than mixing arbitrary bean
69  * definition formats. The equivalent in a web environment is
70  * {@link org.springframework.web.context.support.XmlWebApplicationContext}.
71  *
72  * <p>For custom application context implementations that are supposed to read
73  * special bean definition formats in a refreshable manner, consider deriving
74  * from the {@link AbstractRefreshableApplicationContext} base class.
75  *
76  * @author Juergen Hoeller
77  * @since 1.1.2
78  * @see #registerBeanDefinition
79  * @see #refresh()
80  * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
81  * @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
82  */

83 public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {
84
85     private final DefaultListableBeanFactory beanFactory;
86
87     private ResourceLoader resourceLoader;
88
89     private boolean refreshed = false;
90
91
92     /**
93      * Create a new GenericApplicationContext.
94      * @see #registerBeanDefinition
95      * @see #refresh
96      */

97     public GenericApplicationContext() {
98         this.beanFactory = new DefaultListableBeanFactory();
99     }
100
101     /**
102      * Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
103      * @param beanFactory the DefaultListableBeanFactory instance to use for this context
104      * @see #registerBeanDefinition
105      * @see #refresh
106      */

107     public GenericApplicationContext(DefaultListableBeanFactory beanFactory) {
108         Assert.notNull(beanFactory, "BeanFactory must not be null");
109         this.beanFactory = beanFactory;
110     }
111
112     /**
113      * Create a new GenericApplicationContext with the given parent.
114      * @param parent the parent application context
115      * @see #registerBeanDefinition
116      * @see #refresh
117      */

118     public GenericApplicationContext(ApplicationContext parent) {
119         this();
120         setParent(parent);
121     }
122
123     /**
124      * Create a new GenericApplicationContext with the given DefaultListableBeanFactory.
125      * @param beanFactory the DefaultListableBeanFactory instance to use for this context
126      * @param parent the parent application context
127      * @see #registerBeanDefinition
128      * @see #refresh
129      */

130     public GenericApplicationContext(DefaultListableBeanFactory beanFactory, ApplicationContext parent) {
131         this(beanFactory);
132         setParent(parent);
133     }
134
135
136     /**
137      * Set the parent of this application context, also setting
138      * the parent of the internal BeanFactory accordingly.
139      * @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setParentBeanFactory
140      */

141     public void setParent(ApplicationContext parent) {
142         super.setParent(parent);
143         this.beanFactory.setParentBeanFactory(getInternalParentBeanFactory());
144     }
145
146     /**
147      * Set a ResourceLoader to use for this context. If set, the context will
148      * delegate all <code>getResource</code> calls to the given ResourceLoader.
149      * If not set, default resource loading will apply.
150      * <p>The main reason to specify a custom ResourceLoader is to resolve
151      * resource paths (withour URL prefix) in a specific fashion.
152      * The default behavior is to resolve such paths as class path locations.
153      * To resolve resource paths as file system locations, specify a
154      * FileSystemResourceLoader here.
155      * <p>You can also pass in a full ResourcePatternResolver, which will
156      * be autodetected by the context and used for <code>getResources</code>
157      * calls as well. Else, default resource pattern matching will apply.
158      * @see #getResource
159      * @see org.springframework.core.io.DefaultResourceLoader
160      * @see org.springframework.core.io.FileSystemResourceLoader
161      * @see org.springframework.core.io.support.ResourcePatternResolver
162      * @see #getResources
163      */

164     public void setResourceLoader(ResourceLoader resourceLoader) {
165         this.resourceLoader = resourceLoader;
166     }
167
168
169     /**
170      * This implementation delegates to this context's ResourceLoader if set,
171      * falling back to the default superclass behavior else.
172      * @see #setResourceLoader
173      */

174     public Resource getResource(String JavaDoc location) {
175         if (this.resourceLoader != null) {
176             return this.resourceLoader.getResource(location);
177         }
178         return super.getResource(location);
179     }
180
181     /**
182      * This implementation delegates to this context's ResourceLoader if it
183      * implements the ResourcePatternResolver interface, falling back to the
184      * default superclass behavior else.
185      * @see #setResourceLoader
186      */

187     public Resource[] getResources(String JavaDoc locationPattern) throws IOException JavaDoc {
188         if (this.resourceLoader instanceof ResourcePatternResolver) {
189             return ((ResourcePatternResolver) this.resourceLoader).getResources(locationPattern);
190         }
191         return super.getResources(locationPattern);
192     }
193
194
195     //---------------------------------------------------------------------
196
// Implementations of AbstractApplicationContext's template methods
197
//---------------------------------------------------------------------
198

199     /**
200      * Do nothing: We hold a single internal BeanFactory and rely on callers
201      * to register beans through our public methods (or the BeanFactory's).
202      * @see #registerBeanDefinition
203      */

204     protected final void refreshBeanFactory() throws IllegalStateException JavaDoc {
205         if (this.refreshed) {
206             throw new IllegalStateException JavaDoc(
207                     "GenericApplicationContext does not support multiple refresh attempts: just call 'refresh' once");
208         }
209         this.refreshed = true;
210     }
211
212     /**
213      * Do nothing: We hold a single internal BeanFactory that will never
214      * get released.
215      */

216     protected final void closeBeanFactory() {
217     }
218
219     /**
220      * Return the single internal BeanFactory held by this context
221      * (as ConfigurableListableBeanFactory).
222      */

223     public final ConfigurableListableBeanFactory getBeanFactory() {
224         return this.beanFactory;
225     }
226
227     /**
228      * Return the underlying bean factory of this context,
229      * available for registering bean definitions.
230      * <p><b>NOTE:</b> You need to call {@link #refresh()} to initialize the
231      * bean factory and its contained beans with application context semantics
232      * (autodetecting BeanFactoryPostProcessors, etc).
233      * @return the internal bean factory (as DefaultListableBeanFactory)
234      */

235     public final DefaultListableBeanFactory getDefaultListableBeanFactory() {
236         return this.beanFactory;
237     }
238
239
240     //---------------------------------------------------------------------
241
// Implementation of BeanDefinitionRegistry
242
//---------------------------------------------------------------------
243

244     public BeanDefinition getBeanDefinition(String JavaDoc beanName) throws BeansException {
245         return this.beanFactory.getBeanDefinition(beanName);
246     }
247
248     public void registerBeanDefinition(String JavaDoc beanName, BeanDefinition beanDefinition) throws BeansException {
249         this.beanFactory.registerBeanDefinition(beanName, beanDefinition);
250     }
251
252     public void registerAlias(String JavaDoc beanName, String JavaDoc alias) throws BeansException {
253         this.beanFactory.registerAlias(beanName, alias);
254     }
255
256 }
257
Popular Tags