View Javadoc
1   /*
2    * Copyright 2002-2014 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.support;
18  
19  import java.lang.reflect.Constructor;
20  import java.lang.reflect.Method;
21  
22  import org.springframework.beans.BeansException;
23  import org.springframework.beans.factory.BeanFactory;
24  
25  /**
26   * Interface responsible for creating instances corresponding to a root bean definition.
27   *
28   * <p>This is pulled out into a strategy as various approaches are possible,
29   * including using CGLIB to create subclasses on the fly to support Method Injection.
30   *
31   * @author Rod Johnson
32   * @author Juergen Hoeller
33   * @since 1.1
34   */
35  public interface InstantiationStrategy {
36  
37  	/**
38  	 * Return an instance of the bean with the given name in this factory.
39  	 * @param bd the bean definition
40  	 * @param beanName the name of the bean when it's created in this context.
41  	 * The name can be {@code null} if we're autowiring a bean which doesn't
42  	 * belong to the factory.
43  	 * @param owner the owning BeanFactory
44  	 * @return a bean instance for this bean definition
45  	 * @throws BeansException if the instantiation attempt failed
46  	 */
47  	Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner)
48  			throws BeansException;
49  
50  	/**
51  	 * Return an instance of the bean with the given name in this factory,
52  	 * creating it via the given constructor.
53  	 * @param bd the bean definition
54  	 * @param beanName the name of the bean when it's created in this context.
55  	 * The name can be {@code null} if we're autowiring a bean which doesn't
56  	 * belong to the factory.
57  	 * @param owner the owning BeanFactory
58  	 * @param ctor the constructor to use
59  	 * @param args the constructor arguments to apply
60  	 * @return a bean instance for this bean definition
61  	 * @throws BeansException if the instantiation attempt failed
62  	 */
63  	Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,
64  			Constructor<?> ctor, Object... args) throws BeansException;
65  
66  	/**
67  	 * Return an instance of the bean with the given name in this factory,
68  	 * creating it via the given factory method.
69  	 * @param bd the bean definition
70  	 * @param beanName the name of the bean when it's created in this context.
71  	 * The name can be {@code null} if we're autowiring a bean which doesn't
72  	 * belong to the factory.
73  	 * @param owner the owning BeanFactory
74  	 * @param factoryBean the factory bean instance to call the factory method on,
75  	 * or {@code null} in case of a static factory method
76  	 * @param factoryMethod the factory method to use
77  	 * @param args the factory method arguments to apply
78  	 * @return a bean instance for this bean definition
79  	 * @throws BeansException if the instantiation attempt failed
80  	 */
81  	Object instantiate(RootBeanDefinition bd, String beanName, BeanFactory owner,
82  			Object factoryBean, Method factoryMethod, Object... args) throws BeansException;
83  
84  }