ConfigurableBeanFactory学习

简介

ConfigurableBeanFactory定义BeanFactory的配置。ConfigurableBeanFactory中定义了太多太多的api,比如类加载器,类型转化,属性编辑器,BeanPostProcessor,作用域,bean定义,处理bean依赖关系,合并其他ConfigurableBeanFactory,bean如何销毁。ConfigurableBeanFactory同时继承了HierarchicalBeanFactory 和 SingletonBeanRegistry 这两个接口,即同时继承了分层和单例类注册的功能。

具体:

  1. 2个静态不可变常量分别代表单例类和原型类。
  2. 1个设置父工厂的方法,跟HierarchicalBeanFactory接口的getParentBeanFactory方法互补。
  3. 4个跟类加载器有关的方法:get/set工厂类加载器和get/set临时类加载器。
  4. 2个设置、是否缓存元数据的方法(热加载开关)。
  5. 11个处理Bean注册、加载等细节的方法,包括:Bean表达式分解器、转换服务、属性编辑登记员、属性编辑器、属性编辑注册器、类型转换器、嵌入式的字符串分解器
  6. 2个处理Bean后处理器的方法。
  7. 3个跟注册范围相关的方法。
  8. 1个返回安全访问上下文的方法、1个从其他的工厂复制相关的所有配置的方法。
  9. 2个跟Bean别名相关的方法、1个返回合并后的Bean定义的方法。
  10. 1个判断是否为工厂Bean的方法、2个跟当前Bean创建时机相关的方法。
  11. 3个跟Bean依赖相关的方法、3个销毁Bean相关的方法。

以下是 ConfigurableBeanFactory 接口中一些重要的方法:

  • setAllowCircularReferences(boolean allowCircularReferences):
    设置是否允许循环引用。如果设置为 false,Spring 容器在检测到循环引用时会抛出异常。
  • isAllowCircularReferences():
    返回当前是否允许循环引用。
  • setDependencyCheck(String dependencyCheck):
    设置依赖检查的类型。这可以是 NONE、OBJECTS、SIMPLE、ALL 或 PROTOTYPE 中的一个。这些值决定了 Spring 在创建 bean 时应如何检查其依赖。
  • getDependencyCheck():返回当前的依赖检查类型。
  • setAutowireMode(int autowireMode):
    设置自动装配的模式。这可以是 AUTOWIRE_NO、AUTOWIRE_BY_NAME、AUTOWIRE_BY_TYPE 或 AUTOWIRE_CONSTRUCTOR 中的一个。
  • getAutowireMode():
    返回当前的自动装配模式。
  • setAutowireCandidate(Class<?> beanClass, boolean autowireCandidate):
    设置指定类的实例是否应作为自动装配的候选者。
  • isAutowireCandidate(Class<?> beanClass):
    返回指定类的实例是否是自动装配的候选者。
  • setIgnoreDependencyInterface(boolean ignoreDependencyInterface):
    设置是否应忽略依赖接口。如果设置为 true,则不会将接口作为依赖注入的候选者。
  • isIgnoreDependencyInterface():
    返回当前是否忽略依赖接口。
  • registerSingleton(String beanName, Object singletonObject):
    注册一个单例 bean。这通常用于在 bean 工厂中手动添加或覆盖 bean。
  • removeSingleton(String beanName):
    从 bean 工厂中移除一个单例 bean。
  • registerResolvableDependency(Class<?> dependencyType, Object key, Object value):
    注册一个可解析的依赖,这样当 bean 需要该依赖时,容器可以使用这个注册的值。
  • removeResolvableDependency(Class<?> dependencyType, Object key):
    从容器中移除一个可解析的依赖。

ConfigurableBeanFactory 通常用于更高级的 Spring 配置和自定义扩展。在大多数情况下,开发者不需要直接与此接口交互,因为 Spring 的 XmlBeanFactory、DefaultListableBeanFactory 等实现类已经提供了这些功能。然而,在创建自定义的 BeanFactory 实现或需要更细粒度的控制时,这个接口会很有用。

源码

