spring源碼之Environment

SpringApplication


public ConfigurableApplicationContext run(String... args) {
		
			ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments); //準備好了Environment,此刻Environment中都有哪些配置參數
			configureIgnoreBeanInfo(environment);
			
			
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}

		try {
			listeners.running(context); //發佈running中事件  ApplicationReadyEvent
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context; // 返回ok的ConfigurableApplicationContext
	}

 


private ConfigurableEnvironment getOrCreateEnvironment() {
		if (this.environment != null) {
			return this.environment;
		}
		switch (this.webApplicationType) {
		case SERVLET:
			return new StandardServletEnvironment(); //servlet
		case REACTIVE:
			return new StandardReactiveWebEnvironment();
		default:
			return new StandardEnvironment();
		}
}

AbstractEnvironment


public abstract class AbstractEnvironment implements ConfigurableEnvironment {

    public AbstractEnvironment() {
		customizePropertySources(this.propertySources); //子類覆蓋
	}
    
}


# 調用關係
# AbstractEnvironment 構造函數 -> StandardServletEnvironment.customizePropertySources -> StandardEnvironment.customizePropertySources

StandardServletEnvironment



public class StandardServletEnvironment extends StandardEnvironment implements ConfigurableWebEnvironment {

	/** Servlet context init parameters property source name: {@value}. */
	public static final String SERVLET_CONTEXT_PROPERTY_SOURCE_NAME = "servletContextInitParams";

	/** Servlet config init parameters property source name: {@value}. */
	public static final String SERVLET_CONFIG_PROPERTY_SOURCE_NAME = "servletConfigInitParams";

	/** JNDI property source name: {@value}. */
	public static final String JNDI_PROPERTY_SOURCE_NAME = "jndiProperties";


	
	@Override
	protected void customizePropertySources(MutablePropertySources propertySources) {
		propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME)); //添加servletConfigInitParams propertysource
		propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)); //添加servletContextInitParams  propertysource
		if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
			propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME)); //jndiProperties
		}
		super.customizePropertySources(propertySources); // 添加systemEnvironment systemProperties
	}

}

 

StandardEnvironment 

public class StandardEnvironment extends AbstractEnvironment {

	/** 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( //系統參數 (比如vm options)
				new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties())); // addLast systemProperties
		propertySources.addLast( // 環境變量
				new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); // addLast systemEnvironment
	}

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章