我是靠谱客的博主 迷你流沙,最近开发中收集的这篇文章主要介绍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
	}

}

 

 

 

最后

以上就是迷你流沙为你收集整理的spring源码之Environment的全部内容,希望文章能够帮你解决spring源码之Environment所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部