springboot添加propertySource過程

 

StandardServletEnvironment

StandardServletEnvironment 添加2個 添加servletConfigInitParams 添加servletContextInitParams



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

# 添加 systemProperties systemEnvironment
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
	}

SpringApplication

不一定有,啓動的時候加了命令行參數

這裏是addFirst,添加到第一個位置

protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
		MutablePropertySources sources = environment.getPropertySources();
		if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
			sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
		}
		if (this.addCommandLineProperties && args.length > 0) { // 有命令行參數 比如--server.port=11003
			String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
			if (sources.contains(name)) { // 判斷有沒有存在名稱=name的PropertySource
				PropertySource<?> source = sources.get(name);
				CompositePropertySource composite = new CompositePropertySource(name);
				composite.addPropertySource(
						new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
				composite.addPropertySource(source);
				sources.replace(name, composite); //替換
			}
			else {
				sources.addFirst(new SimpleCommandLinePropertySource(args));  // 這裏添加first
			}
		}
	}

 

 

# systemEnvironment

# StandardEnvironment添加 SystemEnvironmentPropertySource
propertySources.addLast( // 環境變量
				new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment())); // addLast systemEnvironment

 

 

 

ConfigFileApplicationListener

public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
		addPropertySources(environment, application.getResourceLoader());
	}


protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
		RandomValuePropertySource.addToEnvironment(environment); // 添加RandomValuePropertySource
		new Loader(environment, resourceLoader).load(); // 默認情況下 這裏加載所有application開頭的資源文件
	}

 

// 添加RandomValuePropertySource 添加到systemEnvironment後面

加載所有的application配置文件,每個配置文件1個propertysource 

 

 

 

 

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