spring boot 源码分析(六) 配置文件加载之StandardServletEnvironment

一、前言

前面,我们通过源码的方法解析了以下StandardEnvionment,本章,我们继续解析关于web

的envionment,叫做StandardServletEnvironment.

二、类图

我们通过idea自带的类图生成工具生成关于该类的类图

223023_2xQi_1178126.png

三、源码解析

package org.springframework.web.context.support;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import org.springframework.core.env.Environment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.PropertySource.StubPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.jndi.JndiLocatorDelegate;
import org.springframework.jndi.JndiPropertySource;
import org.springframework.lang.Nullable;
import org.springframework.web.context.ConfigurableWebEnvironment;

//用于web应用,所有基于web的ApplicationContext 类默认都会初始化一个实例
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";

    //使用超类贡献和适用于基于servlet标准环境定制属性源:
    //servletConfigInitParams
    //servletContextInitParams
    //jndiProperties
    //优先级  servletConfigInitParams>servletContextInitParams>
    //jndiProperties>StandardEnvironment
    //(system properties and environment variables)
    //基于servlet的属性源在这一步会先存起来,当ServletObject变得可用的时候,他会完全初始化。
	@Override
	protected void customizePropertySources(MutablePropertySources propertySources) {
		propertySources.addLast(new StubPropertySource(SERVLET_CONFIG_PROPERTY_SOURCE_NAME));
		propertySources.addLast(new StubPropertySource(SERVLET_CONTEXT_PROPERTY_SOURCE_NAME));
		if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
			propertySources.addLast(new JndiPropertySource(JNDI_PROPERTY_SOURCE_NAME));
		}
		super.customizePropertySources(propertySources);
	}

	@Override
	public void initPropertySources(@Nullable ServletContext servletContext, @Nullable ServletConfig servletConfig) {
		WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
	}

}

我们通过源码可以得出,他的addLast顺序,严格按照优先级

servletConfigInitParams>servletContextInitParams>jndiProperties>StandardEnvironment(system properties and environment variables)的顺序来的。

230205_hXA4_1178126.png

230229_RnSK_1178126.png

230318_6icB_1178126.png

转载于:https://my.oschina.net/u/1178126/blog/1823557

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值