Spring源码 – 核心接口FactoryBean「建议收藏」

Spring源码 – 核心接口FactoryBean「建议收藏」Spring源码-核心接口FactoryBean

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

Spring源码 – 核心接口FactoryBean

Spring版本:Spring 5.3.13-release


一般情况下,Spring是通过反射机制,利用在Bean进行声明时的class属性指定的实现类来进行Bean的实例化操作。但是在某些场景下,Bean的实例化操作时非常复杂繁琐的,如果按照传统的方式,则需要在<bean></bean>中进行大量配置信息的声明,这种配置方式的灵活性会受到非常大的局限性的。而如果采用编码的方式进行声明可能会变得非常简单。org.springframework.beans.factory.FactoryBean<T>接口应运而生,用户可以实现该接口来定制实例化Bean的逻辑。

# 1、核心接口FactoryBean

FactoryBean<T>Spring占据非常重要的地位。很多第三方中间件都是通过实现FactoryBean<T>接口进行实例化。如open-feign

FactoryBean<T>接口中一共定义了三个方法:

public interface FactoryBean<T> { 
   

	/** * The name of an attribute that can be * {@link org.springframework.core.AttributeAccessor#setAttribute set} on a * {@link org.springframework.beans.factory.config.BeanDefinition} so that * factory beans can signal their object type when it can't be deduced from * the factory bean class. * @since 5.2 */
	String OBJECT_TYPE_ATTRIBUTE = "factoryBeanObjectType";


	/** * Return an instance (possibly shared or independent) of the object * managed by this factory. * <p>As with a {@link BeanFactory}, this allows support for both the * Singleton and Prototype design pattern. * <p>If this FactoryBean is not fully initialized yet at the time of * the call (for example because it is involved in a circular reference), * throw a corresponding {@link FactoryBeanNotInitializedException}. * <p>As of Spring 2.0, FactoryBeans are allowed to return {@code null} * objects. The factory will consider this as normal value to be used; it * will not throw a FactoryBeanNotInitializedException in this case anymore. * FactoryBean implementations are encouraged to throw * FactoryBeanNotInitializedException themselves now, as appropriate. * @return an instance of the bean (can be {@code null}) * @throws Exception in case of creation errors * @see FactoryBeanNotInitializedException * @see FactoryBean#isSingleton() * @see org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#singletonObjects */
	@Nullable
	T getObject() throws Exception;

	/** * Return the type of object that this FactoryBean creates, * or {@code null} if not known in advance. * <p>This allows one to check for specific types of beans without * instantiating objects, for example on autowiring. * <p>In the case of implementations that are creating a singleton object, * this method should try to avoid singleton creation as far as possible; * it should rather estimate the type in advance. * For prototypes, returning a meaningful type here is advisable too. * <p>This method can be called <i>before</i> this FactoryBean has * been fully initialized. It must not rely on state created during * initialization; of course, it can still use such state if available. * <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return * {@code null} here. Therefore it is highly recommended to implement * this method properly, using the current state of the FactoryBean. * @return the type of object that this FactoryBean creates, * or {@code null} if not known at the time of the call * @see ListableBeanFactory#getBeansOfType */
	@Nullable
	Class<?> getObjectType();

	/** * Is the object managed by this factory a singleton? That is, * will {@link #getObject()} always return the same object * (a reference that can be cached)? * <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object, * the object returned from {@code getObject()} might get cached * by the owning BeanFactory. Hence, do not return {@code true} * unless the FactoryBean always exposes the same reference. * <p>The singleton status of the FactoryBean itself will generally * be provided by the owning BeanFactory; usually, it has to be * defined as singleton there. * <p><b>NOTE:</b> This method returning {@code false} does not * necessarily indicate that returned objects are independent instances. * An implementation of the extended {@link SmartFactoryBean} interface * may explicitly indicate independent instances through its * {@link SmartFactoryBean#isPrototype()} method. Plain {@link FactoryBean} * implementations which do not implement this extended interface are * simply assumed to always return independent instances if the * {@code isSingleton()} implementation returns {@code false}. * <p>The default implementation returns {@code true}, since a * {@code FactoryBean} typically manages a singleton instance. * @return whether the exposed object is a singleton * @see #getObject() * @see SmartFactoryBean#isPrototype() * * 返回由 FactoryBean 创建的 Bean 实例的作用域是单例还是原型 * true -> singleton / false -> prototype */
	default boolean isSingleton() { 
   
		return true;
	}

}
  • getObject():返回由FactoryBean创建的Bean实例,如果isSingleton()返回true,则该Bean实例会放入Spring的一级缓存中。
  • getObjectType():返回FactoryBean创建的Bean类型。
  • isSingleton():返回由FactoryBean创建的Bean实例的作用域是单例还是原型。

