001/* 002 * Copyright 2002-2018 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.support; 018 019import java.lang.reflect.Constructor; 020import java.lang.reflect.Method; 021 022import org.springframework.beans.BeansException; 023import org.springframework.beans.factory.BeanFactory; 024import org.springframework.lang.Nullable; 025 026/** 027 * Interface responsible for creating instances corresponding to a root bean definition. 028 * 029 * <p>This is pulled out into a strategy as various approaches are possible, 030 * including using CGLIB to create subclasses on the fly to support Method Injection. 031 * 032 * @author Rod Johnson 033 * @author Juergen Hoeller 034 * @since 1.1 035 */ 036public interface InstantiationStrategy { 037 038 /** 039 * Return an instance of the bean with the given name in this factory. 040 * @param bd the bean definition 041 * @param beanName the name of the bean when it is created in this context. 042 * The name can be {@code null} if we are autowiring a bean which doesn't 043 * belong to the factory. 044 * @param owner the owning BeanFactory 045 * @return a bean instance for this bean definition 046 * @throws BeansException if the instantiation attempt failed 047 */ 048 Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) 049 throws BeansException; 050 051 /** 052 * Return an instance of the bean with the given name in this factory, 053 * creating it via the given constructor. 054 * @param bd the bean definition 055 * @param beanName the name of the bean when it is created in this context. 056 * The name can be {@code null} if we are autowiring a bean which doesn't 057 * belong to the factory. 058 * @param owner the owning BeanFactory 059 * @param ctor the constructor to use 060 * @param args the constructor arguments to apply 061 * @return a bean instance for this bean definition 062 * @throws BeansException if the instantiation attempt failed 063 */ 064 Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, 065 Constructor<?> ctor, Object... args) throws BeansException; 066 067 /** 068 * Return an instance of the bean with the given name in this factory, 069 * creating it via the given factory method. 070 * @param bd the bean definition 071 * @param beanName the name of the bean when it is created in this context. 072 * The name can be {@code null} if we are autowiring a bean which doesn't 073 * belong to the factory. 074 * @param owner the owning BeanFactory 075 * @param factoryBean the factory bean instance to call the factory method on, 076 * or {@code null} in case of a static factory method 077 * @param factoryMethod the factory method to use 078 * @param args the factory method arguments to apply 079 * @return a bean instance for this bean definition 080 * @throws BeansException if the instantiation attempt failed 081 */ 082 Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner, 083 @Nullable Object factoryBean, Method factoryMethod, Object... args) 084 throws BeansException; 085 086}