initPropertySource()方法具有什么功能呢?

151 篇文章 3 订阅

转自:

initPropertySource()方法具有什么功能呢?

下文笔者讲述initPropertySource()方法的功能简介说明,如下所示

initPropertySource()方法简介

initPropertySource()方法简介:
    主要用于Environment后
	为系统提供良好的扩展

initPropertySource()方法启动点

 
public ClassPathXmlApplicationContext(String[] configLocations, 
            boolean refresh,
			ApplicationContext parent) throws BeansException{
	super(parent);
	setConfigLocationins(configurations);
	if(refresh){
		refresh();
	}
}
 

refresh()中的第一个方法prepareRefresh()
 调用initPropertySources()方法

protected void prepareRefresh(){
	initPropertySources();
}
 
从以上的源码中
我们得知prepareRefresh()中
 initPropertySources()方法并没有任何参数
 当进入initPropertySources()方法内部,也会发现该方法是空的,没有任何逻辑。

 Spring采用此种方式设计的目的
 为了让用户根据自己的需要
  重写initPropertySources()方法
  并在其中实现自己的逻辑 

定义项目启动时,必须拥有属性 fileCodingType 
 那么我们就可以通过扩展initPropertySources()方法的方式
 使系统必须拥有此属性 

public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext{	
	protected void initPropertySources(){
		//添加验证要求
		getEnvironment().setRequiredProperties("fileCodingType");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值