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源码的笔记,鉴于本人技术有限,文中难免出现一些错误,感谢大家批评指正。

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • bios设置pcie通道_PCI LAN

    bios设置pcie通道_PCI LANPCIewithlspciPCIeWidth#lspci-s81:00.0-vvv|grepWidthLnkCap:Port#0,Speed8GT/s,Widthx16,ASPMnotsupported,ExitLatencyL0sunlimited,L1unlimited…

  • SqlTransaction——事务详解[通俗易懂]

    SqlTransaction——事务详解[通俗易懂]Postedon2008-07-2001:46停留的风http://www.cnblogs.com/yank/archive/2008/07/20/1246896.html事务处理基本原理           事务是将一系列操作作为一个单元执行,要么成功,要么失败,回滚到最初状态。在事务处理术语中,事务要么提交,要么中止。若要提交事务,所有参与者都必须保证对数据

  • 深信服SCSA认证知识点(2)[通俗易懂]

    深信服SCSA认证知识点(2)[通俗易懂]深信服SCSA认证1、IP数据报文在网络层选路时,是基于最长匹配的原则。2、SNMP依赖于IP协议工作的。3、IP报文头部中有一个TTL字段,该字段长度为7位。4、access端口只能发送untagged帧5、关于rip协议,路由器不可能转发条数为16的路由器条目给它的直连邻居。6、如果希望一台DHCP客户机总是获得一个固定的IP地址,需要在DHCP上为其设置IP作用域。7、关于HTTP相应状态码302为网页重定向。8、nslookup是Windows系统常用的dns测.

  • debounce实现 js_javascript防抖函数debounce详解「建议收藏」

    debounce实现 js_javascript防抖函数debounce详解「建议收藏」定义及解读防抖函数debounce指的是某个函数在某段时间内,无论触发了多少次回调,都只执行最后一次。假如我们设置了一个等待时间3秒的函数,在这3秒内如果遇到函数调用请求就重新计时3秒,直至新的3秒内没有函数调用请求,此时执行函数,不然就以此类推重新计时。举一个小例子:假定在做公交车时,司机需等待最后一个人进入后再关门,每次新进一个人,司机就会把计时器清零并重新开始计时,重新等…

  • java json字符串转list集合

    java json字符串转list集合StringjsonString=”[{“plateNumber”:”1″,”holeNumber”:”A02″,”qcType”:”L”,”value”:”2.36″}”,{“plateNumber”:”1″,”holeNumber”:”A03″,”qcType”:”M”,”value”:”5.36″}]List<QcPlate>listQcPlate=newArr…

  • (5)Spring WebFlux快速上手——响应式Spring的道法术器「建议收藏」

    本系列其他文章见:《响应式Spring的道法术器》。前情提要:响应式流|lambda与函数式|Reactor快速上手1.3.3SpringWebFluxSpringWebFlux是随Spring5推出的响应式Web框架。1)服务端技术栈Spring提供了完整的支持响应式的服务端技术栈。如上图所示,左侧为基于spring-webmvc的技…

发表回复

您的电子邮箱地址不会被公开。

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