Spring-xml文件加载时,环境配置过程

Spring-xml文件加载,环境配置过程

spring 读取xml文件 策略模式

设置读取xml文件的模式
返回用于将位置模式解析为资源实例的资源模式解析器。默认值是路径匹配资源模式解析器,支持Ant样式的位置模式。
可以在子类中重写,例如在web环境中,对于扩展的解析策略。

设置此应用程序上下文的配置文件位置

设置配置文件位置
查看 resolvePath() 方法中的 getEnvironment()

可以配置的形式返回此应用程序上下文的环境,允许进一步自定义。
如果未指定,将通过 createEnvironment()初始化默认环境。

spring

StandardEnvironment.java 具有默认具有两种环境配置

  • 系统属性
  • 系统环境变量
    JVM系统属性 会覆盖 系统属性值
new StandardEnvironment() 

父类AbstractEnvironment构造器

public abstract class AbstractEnvironment implements ConfigurableEnvironment {
	public AbstractEnvironment() {
		this(new MutablePropertySources());
	}
}

	protected AbstractEnvironment(MutablePropertySources propertySources) {
		this.propertySources = propertySources;
		this.propertyResolver = createPropertyResolver(propertySources);
		customizePropertySources(propertySources);
	}

StandardEnvironment.java 实现父类 AbstractEnvironment#customizePropertySources方法,设置属性值

public class StandardEnvironment extends AbstractEnvironment {
	@Override
	protected void customizePropertySources(MutablePropertySources propertySources) {
		propertySources.addLast(
				new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
		propertySources.addLast(
				new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
	}

读取配置AbstractEnvironment#getSystemEnvironment
AbstractEnvironment#getSystemProperties

路径Placeholder处理

修改测试代码

public class SimpleBeanSpring {
	public static void main(String[] args) {
		System.setProperty("spring", "classpath");
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("${spring}:SimpleBean.xml");
		SimpleBean bean = context.getBean(SimpleBean.class);
		bean.send();
		context.close();
	}

字符替换
resolveRequiredPlaceholders方法会将${} 中内容从环境变量中获取值进行替换

System.setProperty(“spring”, “classpath”);
ClassPathXmlApplicationContext context = new > ClassPathXmlApplicationContext(“${spring}:SimpleBean.xml”);
替换为
ClassPathXmlApplicationContext(“classpath:SimpleBean.xml”);
替换值
文件路径处理完毕后,再进行xml文件解析

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值