public interface ConfigurableBeanFactory extends HierarchicalBeanFactory, SingletonBeanRegistry {/*** Scope identifier for the standard singleton scope: "singleton".* Custom scopes can be added via {@code registerScope}.* @see #registerScope*/String SCOPE_SINGLETON = "singleton";/*** Scope identifier for the standard prototype scope: "prototype".* Custom scopes can be added via {@code registerScope}.* @see #registerScope*/String SCOPE_PROTOTYPE = "prototype";/*** Set the parent of this bean factory.* <p>Note that the parent cannot be changed: It should only be set outside* a constructor if it isn't available at the time of factory instantiation.* @param parentBeanFactory the parent BeanFactory* @throws IllegalStateException if this factory is already associated with* a parent BeanFactory* @see #getParentBeanFactory()*/void setParentBeanFactory(BeanFactory parentBeanFactory) throws IllegalStateException;/*** Set the class loader to use for loading bean classes.* Default is the thread context class loader.* <p>Note that this class loader will only apply to bean definitions* that do not carry a resolved bean class yet. This is the case as of* Spring 2.0 by default: Bean definitions only carry bean class names,* to be resolved once the factory processes the bean definition.* @param beanClassLoader the class loader to use,* or {@code null} to suggest the default class loader*/void setBeanClassLoader(@Nullable ClassLoader beanClassLoader);/*** Return this factory's class loader for loading bean classes* (only {@code null} if even the system ClassLoader isn't accessible).* @see org.springframework.util.ClassUtils#forName(String, ClassLoader)*/@NullableClassLoader getBeanClassLoader();/*** Specify a temporary ClassLoader to use for type matching purposes.* Default is none, simply using the standard bean ClassLoader.* <p>A temporary ClassLoader is usually just specified if* <i>load-time weaving</i> is involved, to make sure that actual bean* classes are loaded as lazily as possible. The temporary loader is* then removed once the BeanFactory completes its bootstrap phase.* @since 2.5*/void setTempClassLoader(@Nullable ClassLoader tempClassLoader);/*** Return the temporary ClassLoader to use for type matching purposes,* if any.* @since 2.5*/@NullableClassLoader getTempClassLoader();/*** Set whether to cache bean metadata such as given bean definitions* (in merged fashion) and resolved bean classes. Default is on.* <p>Turn this flag off to enable hot-refreshing of bean definition objects* and in particular bean classes. If this flag is off, any creation of a bean* instance will re-query the bean class loader for newly resolved classes.*/void setCacheBeanMetadata(boolean cacheBeanMetadata);/*** Return whether to cache bean metadata such as given bean definitions* (in merged fashion) and resolved bean classes.*/boolean isCacheBeanMetadata();/*** Specify the resolution strategy for expressions in bean definition values.* <p>There is no expression support active in a BeanFactory by default.* An ApplicationContext will typically set a standard expression strategy* here, supporting "#{...}" expressions in a Unified EL compatible style.* @since 3.0*/void setBeanExpressionResolver(@Nullable BeanExpressionResolver resolver);/*** Return the resolution strategy for expressions in bean definition values.* @since 3.0*/@NullableBeanExpressionResolver getBeanExpressionResolver();/*** Specify a Spring 3.0 ConversionService to use for converting* property values, as an alternative to JavaBeans PropertyEditors.* @since 3.0*/void setConversionService(@Nullable ConversionService conversionService);/*** Return the associated ConversionService, if any.* @since 3.0*/@NullableConversionService getConversionService();/*** Add a PropertyEditorRegistrar to be applied to all bean creation processes.* <p>Such a registrar creates new PropertyEditor instances and registers them* on the given registry, fresh for each bean creation attempt. This avoids* the need for synchronization on custom editors; hence, it is generally* preferable to use this method instead of {@link #registerCustomEditor}.* @param registrar the PropertyEditorRegistrar to register*/void addPropertyEditorRegistrar(PropertyEditorRegistrar registrar);/*** Register the given custom property editor for all properties of the* given type. To be invoked during factory configuration.* <p>Note that this method will register a shared custom editor instance;* access to that instance will be synchronized for thread-safety. It is* generally preferable to use {@link #addPropertyEditorRegistrar} instead* of this method, to avoid for the need for synchronization on custom editors.* @param requiredType type of the property* @param propertyEditorClass the {@link PropertyEditor} class to register*/void registerCustomEditor(Class<?> requiredType, Class<? extends PropertyEditor> propertyEditorClass);/*** Initialize the given PropertyEditorRegistry with the custom editors* that have been registered with this BeanFactory.* @param registry the PropertyEditorRegistry to initialize*/void copyRegisteredEditorsTo(PropertyEditorRegistry registry);/*** Set a custom type converter that this BeanFactory should use for converting* bean property values, constructor argument values, etc.* <p>This will override the default PropertyEditor mechanism and hence make* any custom editors or custom editor registrars irrelevant.* @since 2.5* @see #addPropertyEditorRegistrar* @see #registerCustomEditor*/void setTypeConverter(TypeConverter typeConverter);/*** Obtain a type converter as used by this BeanFactory. This may be a fresh* instance for each call, since TypeConverters are usually <i>not</i> thread-safe.* <p>If the default PropertyEditor mechanism is active, the returned* TypeConverter will be aware of all custom editors that have been registered.* @since 2.5*/TypeConverter getTypeConverter();/*** Add a String resolver for embedded values such as annotation attributes.* @param valueResolver the String resolver to apply to embedded values* @since 3.0*/void addEmbeddedValueResolver(StringValueResolver valueResolver);/*** Determine whether an embedded value resolver has been registered with this* bean factory, to be applied through {@link #resolveEmbeddedValue(String)}.* @since 4.3*/boolean hasEmbeddedValueResolver();/*** Resolve the given embedded value, e.g. an annotation attribute.* @param value the value to resolve* @return the resolved value (may be the original value as-is)* @since 3.0*/@NullableString resolveEmbeddedValue(String value);/*** Add a new BeanPostProcessor that will get applied to beans created* by this factory. To be invoked during factory configuration.* <p>Note: Post-processors submitted here will be applied in the order of* registration; any ordering semantics expressed through implementing the* {@link org.springframework.core.Ordered} interface will be ignored. Note* that autodetected post-processors (e.g. as beans in an ApplicationContext)* will always be applied after programmatically registered ones.* @param beanPostProcessor the post-processor to register*/void addBeanPostProcessor(BeanPostProcessor beanPostProcessor);/*** Return the current number of registered BeanPostProcessors, if any.*/int getBeanPostProcessorCount();/*** Register the given scope, backed by the given Scope implementation.* @param scopeName the scope identifier* @param scope the backing Scope implementation*/void registerScope(String scopeName, Scope scope);/*** Return the names of all currently registered scopes.* <p>This will only return the names of explicitly registered scopes.* Built-in scopes such as "singleton" and "prototype" won't be exposed.* @return the array of scope names, or an empty array if none* @see #registerScope*/String[] getRegisteredScopeNames();/*** Return the Scope implementation for the given scope name, if any.* <p>This will only return explicitly registered scopes.* Built-in scopes such as "singleton" and "prototype" won't be exposed.* @param scopeName the name of the scope* @return the registered Scope implementation, or {@code null} if none* @see #registerScope* 获取作用域*/@NullableScope getRegisteredScope(String scopeName);/*** Provides a security access control context relevant to this factory.* @return the applicable AccessControlContext (never {@code null})* @since 3.0*/AccessControlContext getAccessControlContext();/*** Copy all relevant configuration from the given other factory.* <p>Should include all standard configuration settings as well as* BeanPostProcessors, Scopes, and factory-specific internal settings.* Should not include any metadata of actual bean definitions,* such as BeanDefinition objects and bean name aliases.* @param otherFactory the other BeanFactory to copy from* 复制配置*/void copyConfigurationFrom(ConfigurableBeanFactory otherFactory);/*** Given a bean name, create an alias. We typically use this method to* support names that are illegal within XML ids (used for bean names).* <p>Typically invoked during factory configuration, but can also be* used for runtime registration of aliases. Therefore, a factory* implementation should synchronize alias access.* @param beanName the canonical name of the target bean* @param alias the alias to be registered for the bean* @throws BeanDefinitionStoreException if the alias is already in use* 注册bean的别名*/void registerAlias(String beanName, String alias) throws BeanDefinitionStoreException;/*** Resolve all alias target names and aliases registered in this* factory, applying the given StringValueResolver to them.* <p>The value resolver may for example resolve placeholders* in target bean names and even in alias names.* @param valueResolver the StringValueResolver to apply* @since 2.5* 解析bean的别名*/void resolveAliases(StringValueResolver valueResolver);/*** Return a merged BeanDefinition for the given bean name,* merging a child bean definition with its parent if necessary.* Considers bean definitions in ancestor factories as well.* @param beanName the name of the bean to retrieve the merged definition for* @return a (potentially merged) BeanDefinition for the given bean* @throws NoSuchBeanDefinitionException if there is no bean definition with the given name* @since 2.5* 根据beanName获取bean的定义信息*/BeanDefinition getMergedBeanDefinition(String beanName) throws NoSuchBeanDefinitionException;/*** Determine whether the bean with the given name is a FactoryBean.* @param name the name of the bean to check* @return whether the bean is a FactoryBean* ({@code false} means the bean exists but is not a FactoryBean)* @throws NoSuchBeanDefinitionException if there is no bean with the given name* @since 2.5* 判断该name的bean的是否是FactoryBean类型*/boolean isFactoryBean(String name) throws NoSuchBeanDefinitionException;/*** Explicitly control the current in-creation status of the specified bean.* For container-internal use only.* @param beanName the name of the bean* @param inCreation whether the bean is currently in creation* @since 3.1* inCreation为true则从inCreationCheckExclusions集合中移除该bean* inCreation为false则将beanName添加到inCreationCheckExclusions集合中*/void setCurrentlyInCreation(String beanName, boolean inCreation);/*** Determine whether the specified bean is currently in creation.* @param beanName the name of the bean* @return whether the bean is currently in creation* @since 2.5* 根据beanName判断该bean是否正在创建中*/boolean isCurrentlyInCreation(String beanName);/*** Register a dependent bean for the given bean,* to be destroyed before the given bean is destroyed.* @param beanName the name of the bean* @param dependentBeanName the name of the dependent bean* @since 2.5* 根据beanName和该bean依赖的BeanName注册依赖bean*/void registerDependentBean(String beanName, String dependentBeanName);/*** Return the names of all beans which depend on the specified bean, if any.* @param beanName the name of the bean* @return the array of dependent bean names, or an empty array if none* @since 2.5* 根据benaName获取该bean的所有依赖*/String[] getDependentBeans(String beanName);/*** Return the names of all beans that the specified bean depends on, if any.* @param beanName the name of the bean* @return the array of names of beans which the bean depends on,* or an empty array if none* @since 2.5* 根据beanName获取该bean所有依赖*/String[] getDependenciesForBean(String beanName);/*** Destroy the given bean instance (usually a prototype instance* obtained from this factory) according to its bean definition.* <p>Any exception that arises during destruction should be caught* and logged instead of propagated to the caller of this method.* @param beanName the name of the bean definition* @param beanInstance the bean instance to destroy* 根据beanName和bean实例销毁bean*/void destroyBean(String beanName, Object beanInstance);/*** Destroy the specified scoped bean in the current target scope, if any.* <p>Any exception that arises during destruction should be caught* and logged instead of propagated to the caller of this method.* @param beanName the name of the scoped bean* 根据beanName销毁bean*/void destroyScopedBean(String beanName);/*** Destroy all singleton beans in this factory, including inner beans that have* been registered as disposable. To be called on shutdown of a factory.* <p>Any exception that arises during destruction should be caught* and logged instead of propagated to the caller of this method.* 销毁所有的单例bean*/void destroySingletons();}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/713662.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

