既然介紹這兩個介面,自然要知道這兩個介面在IOC整個體系中的位置和兩者之間的聯系,
ApplicationContext定了高級容器的基本規范,其實他也不是直接繼承BeanFactory基礎容器,可以看到ApplicationContext直接父介面對BeanFactory進行很多拓展其中就包括:
- 時間的注冊和發布
- 訊息決議
- 資源決議
- Bean工廠層級管理
- 監聽器
- 容器環境
通過以上拓展,我們基本可以知道高級IOC容器有哪些特點,這也是學習整個ApplicationContext容器重點了解的部分,
再比較ApplicationContext和ConfigurableApplicationContext定義的方法以及下圖層級關系,ConfigurableApplicationContext是ApplicationContext的子介面,也就包含了ApplicationContext,通過方法可以知道,ConfigurableApplicationContext重在對各種屬性的配置,而ApplicationContext介面主要各種屬性的get方法,
Spring這種將get和set分開到兩個介面的設計增大了屬性設定和獲取的靈活性,將兩者分開也更加清晰,在以后的解決方案設計中,可以參考,將配置資訊和獲取資訊分開,兩者互不干擾,在保證重要的基礎屬性不變的情況,可以按需進行拓展,其實Spring的框架設計中應用了大量的裝飾者模式,這也是高拓展點的需要

1. ApplicationContext
該類提供了高級IOC規范
其中有幾個注意點:
- 從ListableBeanFactory介面繼承來的:用于訪問應用組件的工廠方法
- 從ResourceLoader介面繼承來的:用通用的方式加載檔案資源
- 從ApplicationEventPublisher介面繼承來的:注冊和發布事件
- 從MessageSource介面繼承來的:處理訊息,支持國際化
從父應用背景關系定義的在子背景關系中將始終保持優先,
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
/**
* 回傳一個唯一的應用背景關系id
* @return the unique id of the context, or {@code null} if none
*/
@Nullable
String getId();
/**
* 回傳已經部署的該應用背景關系的名稱.
* @return a name for the deployed application, or the empty String by default
*/
String getApplicationName();
/**
* 回傳此背景關系友好的名稱---這有什么用呢?
* @return a display name for this context (never {@code null})
*/
String getDisplayName();
/**
* 回傳該背景關系第一次被加載的時間戳
* @return the timestamp (ms) when this context was first loaded
*/
long getStartupDate();
/**
*回傳父應用背景關系, 如果沒有父背景關系,該背景關系就是在背景關系層次的根
* @return the parent context, or {@code null} if there is no parent
*/
@Nullable
ApplicationContext getParent();
AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws IllegalStateException;
}
這其中對AutowireCapableBeanFactory getAutowireCapableBeanFactory()方法有疑惑,然后網上百度了一下,Spring提供了一種機制,能夠為第三方框架賦能,讓Spring去管理的Bean去裝配和填充那些沒有被SpringIOC管理的bean,也就是Spring提供了使用第三方框架的能力,能夠做到無縫的將第三方框架整合到Spring中來進行使用,Junit與Quartz借用了這種機制為自己賦能,
2. ConfigurableApplicationContext
整體結構圖

