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