亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring容器刷新prepareRefresh第一步

 更新時(shí)間:2023年03月19日 15:11:00   作者:今年三歲半  
這篇文章主要為大家介紹了Spring容器刷新prepareRefresh第一步示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

關(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

  • 成員變量 activetrue 表示當(dāng)前 context 處于激活狀態(tài)
  • 成員變量 closedtrue 表示當(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ì)將 ServletContextPropertySourceServletConfigPropertySource 加入(替換 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)

    這篇文章給大家分享java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn),需要獲取要賦值給二維碼的鏈接后綴,通過(guò)設(shè)置二維碼的訪問(wèn)路徑等一系列操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧
    2021-06-06
  • Java8新特性之Optional使用詳解

    Java8新特性之Optional使用詳解

    這篇文章主要介紹了Java8新特性之Optional使用詳解,為了解決空指針異常更加優(yōu)雅,Java8?提供了?Optional?類庫(kù),Optional?實(shí)際上是個(gè)容器,它可以保存類型T的值,或者僅僅保存null,,需要的朋友可以參考下
    2023-08-08
  • IDEA自動(dòng)清理類中未使用的import包的操作方法

    IDEA自動(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
  • Windows10安裝IDEA 2020.1.2的方法步驟

    Windows10安裝IDEA 2020.1.2的方法步驟

    這篇文章主要介紹了Windows10安裝IDEA 2020.1.2的方法步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java設(shè)計(jì)模式中的外觀模式詳解

    Java設(shè)計(jì)模式中的外觀模式詳解

    這篇文章主要介紹了Java設(shè)計(jì)模式中的外觀模式詳解,外觀模式也叫“過(guò)程模式,外觀模式為子系統(tǒng)中的一組接口提供一個(gè)一致的界面,此模式定義了一個(gè)高層接口,這個(gè)接口使得這一子系統(tǒng)更加容易使用
    2022-07-07
  • Java System類用法實(shí)戰(zhàn)案例

    Java System類用法實(shí)戰(zhàn)案例

    這篇文章主要介紹了Java System類用法,結(jié)合具體實(shí)例形式分析了java使用System類獲取系統(tǒng)環(huán)境變量信息相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • 詳解SpringMVC中的異常處理

    詳解SpringMVC中的異常處理

    這篇文章主要介紹了SpringMVC中的異常處理的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用SpringMVC,感興趣的朋友可以了解下
    2021-03-03
  • 在java List中進(jìn)行模糊查詢的實(shí)現(xiàn)方法

    在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-11
  • java基于poi導(dǎo)出excel透視表代碼實(shí)例

    java基于poi導(dǎo)出excel透視表代碼實(shí)例

    這篇文章主要介紹了java基于poi導(dǎo)出excel透視表代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Java判斷本機(jī)IP地址類型的方法

    Java判斷本機(jī)IP地址類型的方法

    Java判斷本機(jī)IP地址類型的方法,需要的朋友可以參考一下
    2013-03-03

最新評(píng)論