微软为金融界带来革命性突破——推出Microsoft 365中的下一代AI助手:Microsoft Copilot for Finance

每周跟踪AI热点新闻动向和震撼发展 想要探索生成式人工智能的前沿进展吗&#xff1f;订阅我们的简报&#xff0c;深入解析最新的技术突破、实际应用案例和未来的趋势。与全球数同行一同&#xff0c;从行业内部的深度分析和实用指南中受益。不要错过这个机会&#xff0c;成为AI领…

雷龙CS SD NAND(贴片式TF卡)测评体验

前段时间有幸免费得到了雷龙出品的贴片式的TF卡的芯片及转接板&#xff0c;两片贴片式nand芯片&#xff0b;一个转接板&#xff0c;一种一个已让官方焊接完好&#xff1b;如下图所示&#xff1a; 正面&#xff1a; 背面&#xff1a; 通过转接板&#xff0c;可以将CS SD NAND(贴…

数电实验之流水灯、序列发生器

最近又用到了数电实验设计的一些操作和设计思想&#xff0c;遂整理之。 广告流水灯 实验内容 用触发器、组合函数器件和门电路设计一个广告流水灯&#xff0c;该流水灯由 8 个 LED 组成&#xff0c;工作时始终为 1 暗 7 亮&#xff0c;且这一个暗灯循环右移。 1) 写出设计过…