当声明Bean时指定的class属性为FactoryBean<T>接口的实现类时,Spring容器通过getBean()方法获取的Bean实例为T,即为getObject()方法返回的实例对象。也就是说此时FactoryBean<T>#getObject()方法代理了getBean()方法。如果需要获取此FactoryBean<T>实例,则需要通过&+BeanName进行获取。

# 2、BeanFactoryFactoryBean<T>

需要注意的是,BeanFactoryFactoryBean<T>是两个完全不同概念的接口。BeanFactory根据命名可以看出,它是一个工厂。而FactoryBean<T>根据命名也可以很清晰的看出,它是一个BeanBeanFactorySpring对于IOC思想实现的最基本方式,它提供了对Bean的管理。而FactoryBean<T>Spring针对复杂类型Bean的实例化,允许开发者对此类型的Bean进行的高级定制处理。

GitHub源码地址https://github.com/kapbc/kapcb-spring-source/tree/master/Spring-Framework-v5.3.13

备注:此文为笔者学习Spring源码的笔记,鉴于本人技术有限,文中难免出现一些错误,感谢大家批评指正。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/170037.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • BatchNorm 理解

    BatchNorm 理解BN可以说是NN发展中的一个里程碑式的结构了,不增加inference时间,调参变得简单,收敛更快效果更好。虽然提出的时间已经很久了,而且网上关于BN的解释一堆一堆的,但是博主觉得有不少解释是欠妥的,在此贴出博主贴出对caffe中BN源码的解释和自己对BN的理解,欢迎讨论。caffe中BN的实现比较反人类。BatchNorm层单纯实现标准化,再用一个scale层添加  参数,共同完成BN。…

    2022年5月22日
    39
  • maven学习系列——(一)maven简介[通俗易懂]

    这个系列学习maven,主要是看maven实战和其他网站上整理出自己一些知识点,方便自己以后查找和使用! 这个系列的我先根据自己在公司经常使用到的一些知识点进行整理,后期在做完善! 计划:要在2017 年之前学习和整理完成!

    2022年2月25日
    45
  • RapeLay(电车之狼R)的结局介绍 (隐藏结局攻略)

    RapeLay(电车之狼R)的结局介绍 (隐藏结局攻略)

    2021年11月28日
    92
  • java 多态[通俗易懂]

    java 多态[通俗易懂]多态的情况下,子父类存在同名的成员变量或成员方法优先调用问题1.多态的情况下,子父类存在同名的成员变量时,默认访问的是父类的成员变量数据.2.多态的情况下,子父类存在同名的非静态函数的时候,默认是调用子类的成员函数.3.多态的情况下,子父类存在同名的静态函数时,默认是调用父类的成员函数.原因:java多态的实现,首先说成员变量,因为在java中,一个对象实例是存储在堆中的,而这个对象包含的内容有对象头,对象体以及对其字节,首先对象头存放的是对象运行时的数据,像是hashcode,锁标识,类型指针,

    2022年7月7日
    22
  • 一维数组二分法查找_二分查找算法c语言

    一维数组二分法查找_二分查找算法c语言在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。示例:现有矩阵 matrix 如下:[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23, 26, 30]]给定 target = 5,返回

    2022年8月9日
    3
  • linux查看网卡实时速率命令_linux查看哪个网卡是活跃的

    linux查看网卡实时速率命令_linux查看哪个网卡是活跃的[root@hadoop058~]#mii-tooleth0:negotiated100baseTx-FD,linkok100Mlinux下查看网卡工作速率Ethtool是用于查询及设置网卡参数的命令。概要:ethtoolethX//查询ethX网口基本设置ethtool–h//显示ethtool的命令帮助(help)ethtool–iethX//查询ethX网口的相…

    2022年10月19日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号