這個圖可以看出,ConfigurableApplicationContext是ApplicationContext的子類,該介面的主要任務就是配置應用程式背景關系功能,
/**
* SPI interface to be implemented by most if not all application contexts.
* Provides facilities to configure an application context in addition
* to the application context client methods in the
* {@link org.springframework.context.ApplicationContext} interface.
*
* <p>Configuration and lifecycle methods are encapsulated here to avoid
* making them obvious to ApplicationContext client code. The present
* methods should only be used by startup and shutdown code.
* ~~~~~~~~~~~~~~~~~~~~~~~~
* 中文解釋:
* SPI介面將由大多數(而不是全部)應用程式背景關系實作,
* 除了ApplicationContext介面中的ApplicationContext程式背景關系客戶端方法外,
* 還提供了配置應用程式背景關系的功能,
* 配置和生命周期方法在此處進行了封裝,以避免對ApplicationContext客戶端代碼顯而易見,
* 本方法僅應由啟動和關閉代碼使用
* ~~~~~~~~~~~~~~~~~~~~~~~~
*
* @author Juergen Hoeller
* @author Chris Beams
* @author Sam Brannen
* @since 03.11.2003
*/
具體分析
1. 硬性基礎屬性配置
除了第一個之外,其他的都是將在bean工廠中bean名稱為雙引號小寫的bean的名稱改成左邊的大寫,至于為什么要這樣設定,我還沒理解?
/**
* Any number of these characters are considered delimiters between
* multiple context config paths in a single String value.
* 在單個String值中,可以將任意數量的這些字符視為多個背景關系配置路徑之間的分隔符,
* 就是設定多個應用背景關系組態檔的的分隔符
* @see org.springframework.context.support.AbstractXmlApplicationContext#setConfigLocation
* @see org.springframework.web.context.ContextLoader#CONFIG_LOCATION_PARAM
* @see org.springframework.web.servlet.FrameworkServlet#setContextConfigLocation
*/
String CONFIG_LOCATION_DELIMITERS = ",; \t\n";
/**
* Name of the ConversionService bean in the factory.
* If none is supplied, default conversion rules apply.
* @since 3.0
* @see org.springframework.core.convert.ConversionService
*/
String CONVERSION_SERVICE_BEAN_NAME = "conversionService";
/**
* Name of the LoadTimeWeaver bean in the factory. If such a bean is supplied,
* the context will use a temporary ClassLoader for type matching, in order
* to allow the LoadTimeWeaver to process all actual bean classes.
* @since 2.5
* @see org.springframework.instrument.classloading.LoadTimeWeaver
*/
String LOAD_TIME_WEAVER_BEAN_NAME = "loadTimeWeaver";
/**
* Name of the {@link Environment} bean in the factory.
* @since 3.1
*/
String ENVIRONMENT_BEAN_NAME = "environment";
/**
* Name of the System properties bean in the factory.
* @see java.lang.System#getProperties()
*/
String SYSTEM_PROPERTIES_BEAN_NAME = "systemProperties";
/**
* Name of the System environment bean in the factory.
* @see java.lang.System#getenv()
*/
String SYSTEM_ENVIRONMENT_BEAN_NAME = "systemEnvironment";
/**
* Name of the {@link ApplicationStartup} bean in the factory.
* @since 5.3
*/
String APPLICATION_STARTUP_BEAN_NAME = "applicationStartup";
/**
* {@link Thread#getName() Name} of the {@linkplain #registerShutdownHook()
* shutdown hook} thread: {@value}.
* @since 5.2
* @see #registerShutdownHook()
*/
String SHUTDOWN_HOOK_THREAD_NAME = "SpringContextShutdownHook";
2. 設定容器id
/**
* Set the unique id of this application context.
* 給應用背景關系-容器設定唯一的id
* @since 3.0
*/
void setId(String id);
3. 設定父容器
/**
* Set the parent of this application context.
* <p>Note that the parent shouldn't be changed: It should only be set outside
* a constructor if it isn't available when an object of this class is created,
* for example in case of WebApplicationContext setup.
* 為該應用背景關系設定父級
* 請注意,不應更改父級:僅當在創建此類的物件時不可用
* (例如在WebApplicationContext安裝的情況下)時,才應在建構式外部設定父級,
* @param parent the parent context
* @see org.springframework.web.context.ConfigurableWebApplicationContext
*/
void setParent(@Nullable ApplicationContext parent);
3. 設定環境變數
/**
* Set the {@code Environment} for this application context.
* 為該應用背景關系設定環境變數
* @param environment the new environment
* @since 3.1
*/
void setEnvironment(ConfigurableEnvironment environment);
4. 獲取環境變數配置資訊
/**
* Return the {@code Environment} for this application context in configurable
* form, allowing for further customization.
* 以可配置的形式回傳此應用程式背景關系的{@code Environment},以便進行進一步的自定義
* @since 3.1
*/
@Override
ConfigurableEnvironment getEnvironment();
5. 設定應用啟動
/**
* Set the {@link ApplicationStartup} for this application context.
* <p>This allows the application context to record metrics
* during startup.
* 這允許在啟動期間應用程式背景關系記錄指標
* @param applicationStartup the new context event factory
* @since 5.3
*/
void setApplicationStartup(ApplicationStartup applicationStartup);
6. 添加后置處理器
不知道這個后置處理器的作用,在哪個后面進行使用呢?
/**
* Add a new BeanFactoryPostProcessor that will get applied to the internal
* bean factory of this application context on refresh, before any of the
* bean definitions get evaluated. To be invoked during context configuration.
* @param postProcessor the factory processor to register
*/
void addBeanFactoryPostProcessor(BeanFactoryPostProcessor postProcessor);
7. 應用監聽器
在IOC的很多地方都能見到堅挺器的身影,那監聽器到底起到什么作用呢?
/**
* Add a new ApplicationListener that will be notified on context events
* such as context refresh and context shutdown.
* <p>Note that any ApplicationListener registered here will be applied
* on refresh if the context is not active yet, or on the fly with the
* current event multicaster in case of a context that is already active.
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* 中文解釋:
* 添加一個在應用監聽器,他將在應用事件被發現,列如應用事件:重繪和關閉應用背景關系
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* @param listener the ApplicationListener to register
* @see org.springframework.context.event.ContextRefreshedEvent
* @see org.springframework.context.event.ContextClosedEvent
*/
void addApplicationListener(ApplicationListener<?> listener);
8. 設定ClassLoader
/**
* Specify the ClassLoader to load class path resources and bean classes with.
* <p>This context class loader will be passed to the internal bean factory.
* ~~~~~~~~~~~~~~~~~
* 指定ClassLoader以加載類路徑資源和Bean類,
* 該背景關系類加載器將傳遞給內部bean工廠
* ~~~~~~~~~~~~~~~~~
* @since 5.2.7
* @see org.springframework.core.io.DefaultResourceLoader#DefaultResourceLoader(ClassLoader)
* @see org.springframework.beans.factory.config.ConfigurableBeanFactory#setBeanClassLoader
*/
void setClassLoader(ClassLoader classLoader);
ClassLoader 在之前查看資源決議器中遇到過,在容器創建中少不了對組態檔的決議和類加載,這里提供了類加載設定介面,該檔案加載器還要被加載到內部bean工廠中使用,
9. 增加協議決議器
/**
* Register the given protocol resolver with this application context,
* allowing for additional resource protocols to be handled.
* <p>Any such resolver will be invoked ahead of this context's standard
* resolution rules. It may therefore also override any default rules.
* ~~~~~~~~~~~~~~~~~~~
* 想該應用背景關系注冊給定的協議決議器
* 允許處理其他協議資源
* 任何此類決議程式都將在此背景關系的標準決議規則之前呼叫,
* 因此,它也可以覆寫任何默認規則,
* ~~~~~~~~~~~~~~~~~~~
* @since 4.3
*/
void addProtocolResolver(ProtocolResolver resolver);
協議決議器我在資源決議中也看到可,除了默認的資源決議器之外,Spring還提供介面,允許用戶注冊增加其他資源決議器,大大增強了Spring框架的拓展性,
其實也就是,Spring將整體的流程定義好了,在整個流程的每個環節都為用戶提供了自定義點,增加靈活性,只要各個環節的資料結構規定好,在各個環節中,用戶可以自由發揮,這種設計理念值得借鑒,
10. 重繪容器
/**
* Load or refresh the persistent representation of the configuration, which
* might be from Java-based configuration, an XML file, a properties file, a
* relational database schema, or some other format.
* 加載或重繪配置的持久表示形式,
* 該表示形式可能來自基于Java的配置,XML檔案,屬性檔案,關系資料庫模式或其他某種格式,
* <p>As this is a startup method, it should destroy already created singletons
* if it fails, to avoid dangling resources. In other words, after invocation
* of this method, either all or no singletons at all should be instantiated.
* 因為這是一個啟動方法(在后面的創建容器方法,該方法當做容器創建方法),如果失敗了,它應該銷毀樣創
* 的單例,換句話說,在呼叫此方法之后,應實體化所有單例或根本不實體化,
* @throws BeansException if the bean factory could not be initialized
* @throws IllegalStateException if already initialized and multiple refresh
* attempts are not supported
*/
void refresh() throws BeansException, IllegalStateException;
11. 注冊關閉掛鉤
/**
* Register a shutdown hook with the JVM runtime, closing this context
* on JVM shutdown unless it has already been closed at that time.
* <p>This method can be called multiple times. Only one shutdown hook
* (at max) will be registered for each context instance.
* <p>As of Spring Framework 5.2, the {@linkplain Thread#getName() name} of
* the shutdown hook thread should be {@link #SHUTDOWN_HOOK_THREAD_NAME}.
* @see java.lang.Runtime#addShutdownHook
* @see #close()
*/
void registerShutdownHook();
12. 關閉應用背景關系
/**
* Close this application context, releasing all resources and locks that the
* implementation might hold. This includes destroying all cached singleton beans.
* <p>Note: Does <i>not</i> invoke {@code close} on a parent context;
* parent contexts have their own, independent lifecycle.
* 關閉此應用程式背景關系,釋放實作可能持有的所有資源和鎖,
* 這包括銷毀所有快取的單例bean,
* 注意:不會在父背景關系上呼叫close ; 父級背景關系具有自己的獨立生命周期,
* <p>This method can be called multiple times without side effects: Subsequent
* {@code close} calls on an already closed context will be ignored.
* 這個方法可以多次呼叫此方法而沒有副作用:在已經關閉的背景關系上進行的后續close呼叫將被忽略,
*/
@Override
void close();
13. 判斷應用上下是否關閉,是否處于活躍狀態
/**
* Determine whether this application context is active, that is,
* whether it has been refreshed at least once and has not been closed yet.
* 確定尬應用背景關系是否處于活動狀態,,即,是否至少重繪一次并且尚未關閉,
* @return whether the context is still active
* @see #refresh()
* @see #close()
* @see #getBeanFactory()
*/
boolean isActive();
14. 獲取內部bean工廠
/**
* Return the internal bean factory of this application context.
* Can be used to access specific functionality of the underlying factory.
* <p>Note: Do not use this to post-process the bean factory; singletons
* will already have been instantiated before. Use a BeanFactoryPostProcessor
* to intercept the BeanFactory setup process before beans get touched.
* <p>Generally, this internal factory will only be accessible while the context
* is active, that is, in-between {@link #refresh()} and {@link #close()}.
* The {@link #isActive()} flag can be used to check whether the context
* is in an appropriate state.
* ~~~~~~~~~~~~~~~~
* 回傳該應用背景關系的內部bean工廠
* ~~~~~~~~~~~~~~~~
* @return the underlying bean factory
* @throws IllegalStateException if the context does not hold an internal
* bean factory (usually if {@link #refresh()} hasn't been called yet or
* if {@link #close()} has already been called)
* @see #isActive()
* @see #refresh()
* @see #close()
* @see #addBeanFactoryPostProcessor
*/
ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;
問題
1. 關于一開始對某些名稱的bean名進行大寫替換,為什么要這么做呢?
2. 為什么要給應用程式背景關系設定啟動程式?
3. 容器創建中,監聽器的作用是什么呢?
參考文章
- 介紹依賴注入的發展流程
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/256766.html
標籤:其他
上一篇:2021-02-03 Fabric Contributor Meeting 個人筆記 有關Fabric Gateway RFC的概要