关于DisableIEToEdge插件闪退问题的解决方案

关于DisableIEToEdge插件闪退问题.今天终于测试找到最佳解决方案了&#xff01; 1.管理员权限运行Windows powershell. 2.执行一下两条命令修复系统环境 DISM.exe /Online /Cleanup-image /Restorehealth sfc /scannow 3.关闭Windows安全中心的所有安全选项。 4.管理员权限运行…

【计算机考研择校】四川大学vs电子科技大学哪个难度更大?

川大在文科&#xff0c;经管方面比科大强&#xff0c;医学在国内都很强。但工科方面特别是电子信息领域明显是科大强于川大。毕竟一个是综合大学&#xff0c;一个是工科大学不可同日而语。 就业上&#xff0c;电子科大在IT领域的社会声誉口碑不错。就业一向都很好。这个多问问…

.datastore@cyberfear.com.mkp勒索病毒的最新威胁:如何恢复您的数据?

导言&#xff1a; 我们享受着数字化带来的便利&#xff0c;但同时也要面对不断演进的网络威胁。最近出现的 .datastorecyberfear.com.mkp、[hendersoncock.li].mkp [hudsonLcock.li]、.mkp [myersairmail.cc].mkp 勒索病毒就是其中之一&#xff0c;它对我们的数据安全构成了…

