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.pswp.cn/news/713662.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/713662.shtml
英文地址,請注明出處:http://en.pswp.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…