KickJava   Java API By Example, From Geeks To Geeks.

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


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.ConfigurableListableBeanFactory;
23 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.context.ApplicationContextException;
26
27 /**
28  * Base class for {@link org.springframework.context.ApplicationContext}
29  * implementations which are supposed to support multiple refreshs,
30  * creating a new internal bean factory instance every time.
31  * Typically (but not necessarily), such a context will be driven by
32  * a set of config locations to load bean definitions from.
33  *
34  * <p>The only method to be implemented by subclasses is {@link #loadBeanDefinitions},
35  * which gets invoked on each refresh. A concrete implementation is supposed to load
36  * bean definitions into the given
37  * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory},
38  * typically delegating to one or more specific bean definition readers.
39  *
40  * <p><b>Note that there is a similar base class for WebApplicationContexts.</b>
41  * {@link org.springframework.web.context.support.AbstractRefreshableWebApplicationContext}
42  * provides the same subclassing strategy, but additionally pre-implements
43  * all context functionality for web environments. There is also a
44  * pre-defined way to receive config locations for a web context.
45  *
46  * <p>Concrete standalone subclasses of this base class, reading in a
47  * specific bean definition format, are {@link ClassPathXmlApplicationContext}
48  * and {@link FileSystemXmlApplicationContext}, which both derive from the
49  * common {@link AbstractXmlApplicationContext} base class.
50  *
51  * @author Juergen Hoeller
52  * @since 1.1.3
53  * @see #loadBeanDefinitions
54  * @see org.springframework.beans.factory.support.DefaultListableBeanFactory
55  * @see org.springframework.web.context.support.AbstractRefreshableWebApplicationContext
56  * @see AbstractXmlApplicationContext
57  * @see ClassPathXmlApplicationContext
58  * @see FileSystemXmlApplicationContext
59  */

60 public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {
61
62     /** Bean factory for this context */
63     private DefaultListableBeanFactory beanFactory;
64
65     /** Synchronization monitor for the internal BeanFactory */
66     private final Object JavaDoc beanFactoryMonitor = new Object JavaDoc();
67
68
69     /**
70      * Create a new AbstractRefreshableApplicationContext with no parent.
71      */

72     public AbstractRefreshableApplicationContext() {
73     }
74
75     /**
76      * Create a new AbstractRefreshableApplicationContext with the given parent context.
77      * @param parent the parent context
78      */

79     public AbstractRefreshableApplicationContext(ApplicationContext parent) {
80         super(parent);
81     }
82
83
84     protected final void refreshBeanFactory() throws BeansException {
85         // Shut down previous bean factory, if any.
86
ConfigurableListableBeanFactory oldBeanFactory = null;
87         synchronized (this.beanFactoryMonitor) {
88             oldBeanFactory = this.beanFactory;
89         }
90         if (oldBeanFactory != null) {
91             oldBeanFactory.destroySingletons();
92             synchronized (this.beanFactoryMonitor) {
93                 this.beanFactory = null;
94             }
95         }
96
97         // Initialize fresh bean factory.
98
try {
99             DefaultListableBeanFactory beanFactory = createBeanFactory();
100             loadBeanDefinitions(beanFactory);
101             synchronized (this.beanFactoryMonitor) {
102                 this.beanFactory = beanFactory;
103             }
104         }
105         catch (IOException JavaDoc ex) {
106             throw new ApplicationContextException(
107                     "I/O error parsing XML document for application context [" + getDisplayName() + "]", ex);
108         }
109     }
110
111     protected final void closeBeanFactory() {
112         synchronized (this.beanFactoryMonitor) {
113             this.beanFactory = null;
114         }
115     }
116
117     public final ConfigurableListableBeanFactory getBeanFactory() {
118         synchronized (this.beanFactoryMonitor) {
119             if (this.beanFactory == null) {
120                 throw new IllegalStateException JavaDoc("BeanFactory not initialized or already closed - " +
121                         "call 'refresh' before accessing beans via the ApplicationContext");
122             }
123             return this.beanFactory;
124         }
125     }
126
127
128     /**
129      * Create an internal bean factory for this context.
130      * Called for each {@link #refresh()} attempt.
131      * <p>The default implementation creates a
132      * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory}
133      * with the {@link #getInternalParentBeanFactory() internal bean factory} of this
134      * context's parent as parent bean factory. Can be overridden in subclasses,
135      * for example to customize DefaultListableBeanFactory's settings.
136      * @return the bean factory for this context
137      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
138      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading
139      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
140      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
141      */

142     protected DefaultListableBeanFactory createBeanFactory() {
143         return new DefaultListableBeanFactory(getInternalParentBeanFactory());
144     }
145
146     /**
147      * Customize the internal bean factory used by this context.
148      * Called for each {@link #refresh()} attempt.
149      * <p>The default implementation is empty. Can be overridden in subclasses
150      * to customize DefaultListableBeanFactory's standard settings.
151      * @param beanFactory the newly created bean factory for this context
152      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
153      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading
154      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
155      * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
156      */

157     protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
158     }
159
160     /**
161      * Load bean definitions into the given bean factory, typically through
162      * delegating to one or more bean definition readers.
163      * @param beanFactory the bean factory to load bean definitions into
164      * @throws IOException if loading of bean definition files failed
165      * @throws BeansException if parsing of the bean definitions failed
166      * @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
167      * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader
168      */

169     protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
170             throws IOException JavaDoc, BeansException;
171
172 }
173
Popular Tags