1. spring5源碼講解-bean的加載過程(一)

spring-core源碼解析-(1)

基本

本部分從最基本的Spring開始。配置文件:

<?xml version="1.0" encoding="UTF-8"?>    
<beans>    
    <bean class="base.SimpleBean"></bean>
</beans>

啟動(dòng)代碼:

public static void main(String[] args) {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
    SimpleBean bean = context.getBean(SimpleBean.class);
    bean.send();
    context.close();
}

SimpleBean:

public class SimpleBean {
    public void send() {
        System.out.println("I am send method from SimpleBean!");
    }
}

ClassPathXmlApplicationContext

整個(gè)繼承體系如下:

ClassPathXmlApplicationContext繼承體系

ResourceLoader代表了加載資源的一種方式,正是策略模式的實(shí)現(xiàn)。

構(gòu)造器源碼:

public ClassPathXmlApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent) {
    super(parent);
    setConfigLocations(configLocations);
    //默認(rèn)true
    if (refresh) {
        refresh();
    }
}

構(gòu)造器

首先看父類構(gòu)造器,沿著繼承體系一直向上調(diào)用,直到AbstractApplicationContext:

public AbstractApplicationContext(ApplicationContext parent) {
    this();
    setParent(parent);
}
public AbstractApplicationContext() {
    this.resourcePatternResolver = getResourcePatternResolver();
}

getResourcePatternResolver:

protected ResourcePatternResolver getResourcePatternResolver() {
    return new PathMatchingResourcePatternResolver(this);
}

PathMatchingResourcePatternResolver支持Ant風(fēng)格的路徑解析。

設(shè)置配置文件路徑

即AbstractRefreshableConfigApplicationContext.setConfigLocations:

public void setConfigLocations(String... locations) {
    if (locations != null) {
        Assert.noNullElements(locations, "Config locations must not be null");
        this.configLocations = new String[locations.length];
        for (int i = 0; i < locations.length; i++) {
            this.configLocations[i] = resolvePath(locations[i]).trim();
        }
    } else {
        this.configLocations = null;
    }
}

resolvePath:

protected String resolvePath(String path) {
    return getEnvironment().resolveRequiredPlaceholders(path);
}

此方法的目的在于將占位符(placeholder)解析成實(shí)際的地址。比如可以這么寫: new ClassPathXmlApplicationContext("classpath:config.xml");那么classpath:就是需要被解析的。

getEnvironment方法來自于ConfigurableApplicationContext接口,源碼很簡(jiǎn)單,如果為空就調(diào)用createEnvironment創(chuàng)建一個(gè)。AbstractApplicationContext.createEnvironment:

protected ConfigurableEnvironment createEnvironment() {
    return new StandardEnvironment();
}

Environment接口

StandardEnvironment繼承體系:

StandardEnvironment繼承體系

Environmen接口代表了當(dāng)前應(yīng)用所處的環(huán)境。從此接口的方法可以看出,其主要和profile、Property相關(guān)。

Profile

Spring Profile特性是從3.1開始的,其主要是為了解決這樣一種問題: 線上環(huán)境和測(cè)試環(huán)境使用不同的配置或是數(shù)據(jù)庫或是其它。有了Profile便可以在 不同環(huán)境之間無縫切換。Spring容器管理的所有bean都是和一個(gè)profile綁定在一起的。使用了Profile的配置文件示例:

<beans profile="develop">  
    <context:property-placeholder location="classpath*:jdbc-develop.properties"/>  
</beans>  
<beans profile="production">  
    <context:property-placeholder location="classpath*:jdbc-production.properties"/>  
</beans>  
<beans profile="test">  
    <context:property-placeholder location="classpath*:jdbc-test.properties"/>  
</beans>

在啟動(dòng)代碼中可以用如下代碼設(shè)置活躍(當(dāng)前使用的)Profile:

context.getEnvironment().setActiveProfiles("dev");

當(dāng)然使用的方式還有很多(比如注解),參考:

spring3.1 profile 配置不同的環(huán)境

Spring Profiles example

Property

這里的Property指的是程序運(yùn)行時(shí)的一些參數(shù),引用注釋:

properties files, JVM system properties, system environment variables, JNDI, servlet context parameters, ad-hoc Properties objects,Maps, and so on.

Environment構(gòu)造器

private final MutablePropertySources propertySources = new MutablePropertySources(this.logger);
public AbstractEnvironment() {
    customizePropertySources(this.propertySources);
}
PropertySources接口

