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.beans.factory.access;
18
19 import org.springframework.beans.BeansException;
20
21 /**
22 * Defines a contract for the lookup, use, and release of a
23 * {@link org.springframework.beans.factory.BeanFactory},
24 * or a <code>BeanFactory</code> subclass such as an
25 * {@link org.springframework.context.ApplicationContext}.
26 *
27 * <p>Where this interface is implemented as a singleton class such as
28 * {@link SingletonBeanFactoryLocator}, the Spring team <strong>strongly</strong>
29 * suggests that it be used sparingly and with caution. By far the vast majority
30 * of the code inside an application is best written in a Dependency Injection
31 * style, where that code is served out of a
32 * <code>BeanFactory</code>/<code>ApplicationContext</code> container, and has
33 * its own dependencies supplied by the container when it is created. However,
34 * even such a singleton implementation sometimes has its use in the small glue
35 * layers of code that is sometimes needed to tie other code together. For
36 * example, third party code may try to construct new objects directly, without
37 * the ability to force it to get these objects out of a <code>BeanFactory</code>.
38 * If the object constructed by the third party code is just a small stub or
39 * proxy, which then uses an implementation of this class to get a
40 * <code>BeanFactory</code> from which it gets the real object, to which it
41 * delegates, then proper Dependency Injection has been achieved.
42 *
43 * <p>As another example, in a complex J2EE app with multiple layers, with each
44 * layer having its own <code>ApplicationContext</code> definition (in a
45 * hierarchy), a class like <code>SingletonBeanFactoryLocator</code> may be used
46 * to demand load these contexts.
47 *
48 * @author Colin Sampaleanu
49 * @see org.springframework.beans.factory.BeanFactory
50 * @see org.springframework.context.access.DefaultLocatorFactory
51 * @see org.springframework.context.ApplicationContext
52 */
53 public interface BeanFactoryLocator {
54
55 /**
56 * Use the {@link org.springframework.beans.factory.BeanFactory} (or derived
57 * interface such as {@link org.springframework.context.ApplicationContext})
58 * specified by the <code>factoryKey</code> parameter.
59 * <p>The definition is possibly loaded/created as needed.
60 * @param factoryKey a resource name specifying which <code>BeanFactory</code> the
61 * <code>BeanFactoryLocator</code> must return for usage. The actual meaning of the
62 * resource name is specific to the implementation of <code>BeanFactoryLocator</code>.
63 * @return the <code>BeanFactory</code> instance, wrapped as a {@link BeanFactoryReference} object
64 * @throws BeansException if there is an error loading or accessing the <code>BeanFactory</code>
65 */
66 BeanFactoryReference useBeanFactory(String factoryKey) throws BeansException;
67
68 }