张俊将出席用磁悬浮技术改变生活演讲

演讲嘉宾&#xff1a;张俊 空压机销售总监 亿昇(天津)科技有限公司 演讲题目&#xff1a;用磁悬浮技术改变生活 会议简介 “十四五”规划中提出&#xff0c;提高工业、能源领城智能化与信息化融合&#xff0c;明确“低碳经济”新的战略目标&#xff0c;热能产业是能源产业和…

Python环境下一种改进的基于梯度下降的自适应短时傅里叶变换

在数字信号处理技术中&#xff0c;傅里叶变换及其逆变换是一种信号时频分析方法。该方法将信号的时域描述及频域描述联系在一起&#xff0c;时域信号可通过正变换转变为频域信号&#xff0c;频域信号可通过逆变换转变为时域信号进行分析。但傅里叶变换及其逆变换是一种信号的整…

Linux/Centos 部署静态IP,解决无法访问目标主机、Destination Host Unreachable、无法ping通互联网的问题

Linux/Centos 部署IP&#xff0c;解决无法访问目标主机、Destination Host Unreachable、无法ping通互联网的问题 Linux/Centos 部署静态IP查物理机/自身电脑的IP设置VMware上的虚拟网络编辑器设置网卡IP&#xff0c;激活至此就可访问百度了 Linux/Centos 部署静态IP 需要注意…

软考基础知识2

1.DMA控制方式&#xff1a;直接内存存取。数据在内存与I/O设备间直接成块传送&#xff0c;不需要CPU的任何干涉&#xff0c;由DMA硬件直接执行完成。 例题&#xff1a; 2.程序计数器总是存下一个指令的地址。 例题&#xff1a; 3.可靠度的计算&#xff1a; 例题&#xff1a…

