Spring容器刷新prepareRefresh第一步
關(guān)鍵源碼
這次的內(nèi)容是上圖中的第1步,容器刷新前的準(zhǔn)備工作。基本上都是一些初始化動(dòng)作。
下面是這部分的涉及到的源碼中的關(guān)鍵部分:
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext { private long startupDate; /** Flag that indicates whether this context is currently active. */ private final AtomicBoolean active = new AtomicBoolean(); /** Flag that indicates whether this context has been closed already. */ private final AtomicBoolean closed = new AtomicBoolean(); /** Environment used by this context. */ @Nullable private ConfigurableEnvironment environment; protected void prepareRefresh() { // Switch to active. this.startupDate = System.currentTimeMillis(); // 1. 初始化狀態(tài)位 this.closed.set(false); this.active.set(true); if (logger.isDebugEnabled()) { if (logger.isTraceEnabled()) { logger.trace("Refreshing " + this); } else { logger.debug("Refreshing " + getDisplayName()); } } // 2. 留給子類的擴(kuò)展方法 // Initialize any placeholder property sources in the context environment. initPropertySources(); // 3. 驗(yàn)證必須的配置項(xiàng)是否存在 // Validate that all properties marked as required are resolvable: // see ConfigurablePropertyResolver#setRequiredProperties getEnvironment().validateRequiredProperties(); // 4. 處理早期事件 // Store pre-refresh ApplicationListeners... if (this.earlyApplicationListeners == null) { this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners); } else { // Reset local application listeners to pre-refresh state. this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners); } // Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available... this.earlyApplicationEvents = new LinkedHashSet<>(); } }
1.初始化狀態(tài)位
一上來(lái)就修改兩個(gè)成員變量,active 改為 true, closed 改為 false:
- 成員變量 active 為 true 表示當(dāng)前 context 處于激活狀態(tài)
- 成員變量 closed 為 true 表示當(dāng)前 context 已經(jīng)被關(guān)閉
這里修改了狀態(tài),后續(xù)有兩個(gè)地方使用。
第一個(gè)地方是容器關(guān)閉的時(shí)候(避免重復(fù)關(guān)閉)
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext { protected void doClose() { // 當(dāng)前是激活狀態(tài) && 還沒(méi)有被關(guān)閉 // Check whether an actual close attempt is necessary... if (this.active.get() && this.closed.compareAndSet(false, true)) { // 這里省略 N 行代碼 // 這里省略 N 行代碼 // Switch to inactive. this.active.set(false); } } }
第二個(gè)地方是和 BeanFactory
交互的時(shí)候作斷言用的
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext { protected void assertBeanFactoryActive() { if (!this.active.get()) { if (this.closed.get()) { throw new IllegalStateException(getDisplayName() + " has been closed already"); } else { throw new IllegalStateException(getDisplayName() + " has not been refreshed yet"); } } } }
幾乎所有和 BeanFactory
交互的方法都需要調(diào)用 assertBeanFactoryActive
方法來(lái)檢測(cè)容器的狀態(tài)。AbstractApplicationContext
中有二三十個(gè)地方使用了該方法。
比如最常見(jiàn)的各種重載的 AbstractApplicationContext.getBean(java.lang.String)
方法都會(huì)在將方法調(diào)用委托給 getBeanFactory().getBean(name, args);
之前調(diào)用 assertBeanFactoryActive()
來(lái)檢測(cè)容器狀態(tài);畢竟在一個(gè)已經(jīng)關(guān)閉了的容器上 getBean()
是不正常的吧。
2.initPropertySources
這個(gè)方法主要是留給子類用來(lái)將 StubPropertySource
(占位符) 替換為真實(shí)的 PropertySource
。
比如在 servlet 環(huán)境下,會(huì)將 ServletContextPropertySource
和 ServletConfigPropertySource
加入(替換 Stub)到 Environment
中。
public abstract class AbstractRefreshableWebApplicationContext extends AbstractRefreshableConfigApplicationContext implements ConfigurableWebApplicationContext, ThemeSource { @Override protected void initPropertySources() { ConfigurableEnvironment env = getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { // 這里實(shí)際上是調(diào)用了 WebApplicationContextUtils#initServletPropertySources ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig); } } } public abstract class WebApplicationContextUtils { public static void initServletPropertySources(MutablePropertySources sources, @Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) { Assert.notNull(sources, "'propertySources' must not be null"); String name = StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME; // servletContextInitParams if (servletContext != null && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletContextPropertySource(name, servletContext)); } name = StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME; // servletConfigInitParams if (servletConfig != null && sources.get(name) instanceof StubPropertySource) { sources.replace(name, new ServletConfigPropertySource(name, servletConfig)); } } }
當(dāng)然,你可以在這里直接 修改/替換 Environment
中的任何 PropertySource
。
也就是說(shuō),可以在這里做類似于 spring-boot 中提供的 EnvironmentPostProcessor
能做的事情。
如果是 spring-boot 項(xiàng)目的話,還是推薦直接使用 EnvironmentPostProcessor
。 而不是像下面這樣再搞一個(gè) ApplicationContext
的實(shí)現(xiàn)類。
public class PrepareRefreshTest { /** * 重寫(xiě) initPropertySources(),給 Environment 中新增兩個(gè)自定義的配置項(xiàng) "osName" 和 "a.b.c.d" */ @Test void initPropertySourcesTest() { final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PrepareRefreshTest.class) { @Override protected void initPropertySources() { super.initPropertySources(); final ConfigurableEnvironment environment = getEnvironment(); final Map<String, Object> config = new HashMap<>(); config.put("osName", System.getProperty("os.name", "UNKNOWN")); config.put("a.b.c.d", "haha"); environment.getPropertySources().addFirst(new MapPropertySource("demo-property-source", config)); } }; final Environment environment = applicationContext.getEnvironment(); Assertions.assertEquals(System.getProperty("os.name"), environment.getProperty("osName")); Assertions.assertEquals("haha", environment.getProperty("a.b.c.d")); } }
3.validateRequiredProperties
這里主要是驗(yàn)證 ConfigurablePropertyResolver.setRequiredProperties(String... requiredProperties)
方法中指定的那些 必須出現(xiàn)的配置項(xiàng) 是不是都已經(jīng)在 Environment 中了。
所謂的驗(yàn)證,邏輯也很簡(jiǎn)單:所有指定的配置項(xiàng)名稱都遍歷一遍,如果發(fā)現(xiàn) Environment 中獲取不到對(duì)應(yīng)的配置項(xiàng)就直接拋出 MissingRequiredPropertiesException
public abstract class AbstractPropertyResolver implements ConfigurablePropertyResolver { @Override public void validateRequiredProperties() { MissingRequiredPropertiesException ex = new MissingRequiredPropertiesException(); for (String key : this.requiredProperties) { if (this.getProperty(key) == null) { ex.addMissingRequiredProperty(key); } } if (!ex.getMissingRequiredProperties().isEmpty()) { throw ex; } } }
下面這段代碼是驗(yàn)證 validateRequiredProperties()
方法的(同樣的功能,可以使用 spring-boot 提供的 EnvironmentPostProcessor
來(lái)完成)。
public class PrepareRefreshTest { /** * 驗(yàn)證 getEnvironment().validateRequiredProperties(); 的功能 * <p> * 拋出 MissingRequiredPropertiesException 異常(Environment 中缺少必須出現(xiàn)的配置項(xiàng)"jdbc.url") */ @Test void validateRequiredPropertiesTest() { Assertions.assertThrows(MissingRequiredPropertiesException.class, () -> { final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(PrepareRefreshTest.class) { @Override protected void initPropertySources() { super.initPropertySources(); // 這里指定 Environment 中必須要有一個(gè)名為 "jdbc.url" 的配置項(xiàng) // 如果 Environment 中沒(méi)有名為 "jdbc.url" 的配置項(xiàng), 就會(huì)在 validateRequiredProperties() 方法中拋出 MissingRequiredPropertiesException getEnvironment().setRequiredProperties("jdbc.url"); } }; } ); } }
4.處理早期事件
什么叫做早期(early)事件?
spring 中的事件最終是委托給 ApplicationEventMulticaster
(多播器) 發(fā)布的。 但現(xiàn)在是在 prepareRefresh 階段,多播器 實(shí)例還沒(méi)有初始化呢。 這時(shí)候要是有事件的話,就只能先將這種 "早期"事件保存下來(lái),等到多播器初始化好之后再回過(guò)頭來(lái)發(fā)布這種"早期"事件。
處理早期事件 這一步所作的事情就是 初始化 用來(lái) 臨時(shí) 保存 "早期" 事件的兩個(gè)集合:
earlyApplicationEvents
: 早期事件earlyApplicationListeners
: 早期事件監(jiān)聽(tīng)器
等到后續(xù)的 initApplicationEventMulticaster()
之后會(huì)回過(guò)頭來(lái)遍歷 earlyApplicationEvents
發(fā)布事件。
詳細(xì)內(nèi)容會(huì)在 步驟8-initApplicationEventMulticaster() 和 步驟10-registerListeners() 相關(guān)的文章中介紹。這里只介紹和 prepareRefresh
相關(guān)的內(nèi)容。
public abstract class AbstractApplicationContext extends DefaultResourceLoader implements ConfigurableApplicationContext { private final Set<ApplicationListener<?>> applicationListeners = new LinkedHashSet<>(); /** Local listeners registered before refresh. */ @Nullable private Set<ApplicationListener<?>> earlyApplicationListeners; /** ApplicationEvents published before the multicaster setup. */ @Nullable private Set<ApplicationEvent> earlyApplicationEvents; protected void prepareRefresh() { // Switch to active. // 1. 初始化狀態(tài)位 // ... // 2. 留給子類的擴(kuò)展方法 // ... // 3. 驗(yàn)證必須的配置項(xiàng)是否存在 // ... // 4. 處理早期事件 // Store pre-refresh ApplicationListeners... if (this.earlyApplicationListeners == null) { this.earlyApplicationListeners = new LinkedHashSet<>(this.applicationListeners); } else { // Reset local application listeners to pre-refresh state. this.applicationListeners.clear(); this.applicationListeners.addAll(this.earlyApplicationListeners); } // Allow for the collection of early ApplicationEvents, // to be published once the multicaster is available... this.earlyApplicationEvents = new LinkedHashSet<>(); } }
以上就是Spring容器刷新prepareRefresh第一步的詳細(xì)內(nèi)容,更多關(guān)于Spring容器刷新的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)
這篇文章給大家分享java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn),需要獲取要賦值給二維碼的鏈接后綴,通過(guò)設(shè)置二維碼的訪問(wèn)路徑等一系列操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧2021-06-06IDEA自動(dòng)清理類中未使用的import包的操作方法
在項(xiàng)目開(kāi)發(fā)中,經(jīng)常會(huì)引入很多未使用的import包,這不僅增加了編譯時(shí)間,還會(huì)使代碼可讀性變差,設(shè)置IDEA自動(dòng)清理未使用的import包,可以提高代碼的可讀性,本文給大家介紹IDEA自動(dòng)清理類中未使用的import包的方法,感興趣的朋友一起看看吧2024-09-09在java List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇在java List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11java基于poi導(dǎo)出excel透視表代碼實(shí)例
這篇文章主要介紹了java基于poi導(dǎo)出excel透視表代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12