spring boot默認已經配置了不少環境變量,例如,tomcat的默認端口是8080,項目的contextpath是「/」等等,spring boot容許你自定義一個application.properties文件,而後放在如下的地方,來重寫spring boot的環境變量web
spring對配置application.properties的加載過程:spring
- 服務啓動調用:SpringApplication.run
- 建立默認的環境參數:ConfigurableEnvironment
- 觸發事件:ApplicationEnvironmentPreparedEvent
- 完成加載
整個過程主要使用spring boot 內置的ConfigFileApplicationListener監聽器監聽ApplicationEnvironmentPreparedEvent事件完成對application.properties加載以及設置。tomcat
下面咱們來跟蹤源碼,看下spring boot是怎樣完成對application.properties文件的加載app
- SpringApplication 入口 run:
-
public ConfigurableApplicationContext run(String... args) {
-
-
-
ConfigurableApplicationContext context =
null;
-
FailureAnalyzers analyzers =
null;
-
configureHeadlessProperty();
-
-
SpringApplicationRunListeners listeners = getRunListeners(args);
-
-
-
ApplicationArguments applicationArguments =
new DefaultApplicationArguments(
-
-
-
-
ConfigurableEnvironment environment = prepareEnvironment(listeners,
-
-
-
prepareEnvironment方法less
-
private ConfigurableEnvironment prepareEnvironment(
-
SpringApplicationRunListeners listeners,
-
ApplicationArguments applicationArguments) {
-
-
-
ConfigurableEnvironment environment = getOrCreateEnvironment();
-
configureEnvironment(environment, applicationArguments.getSourceArgs());
-
-
listeners.environmentPrepared(environment);
-
if (!this.webEnvironment) {
-
environment =
new EnvironmentConverter(getClassLoader())
-
.convertToStandardEnvironmentIfNecessary(environment);
-
-
-
- ConfigFileApplicationListener:
-
public void onApplicationEvent(ApplicationEvent event) {
-
-
if (event instanceof ApplicationEnvironmentPreparedEvent) {
-
onApplicationEnvironmentPreparedEvent(
-
(ApplicationEnvironmentPreparedEvent)
event);
-
-
if (event instanceof ApplicationPreparedEvent) {
-
onApplicationPreparedEvent(
event);
-
-
onApplicationEnvironmentPreparedEventdom
-
private void onApplicationEnvironmentPreparedEvent(
-
ApplicationEnvironmentPreparedEvent
event) {
-
-
List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
-
-
postProcessors.
add(this);
-
AnnotationAwareOrderComparator.sort(postProcessors);
-
-
-
for (EnvironmentPostProcessor postProcessor : postProcessors) {
-
postProcessor.postProcessEnvironment(
event.getEnvironment(),
-
event.getSpringApplication());
-
-
postProcessEnvironmentide
-
-
public void postProcessEnvironment(ConfigurableEnvironment environment,
-
SpringApplication application) {
-
-
addPropertySources(environment, application.getResourceLoader());
-
configureIgnoreBeanInfo(environment);
-
bindToSpringApplication(environment, application);
-
addPropertySources工具
-
protected void addPropertySources(ConfigurableEnvironment environment,
-
ResourceLoader resourceLoader) {
-
-
RandomValuePropertySource.addToEnvironment(environment);
-
-
new Loader(environment, resourceLoader).load();
-
loadpost
-
-
-
this.propertiesLoader = new PropertySourcesLoader();
-
this.activatedProfiles = false;
-
-
-
while (!this.profiles.isEmpty()) {
-
Profile profile =
this.profiles.poll();
-
-
for (String location : getSearchLocations()) {
-
-
if (!location.endsWith("/")) {
-
-
-
load(location,
null, profile);
-
-
-
-
for (String name : getSearchNames()) {
-
load(location, name, profile);
-
-
-
-
this.processedProfiles.add(profile);
-
-
-
-
addConfigurationProperties(
this.propertiesLoader.getPropertySources());
-
loadui
-
private void load(String location, String name, Profile profile) {
-
-
String
group = "profile=" + (profile == null ? "" : profile);
-
if (!StringUtils.hasText(name)) {
-
-
loadIntoGroup(
group, location, profile);
-
-
-
-
-
loadIntoGroup(
group, location + name + "." + ext, profile);
-
-
-
loadIntoGroup
-
private PropertySource<?> doLoadIntoGroup(
String identifier, String location,
-
Profile profile) throws IOException {
-
Resource resource =
this.resourceLoader.getResource(location);
-
PropertySource<?> propertySource =
null;
-
if (resource != null && resource.exists()) {
-
String name = "applicationConfig: [" + location + "]";
-
String group = "applicationConfig: [" + identifier + "]";
-
-
-
-
propertySource =
this.propertiesLoader.load(resource,
-
-
-
-
- PropertiesPropertySourceLoader:
-
-
public PropertySource<?> load(String name, Resource resource, String profile)
-
-
-
-
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
-
if (!properties.isEmpty()) {
-
return new PropertiesPropertySource(name, properties);
-
-
-
-
到此application.properties就真正的加載並共享到環境信息中,供系統其它地方調用