繼承體系:

PropertySources繼承體系

此接口實(shí)際上是PropertySource的容器,默認(rèn)的MutablePropertySources實(shí)現(xiàn)內(nèi)部含有一個(gè)CopyOnWriteArrayList作為存儲(chǔ)載體。

StandardEnvironment.customizePropertySources:

/** System environment property source name: {@value} */
public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = "systemEnvironment";
/** JVM system properties property source name: {@value} */
public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
    propertySources.addLast(new MapPropertySource
        (SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
    propertySources.addLast(new SystemEnvironmentPropertySource
        (SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
PropertySource接口

PropertySource接口代表了鍵值對(duì)的Property來源。繼承體系:

PropertySource繼承體系

AbstractEnvironment.getSystemProperties:

@Override
public Map<String, Object> getSystemProperties() {
    try {
        return (Map) System.getProperties();
    }
    catch (AccessControlException ex) {
        return (Map) new ReadOnlySystemAttributesMap() {
            @Override
            protected String getSystemAttribute(String attributeName) {
                try {
                    return System.getProperty(attributeName);
                }
                catch (AccessControlException ex) {
                    if (logger.isInfoEnabled()) {
                        logger.info(format("Caught AccessControlException when accessing system " +
                                "property [%s]; its value will be returned [null]. Reason: %s",
                                attributeName, ex.getMessage()));
                    }
                    return null;
                }
            }
        };
    }
}

這里的實(shí)現(xiàn)很有意思,如果安全管理器阻止獲取全部的系統(tǒng)屬性,那么會(huì)嘗試獲取單個(gè)屬性的可能性,如果還不行就拋異常了。

getSystemEnvironment方法也是一個(gè)套路,不過最終調(diào)用的是System.getenv,可以獲取jvm和OS的一些版本信息。

路徑Placeholder處理

AbstractEnvironment.resolveRequiredPlaceholders:

@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
    //text即配置文件路徑,比如classpath:config.xml
    return this.propertyResolver.resolveRequiredPlaceholders(text);
}

propertyResolver是一個(gè)PropertySourcesPropertyResolver對(duì)象:

private final ConfigurablePropertyResolver propertyResolver =
            new PropertySourcesPropertyResolver(this.propertySources);
PropertyResolver接口

PropertySourcesPropertyResolver繼承體系(排除Environment分支):

PropertySourcesPropertyResolver繼承關(guān)系

此接口正是用來解析PropertyResource。

解析

AbstractPropertyResolver.resolveRequiredPlaceholders:

@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
    if (this.strictHelper == null) {
        this.strictHelper = createPlaceholderHelper(false);
    }
    return doResolvePlaceholders(text, this.strictHelper);
}
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {
    //三個(gè)參數(shù)分別是${, }, :
    return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
        this.valueSeparator, ignoreUnresolvablePlaceholders);
}

doResolvePlaceholders:

private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
    //PlaceholderResolver接口依然是策略模式的體現(xiàn)
    return helper.replacePlaceholders(text, new PropertyPlaceholderHelper.PlaceholderResolver() {
        @Override
        public String resolvePlaceholder(String placeholderName) {
            return getPropertyAsRawString(placeholderName);
        }
    });
}

其實(shí)代碼執(zhí)行到這里的時(shí)候還沒有進(jìn)行xml配置文件的解析,那么這里的解析placeHolder是什么意思呢,原因在于可以這么寫:

System.setProperty("spring", "classpath");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("${spring}:config.xml");
SimpleBean bean = context.getBean(SimpleBean.class);

這樣就可以正確解析。placeholder的替換其實(shí)就是字符串操作,這里只說一下正確的屬性是怎么來的。實(shí)現(xiàn)的關(guān)鍵在于PropertySourcesPropertyResolver.getProperty:

@Override
protected String getPropertyAsRawString(String key) {
    return getProperty(key, String.class, false);
}
protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolveNestedPlaceholders) {
    if (this.propertySources != null) {
        for (PropertySource<?> propertySource : this.propertySources) {
            Object value = propertySource.getProperty(key);
            return value;
        }
    }
    return null;
}

很明顯了,就是從System.getProperty和System.getenv獲取,但是由于環(huán)境變量是無法自定義的,所以其實(shí)此處只能通過System.setProperty指定。

注意,classpath:XXX這種寫法的classpath前綴到目前為止還沒有被處理。

關(guān)注公眾號(hào): 太上老君007
spring源碼分析第一時(shí)間通知你

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容