欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Spring?@Profile注解的使用和源碼解析

 更新時間:2023年04月14日 10:07:03   作者:劉牌  
這篇文章主要將通過源碼帶大家深入了解一下Spring中@Profile注解的原理與使用,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

介紹

在之前的文章中,寫了一篇使用Spring @Profile實現(xiàn)開發(fā)環(huán)境,測試環(huán)境,生產(chǎn)環(huán)境的切換,之前的文章是使用SpringBoot項目搭建,實現(xiàn)了不同環(huán)境數(shù)據(jù)源的切換,在我們實際開發(fā)中,會分為dev,test,prod等環(huán)境,他們之間數(shù)獨立的,今天進來詳解介紹Spring @Profile的原理。

Spring注解@Profile實現(xiàn)開發(fā)環(huán)境,測試環(huán)境,生產(chǎn)環(huán)境的切換

使用

帶有@Profile的注解的bean的不會被注冊進IOC容器,需要為其設(shè)置環(huán)境變量激活,才能注冊進IOC容器,如下通過setActiveProfiles設(shè)置了dev值,那么這三個值所對應(yīng)的Bean會被注冊進IOC容器。當(dāng)然,我們在實際使用中,不會這樣去做,使用SpringBoot的話,我們一般是使用yml,在yml中配置spring.profiles.active,也可以通過配置jvm參數(shù)。

通過Environment設(shè)置profile

我們可以直接通過Environment來設(shè)置環(huán)境屬性,這是比較原生的方法。

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.getEnvironment().setActiveProfiles("dev");

通過JVM參數(shù)設(shè)置

可以通過JVM參數(shù)來設(shè)置環(huán)境變量的值,在開發(fā)中,這種方式也是使用得比較普遍。

SpringBoot通過yml進行配置

在SpringBoot項目中,我們得配置項一般都是配置在yml文件中,這樣就能和代碼分開,并且也能進行動態(tài)配置。

從上面我們看出可以通過好幾種方式進行配置,但是他們最終其實都是將環(huán)境變量設(shè)置進Environment中,這樣,spring在后續(xù)得流程里面,就能從Environment中獲取環(huán)境變量,然后進行相應(yīng)的邏輯處理。

源碼解析

BeanDefinition注冊

首先,需要注冊bean的元信息BeanDefinition,不過對于@Profile標(biāo)注的方法,如果環(huán)境變量中有對應(yīng)的變量值,那么就能注冊,沒有的話則不會進行注冊,我們來看關(guān)鍵的代碼,在ConfigurationClassBeanDefinitionReader中,有一個shouldSkip判斷,它會篩選出符合的bean,不符合條件的bean則被加入skippedBeanMethods集合中,不會被注冊。

private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) {
	ConfigurationClass configClass = beanMethod.getConfigurationClass();
	MethodMetadata metadata = beanMethod.getMetadata();
	String methodName = metadata.getMethodName();
		// Do we need to mark the bean as skipped by its condition?
	if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) {
            configClass.skippedBeanMethods.add(methodName);
            return;
	}
            if (configClass.skippedBeanMethods.contains(methodName)) {
            return;
	}
}

shouldSkip源碼

在shouldSkip中,會使用Condition接口,@Profile使用的是ProfileCondition,然后調(diào)用matches方法。

    public boolean shouldSkip(@Nullable AnnotatedTypeMetadata metadata, @Nullable ConfigurationCondition.ConfigurationPhase phase) {
        for (Condition condition : conditions) {
            ConfigurationCondition.ConfigurationPhase requiredPhase = null;
            if (condition instanceof ConfigurationCondition configurationCondition) {
                requiredPhase = configurationCondition.getConfigurationPhase();
            }
            if ((requiredPhase == null || requiredPhase == phase) && !condition.matches(this.context, metadata)) {
                return true;
            }
        }
        return false;
    }

ProfileCondition匹配

在ProfileCondition的matches方法中,主要就是去Environment中尋找環(huán)境變量,然后解析@Profile注解設(shè)置的value值,如果Environment中激活的配置中包含當(dāng)前的配置,包含則能為true,不包含則為false,如上通過setActiveProfiles設(shè)置Environment中激活的配置為dev,當(dāng)前傳過來的配置為dev,那么就能匹配上,就能裝配進IOC容器。

    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        MultiValueMap<String, Object> attrs = metadata.getAllAnnotationAttributes(Profile.class.getName());
        if (attrs != null) {
            for (Object value : attrs.get("value")) {
                if (context.getEnvironment().acceptsProfiles(Profiles.of((String[]) value))) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }

從源碼可以看出,其最核心的思想就是是否注冊bean的元信息BeanDefinition,因為只有注冊了BeanDefinition,后續(xù)才能為創(chuàng)建bean提供元數(shù)據(jù)支持,判斷是否注冊bean元信息,主要就是從Environment中取出profiles的值,然后和@Profile注解設(shè)置的值進行匹配,匹配得上就注冊,bean不上就不注冊。

總結(jié)

上面我們對@Profile的使用做了詳細(xì)的介紹,并對它的核心源碼進行解剖,無非就是判斷是否要注冊BeanDefinition,如果我們需要做一些環(huán)境隔離的工作,使用@Profile還是比較不錯的。

到此這篇關(guān)于詳解Spring @Profile注解的使用和源碼解析的文章就介紹到這了,更多相關(guān)Spring @Profile注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論