最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring的@PropertySource注解源碼解析

 更新時間:2023年11月23日 11:00:53   作者:Super_Leng  
這篇文章主要介紹了Spring的@PropertySource注解源碼解析,就以源碼時序圖的方式,直觀的感受下@PropertySource注解在Spring源碼層面的執(zhí)行流程,需要的朋友可以參考下

一、源碼時序圖

本節(jié),就以源碼時序圖的方式,直觀的感受下@PropertySource注解在Spring源碼層面的執(zhí)行流程。

@PropertySource注解在Spring源碼層面的執(zhí)行流程如圖所示:

在這里插入圖片描述

在這里插入圖片描述

由圖可以看出,@PropertySource注解在Spring源碼層面的執(zhí)行流程會涉及到PropertySourceTest類、AnnotationConfigApplicationContext類、AbstractApplicationContext類、PostProcessorRegistrationDelegate類、ConfigurationClassPostProcessor類、ConfigurationClassParser類、PropertySourceRegistry類、PropertySourceProcessor類和DefaultPropertySourceFactory類。具體的源碼執(zhí)行細(xì)節(jié)參見源碼解析部分。

二、源碼解析

@PropertySource注解在Spring源碼層面的執(zhí)行流程,結(jié)合源碼執(zhí)行的時序圖,會理解的更加深刻。

1. 運行案例程序啟動類

在PropertySourceTest類的main()方法中調(diào)用了AnnotationConfigApplicationContext類的構(gòu)造方法,并傳入了PropertySourceConfig類的Class對象來創(chuàng)建IOC容器。

接下來,會進(jìn)入AnnotationConfigApplicationContext類的構(gòu)造方法。

注意:@PropertySource注解在Spring源碼中的執(zhí)行流程的(2)~(11)步與前面章節(jié)的@Import注解相同,這里不再贅述,直接跳到ConfigurationClassParser類的doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass, Predicatefilter)方法。

2. 解析ConfigurationClassParser類的doProcessConfigurationClass(ConfigurationClass configClass, SourceClass sourceClass, Predicatefilter)方法

protected final SourceClass doProcessConfigurationClass(
    ConfigurationClass configClass, SourceClass sourceClass, Predicate<String> filter)
    throws IOException {
    //#############省略其他代碼################
    for (AnnotationAttributes propertySource : AnnotationConfigUtils.attributesForRepeatable(
        sourceClass.getMetadata(), PropertySources.class,
        org.springframework.context.annotation.PropertySource.class)) {
        if (this.propertySourceRegistry != null) {
            this.propertySourceRegistry.processPropertySource(propertySource);
        }
    }
 //#############省略其他代碼################
}

可以看到,在ConfigurationClassParser類的doProcessConfigurationClass()方法中,遍歷獲取到的@PropertySources注解和@PropertySource注解的屬性,并且調(diào)用propertySourceRegistry對象的processPropertySource()方法解析注解屬性的值。

3. 解析PropertySourceRegistry類的processPropertySource(AnnotationAttributes propertySource)方法

void processPropertySource(AnnotationAttributes propertySource) throws IOException {
    String name = propertySource.getString("name");
    if (!StringUtils.hasLength(name)) {
        name = null;
    }
    String encoding = propertySource.getString("encoding");
    if (!StringUtils.hasLength(encoding)) {
        encoding = null;
    }
    String[] locations = propertySource.getStringArray("value");
    Assert.isTrue(locations.length > 0, "At least one @PropertySource(value) location is required");
    boolean ignoreResourceNotFound = propertySource.getBoolean("ignoreResourceNotFound");
    Class<? extends PropertySourceFactory> factoryClass = propertySource.getClass("factory");
    Class<? extends PropertySourceFactory> factorClassToUse = (factoryClass != PropertySourceFactory.class ? factoryClass : null);
    PropertySourceDescriptor descriptor = new PropertySourceDescriptor(Arrays.asList(locations), ignoreResourceNotFound, name, factorClassToUse, encoding);
    this.propertySourceProcessor.processPropertySource(descriptor);
    this.descriptors.add(descriptor);
}

可以看到,在PropertySourceRegistry類的processPropertySource()方法中,解析@PropertySource注解中的屬性后,將解析出的屬性值封裝到PropertySourceDescriptor對象中,調(diào)用propertySourceProcessor對象的processPropertySource()方法,并傳入PropertySourceDescriptor對象進(jìn)行進(jìn)一步處理。