吸猫毛空气净化器哪个好?推荐除猫毛效果好宠物空气净化器品牌

当下有越来越多的家庭选择养宠物&#xff01;尽管家里变得更加温馨&#xff0c;但养宠可能会带来异味和空气中的毛发增多可能会带来健康问题&#xff0c;这是一个大问题&#xff01; 不想家里弥漫着异味&#xff0c;特别是来自宠物便便的味道&#xff0c;所以需要一款能够处理…

大语言模型LLM Pro+中Pro+(Prompting)的意义

—— Pro &#xff0c;即Prompting&#xff0c;构造提示 1.LLM Pro中Pro&#xff08;Prompting&#xff09;的意义 Prompting不仅是大语言模型交互和调用的一种高效手段&#xff0c;而且已成为推动模型泛化能力和应用灵活性的关键技术路径&#xff0c;它不仅极大地拓展了模型功…

ABAP - SALV教程02 - 开篇:打开SALV的三种方式之二

全屏模式生成SALV的方式&#xff1a;http://t.csdnimg.cn/CzNLz本文讲解生成可控模式的SALV&#xff0c;该方式需要依赖自己创建屏幕的自定义控件区域&#xff08;Custom Control&#xff09;实现步骤&#xff1a;需要注意的点是SALV的实例对象和dispaly方法一定是在屏幕PBO事件…

利用IP地址识别风险用户:保护网络安全的重要手段

随着互联网的发展和普及&#xff0c;网络安全问题日益突出&#xff0c;各种网络诈骗、恶意攻击等风险不断涌现&#xff0c;给个人和企业的财产安全和信息安全带来了严重威胁。在这样的背景下&#xff0c;利用IP地址识别风险用户成为了保护网络安全的重要手段之一。IP数据云探讨…

Qt常用的多线程使用方式

目前(Qt5)常用的多线程的方式&#xff1f; 1、派生于QThread然后重写run()函数 2、通过将派生QObject的类对象通过moveToThread()来移动到新的线程中 3、通过inherit QRunnable类然后重写run()方法、然后借助QThreadPool线程池来实现多线程 4、通过高级语法 QtConcurrent模块来…

JVM内存回收算法

1.1 引用计数法 每个对象创建的时候&#xff0c;会分配一个引用计数器&#xff0c;当这个对象被引用的时候计数器就加1&#xff0c;当不被引用或者引用失效的时候计数器就会减1。任何时候&#xff0c;对象的引用计数器值为0就说明这个对象不被使用了&#xff0c;就认为是“垃圾…

奇舞周刊第521期:“一切非 Rust 项目均为非法”

奇舞推荐 ■ ■ ■ 拜登&#xff1a;“一切非 Rust 项目均为非法” 科技巨头要为Coding安全负责。这并不是拜登政府对内存安全语言的首次提倡。“程序员编写代码并非没有后果&#xff0c;他们的⼯作⽅式于国家利益而言至关重要。”白宫国家网络总监办公室&#xff08;ONCD&…

在idea中用模板骨架初始创建maven管理的web项目时没有src有关的目录的解决方案

一.问题如下 二.解决方法 首先关闭当前项目&#xff0c;接着修改全局设置&#xff0c;重新创建项目 在VM Options中添加"-DarchetypeCataloginternal"&#xff0c;点击ok保存 点击创建&#xff0c;如果创建成功没报错且有src&#xff0c;就ok了。 当然如果出现以下…

「媒体宣传」如何写好新闻稿?

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 写好新闻稿是媒体宣传的关键环节之一&#xff0c;下面是一些关于如何写好新闻稿的建议&#xff1a; 明确新闻稿的目的和受众&#xff1a;在写新闻稿之前&#xff0c;首先要明确新闻稿的目…

仿牛客网项目---帖子详情功能的实现

这篇文章主要讲讲帖子详情功能。其实帖子详情功能简单来说就是你点进去可以看到文章&#xff0c;这就叫帖子详情功能。那接下来我讲讲我的这个项目是如何实现这个功能的。 首先写DAO层。 Mapper public interface DiscussPostMapper {List<DiscussPost> selectDiscussPo…