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.beans.factory.access;
018
019import org.springframework.beans.BeansException;
020
021/**
022 * Defines a contract for the lookup, use, and release of a
023 * {@link org.springframework.beans.factory.BeanFactory},
024 * or a {@code BeanFactory} subclass such as an
025 * {@link org.springframework.context.ApplicationContext}.
026 *
027 * <p>Where this interface is implemented as a singleton class such as
028 * {@link SingletonBeanFactoryLocator}, the Spring team <strong>strongly</strong>
029 * suggests that it be used sparingly and with caution. By far the vast majority
030 * of the code inside an application is best written in a Dependency Injection
031 * style, where that code is served out of a
032 * {@code BeanFactory}/{@code ApplicationContext} container, and has
033 * its own dependencies supplied by the container when it is created. However,
034 * even such a singleton implementation sometimes has its use in the small glue
035 * layers of code that is sometimes needed to tie other code together. For
036 * example, third party code may try to construct new objects directly, without
037 * the ability to force it to get these objects out of a {@code BeanFactory}.
038 * If the object constructed by the third party code is just a small stub or
039 * proxy, which then uses an implementation of this class to get a
040 * {@code BeanFactory} from which it gets the real object, to which it
041 * delegates, then proper Dependency Injection has been achieved.
042 *
043 * <p>As another example, in a complex J2EE app with multiple layers, with each
044 * layer having its own {@code ApplicationContext} definition (in a
045 * hierarchy), a class like {@code SingletonBeanFactoryLocator} may be used
046 * to demand load these contexts.
047 *
048 * @author Colin Sampaleanu
049 * @see org.springframework.beans.factory.BeanFactory
050 * @see org.springframework.context.access.DefaultLocatorFactory
051 * @see org.springframework.context.ApplicationContext
052 */
053public interface BeanFactoryLocator {
054
055        /**
056         * Use the {@link org.springframework.beans.factory.BeanFactory} (or derived
057         * interface such as {@link org.springframework.context.ApplicationContext})
058         * specified by the {@code factoryKey} parameter.
059         * <p>The definition is possibly loaded/created as needed.
060         * @param factoryKey a resource name specifying which {@code BeanFactory} the
061         * {@code BeanFactoryLocator} must return for usage. The actual meaning of the
062         * resource name is specific to the implementation of {@code BeanFactoryLocator}.
063         * @return the {@code BeanFactory} instance, wrapped as a {@link BeanFactoryReference} object
064         * @throws BeansException if there is an error loading or accessing the {@code BeanFactory}
065         */
066        BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException;
067
068}