001/*
002 * Copyright 2002-2020 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;
018
019import java.io.Closeable;
020
021import org.springframework.beans.BeansException;
022import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
023import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
024import org.springframework.core.env.ConfigurableEnvironment;
025import org.springframework.core.env.Environment;
026import org.springframework.core.io.ProtocolResolver;
027
028/**
029 * SPI interface to be implemented by most if not all application contexts.
030 * Provides facilities to configure an application context in addition
031 * to the application context client methods in the
032 * {@link org.springframework.context.ApplicationContext} interface.
033 *
034 * <p>Configuration and lifecycle methods are encapsulated here to avoid
035 * making them obvious to ApplicationContext client code. The present
036 * methods should only be used by startup and shutdown code.
037 *
038 * @author Juergen Hoeller
039 * @author Chris Beams
040 * @since 03.11.2003
041 */
042public interface ConfigurableApplicationContext extends ApplicationContext, Lifecycle, Closeable {
043
044        /**
045         * Any number of these characters are considered delimiters between
046         * multiple context config paths in a single String value.
047         * @see org.springframework.context.support.AbstractXmlApplicationContext#setConfigLocation
048         * @see org.springframework.web.context.ContextLoader#CONFIG_LOCATION_PARAM
049         * @see org.springframework.web.servlet.FrameworkServlet#setContextConfigLocation
050         */
051        String CONFIG_LOCATION_DELIMITERS = ",; \t\n";
052
053        /**
054         * Name of the ConversionService bean in the factory.
055         * If none is supplied, default conversion rules apply.
056         * @since 3.0
057         * @see org.springframework.core.convert.ConversionService
058         */
059        String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
060
061        /**
062         * Name of the LoadTimeWeaver bean in the factory. If such a bean is supplied,
063         * the context will use a temporary ClassLoader for type matching, in order
064         * to allow the LoadTimeWeaver to process all actual bean classes.
065         * @since 2.5
066         * @see org.springframework.instrument.classloading.LoadTimeWeaver
067         */
068        String LOAD_TIME_WEAVER_BEAN_NAME = "loadTimeWeaver";
069
070        /**
071         * Name of the {@link Environment} bean in the factory.
072         * @since 3.1
073         */
074        String ENVIRONMENT_BEAN_NAME = "environment";
075
076        /**
077         * Name of the System properties bean in the factory.
078         * @see java.lang.System#getProperties()
079         */
080        String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties";
081
082        /**
083         * Name of the System environment bean in the factory.
084         * @see java.lang.System#getenv()
085         */
086        String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";
087
088
089        /**
090         * Set the unique id of this application context.
091         * @since 3.0
092         */
093        void setId(String id);
094
095        /**
096         * Set the parent of this application context.
097         * <p>Note that the parent shouldn't be changed: It should only be set outside
098         * a constructor if it isn't available when an object of this class is created,
099         * for example in case of WebApplicationContext setup.
100         * @param parent the parent context
101         * @see org.springframework.web.context.ConfigurableWebApplicationContext
102         */
103        void setParent(ApplicationContext parent);
104
105        /**
106         * Set the {@code Environment} for this application context.
107         * @param environment the new environment
108         * @since 3.1
109         */
110        void setEnvironment(ConfigurableEnvironment environment);
111
112        /**
113         * Return the {@code Environment} for this application context in configurable
114         * form, allowing for further customization.
115         * @since 3.1
116         */
117        @Override
118        ConfigurableEnvironment getEnvironment();
119
120        /**
121         * Add a new BeanFactoryPostProcessor that will get applied to the internal
122         * bean factory of this application context on refresh, before any of the
123         * bean definitions get evaluated. To be invoked during context configuration.
124         * @param postProcessor the factory processor to register
125         */
126        void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor);
127
128        /**
129         * Add a new ApplicationListener that will be notified on context events
130         * such as context refresh and context shutdown.
131         * <p>Note that any ApplicationListener registered here will be applied
132         * on refresh if the context is not active yet, or on the fly with the
133         * current event multicaster in case of a context that is already active.
134         * @param listener the ApplicationListener to register
135         * @see org.springframework.context.event.ContextRefreshedEvent
136         * @see org.springframework.context.event.ContextClosedEvent
137         */
138        void addApplicationListener(ApplicationListener<?> listener);
139
140        /**
141         * Register the given protocol resolver with this application context,
142         * allowing for additional resource protocols to be handled.
143         * <p>Any such resolver will be invoked ahead of this context's standard
144         * resolution rules. It may therefore also override any default rules.
145         * @since 4.3
146         */
147        void addProtocolResolver(ProtocolResolver resolver);
148
149        /**
150         * Load or refresh the persistent representation of the configuration, which
151         * might be from Java-based configuration, an XML file, a properties file, a
152         * relational database schema, or some other format.
153         * <p>As this is a startup method, it should destroy already created singletons
154         * if it fails, to avoid dangling resources. In other words, after invocation
155         * of this method, either all or no singletons at all should be instantiated.
156         * @throws BeansException if the bean factory could not be initialized
157         * @throws IllegalStateException if already initialized and multiple refresh
158         * attempts are not supported
159         */
160        void refresh() throws BeansException, IllegalStateException;
161
162        /**
163         * Register a shutdown hook with the JVM runtime, closing this context
164         * on JVM shutdown unless it has already been closed at that time.
165         * <p>This method can be called multiple times. Only one shutdown hook
166         * (at max) will be registered for each context instance.
167         * @see java.lang.Runtime#addShutdownHook
168         * @see #close()
169         */
170        void registerShutdownHook();
171
172        /**
173         * Close this application context, releasing all resources and locks that the
174         * implementation might hold. This includes destroying all cached singleton beans.
175         * <p>Note: Does <i>not</i> invoke {@code close} on a parent context;
176         * parent contexts have their own, independent lifecycle.
177         * <p>This method can be called multiple times without side effects: Subsequent
178         * {@code close} calls on an already closed context will be ignored.
179         */
180        @Override
181        void close();
182
183        /**
184         * Determine whether this application context is active, that is,
185         * whether it has been refreshed at least once and has not been closed yet.
186         * @return whether the context is still active
187         * @see #refresh()
188         * @see #close()
189         * @see #getBeanFactory()
190         */
191        boolean isActive();
192
193        /**
194         * Return the internal bean factory of this application context.
195         * Can be used to access specific functionality of the underlying factory.
196         * <p>Note: Do not use this to post-process the bean factory; singletons
197         * will already have been instantiated before. Use a BeanFactoryPostProcessor
198         * to intercept the BeanFactory setup process before beans get touched.
199         * <p>Generally, this internal factory will only be accessible while the context
200         * is active, that is, in-between {@link #refresh()} and {@link #close()}.
201         * The {@link #isActive()} flag can be used to check whether the context
202         * is in an appropriate state.
203         * @return the underlying bean factory
204         * @throws IllegalStateException if the context does not hold an internal
205         * bean factory (usually if {@link #refresh()} hasn't been called yet or
206         * if {@link #close()} has already been called)
207         * @see #isActive()
208         * @see #refresh()
209         * @see #close()
210         * @see #addBeanFactoryPostProcessor
211         */
212        ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
213
214}