1 Star 0 Fork 156

whl595145999 / Spring-Analysis

forked from huifer / Code-Analysis 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Spring-InstantiationStrategy.md 3.41 KB
一键复制 编辑 原始数据 按行查看 历史
huifer 提交于 2020-12-03 11:20 . feat: 0.0.9

Spring InstantiationStrategy

  • 类全路径: org.springframework.beans.factory.support.InstantiationStrategy

  • 类图:

    CglibSubclassingInstantiationStrategy

InstantiationStrategy 是一个接口定义了下面三种实例化Bean的方式

  1. 通过BeanFactory将对应BeanName的实例返回
  2. 通过构造函数将Bean实例返回
  3. 通过工厂函数将Bean实例返回
详细代码如下
public interface InstantiationStrategy {

   /**
    * Return an instance of the bean with the given name in this factory.
    * 从 beanFactory 中返回 对应的 BeanName 实例对象
    * @param bd the bean definition
    * @param beanName the name of the bean when it is created in this context.
    * The name can be {@code null} if we are autowiring a bean which doesn't
    * belong to the factory.
    * @param owner the owning BeanFactory
    * @return a bean instance for this bean definition
    * @throws BeansException if the instantiation attempt failed
    */
   Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner)
         throws BeansException;

   /**
    * Return an instance of the bean with the given name in this factory,
    * creating it via the given constructor.
    * 从 beanFactory 中返回 对应的 BeanName 实例对象, <b>指定构造函数</b>
    * @param bd the bean definition
    * @param beanName the name of the bean when it is created in this context.
    * The name can be {@code null} if we are autowiring a bean which doesn't
    * belong to the factory.
    * @param owner the owning BeanFactory
    * @param ctor the constructor to use
    * @param args the constructor arguments to apply
    * @return a bean instance for this bean definition
    * @throws BeansException if the instantiation attempt failed
    */
   Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
         Constructor<?> ctor, Object... args) throws BeansException;

   /**
    * Return an instance of the bean with the given name in this factory,
    * creating it via the given factory method.
    * 从 beanFactory 中返回 对应的 BeanName 实例对象, <b>通过指定的FactoryMethod</b>
    * @param bd the bean definition
    * @param beanName the name of the bean when it is created in this context.
    * The name can be {@code null} if we are autowiring a bean which doesn't
    * belong to the factory.
    * @param owner the owning BeanFactory
    * @param factoryBean the factory bean instance to call the factory method on,
    * or {@code null} in case of a static factory method
    * @param factoryMethod the factory method to use
    * @param args the factory method arguments to apply
    * @return a bean instance for this bean definition
    * @throws BeansException if the instantiation attempt failed
    */
   Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
         @Nullable Object factoryBean, Method factoryMethod, Object... args)
         throws BeansException;

}

有关子类实现请查看下面文章

Java
1
https://gitee.com/595145999/spring-analysis.git
git@gitee.com:595145999/spring-analysis.git
595145999
spring-analysis
Spring-Analysis
master

搜索帮助