4. 解析PropertySourceProcessor類的processPropertySource(PropertySourceDescriptor descriptor)方法

public void processPropertySource(PropertySourceDescriptor descriptor) throws IOException {
    String name = descriptor.name();
    String encoding = descriptor.encoding();
    List<String> locations = descriptor.locations();
    Assert.isTrue(locations.size() > 0, "At least one @PropertySource(value) location is required");
    boolean ignoreResourceNotFound = descriptor.ignoreResourceNotFound();
    PropertySourceFactory factory = (descriptor.propertySourceFactory() != null ? instantiateClass(descriptor.propertySourceFactory()) : DEFAULT_PROPERTY_SOURCE_FACTORY);
    for (String location : locations) {
        try {
            String resolvedLocation = this.environment.resolveRequiredPlaceholders(location);
            Resource resource = this.resourceLoader.getResource(resolvedLocation);
            addPropertySource(factory.createPropertySource(name, new EncodedResource(resource, encoding)));
        }
        catch (IllegalArgumentException | FileNotFoundException | UnknownHostException | SocketException ex) {
   //#########省略其他代碼################
        }
    }
}

可以看到,在processPropertySource()方法中,會通過@PropertySource注解的屬性值解析出配置文件的內(nèi)容,并且通過factory對象的createPropertySource()方法來創(chuàng)建PropertySource對象。

5. 解析DefaultPropertySourceFactory類的createPropertySource(String name, EncodedResource resource)方法

@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
    return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource));
}

6. 回到PropertySourceProcessor類的processPropertySource(PropertySourceDescriptor descriptor)方法

在PropertySourceProcessor類的processPropertySource()方法中,創(chuàng)建完P(guān)ropertySource對象后,會調(diào)用addPropertySource()方法將獲取到的屬性值添加到Spring的環(huán)境變量中。

7. 解析PropertySourceProcessor類的addPropertySource(PropertySource<?> propertySource)方法

private void addPropertySource(org.springframework.core.env.PropertySource<?> propertySource) {
    String name = propertySource.getName();
    MutablePropertySources propertySources = this.environment.getPropertySources();
    if (this.propertySourceNames.contains(name)) {
        org.springframework.core.env.PropertySource<?> existing = propertySources.get(name);
        if (existing != null) {
            PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ?((ResourcePropertySource) propertySource).withResourceName() : propertySource);
            if (existing instanceof CompositePropertySource) {
                ((CompositePropertySource) existing).addFirstPropertySource(newSource);
            }
            else {
                if (existing instanceof ResourcePropertySource) {
                    existing = ((ResourcePropertySource) existing).withResourceName();
                }
                CompositePropertySource composite = new CompositePropertySource(name);
                composite.addPropertySource(newSource);
                composite.addPropertySource(existing);
                propertySources.replace(name, composite);
            }
            return;
        }
    }
    if (this.propertySourceNames.isEmpty()) {
        propertySources.addLast(propertySource);
    }
    else {
        String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1);
        propertySources.addBefore(firstProcessed, propertySource);
    }
    this.propertySourceNames.add(name);
}

可以看到,在PropertySourceProcessor類的addPropertySource()方法中,會將解析出的配置文件的內(nèi)容添加到Spring的環(huán)境變量中。

具體就是在PropertySourceProcessor類的addPropertySource()方法中,獲取到ConfigurableEnvironment中的MutablePropertySources對象,用來存儲解析出的配置文件中的配置項內(nèi)容。

如果有相同的配置項內(nèi)容,將existing對象強轉(zhuǎn)為CompositePropertySource類型,把新舊相同的配置項進(jìn)行合并,再放到MutablePropertySources對象中。

后續(xù)就可以通過Spring的環(huán)境變量,來獲取到配置文件中的配置項內(nèi)容。

至此,@PropertySource注解在Spring源碼中的執(zhí)行流程分析完畢。

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

相關(guān)文章

最新評論

沅陵县| 军事| 绥滨县| 岱山县| 南岸区| 雅江县| 桂平市| 揭阳市| 阿勒泰市| 京山县| 巫山县| 雅安市| 苗栗县| 三台县| 长春市| 南平市| 莆田市| 酒泉市| 庐江县| 临沭县| 青海省| 莲花县| 五指山市| 璧山县| 囊谦县| 柘荣县| 渝中区| 云安县| 汤原县| 土默特右旗| 中江县| 鹤山市| 新化县| 交口县| 秦皇岛市| 神池县| 丰都县| 鸡东县| 通山县| 万年县| 师宗县|