001/*
002 * Copyright 2002-2012 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 org.springframework.beans.BeansException;
020import org.springframework.beans.factory.Aware;
021
022/**
023 * Interface to be implemented by any object that wishes to be notified
024 * of the {@link ApplicationContext} that it runs in.
025 *
026 * <p>Implementing this interface makes sense for example when an object
027 * requires access to a set of collaborating beans. Note that configuration
028 * via bean references is preferable to implementing this interface just
029 * for bean lookup purposes.
030 *
031 * <p>This interface can also be implemented if an object needs access to file
032 * resources, i.e. wants to call {@code getResource}, wants to publish
033 * an application event, or requires access to the MessageSource. However,
034 * it is preferable to implement the more specific {@link ResourceLoaderAware},
035 * {@link ApplicationEventPublisherAware} or {@link MessageSourceAware} interface
036 * in such a specific scenario.
037 *
038 * <p>Note that file resource dependencies can also be exposed as bean properties
039 * of type {@link org.springframework.core.io.Resource}, populated via Strings
040 * with automatic type conversion by the bean factory. This removes the need
041 * for implementing any callback interface just for the purpose of accessing
042 * a specific file resource.
043 *
044 * <p>{@link org.springframework.context.support.ApplicationObjectSupport} is a
045 * convenience base class for application objects, implementing this interface.
046 *
047 * <p>For a list of all bean lifecycle methods, see the
048 * {@link org.springframework.beans.factory.BeanFactory BeanFactory javadocs}.
049 *
050 * @author Rod Johnson
051 * @author Juergen Hoeller
052 * @author Chris Beams
053 * @see ResourceLoaderAware
054 * @see ApplicationEventPublisherAware
055 * @see MessageSourceAware
056 * @see org.springframework.context.support.ApplicationObjectSupport
057 * @see org.springframework.beans.factory.BeanFactoryAware
058 */
059public interface ApplicationContextAware extends Aware {
060
061        /**
062         * Set the ApplicationContext that this object runs in.
063         * Normally this call will be used to initialize the object.
064         * <p>Invoked after population of normal bean properties but before an init callback such
065         * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()}
066         * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader},
067         * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and
068         * {@link MessageSourceAware}, if applicable.
069         * @param applicationContext the ApplicationContext object to be used by this object
070         * @throws ApplicationContextException in case of context initialization errors
071         * @throws BeansException if thrown by application context methods
072         * @see org.springframework.beans.factory.BeanInitializationException
073         */
074        void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
075
076}