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

打印 上一主题 下一主题

主题 899|帖子 899|积分 2697

转自:
http://www.java265.com/JavaFramework/Spring/202207/3975.html
下文笔者讲述initPropertySource()方法的功能简介说明,如下所示
initPropertySource()方法简介
  1. initPropertySource()方法简介:
  2.     主要用于Environment后
  3.         为系统提供良好的扩展
复制代码
initPropertySource()方法启动点
  1. public ClassPathXmlApplicationContext(String[] configLocations,
  2.             boolean refresh,
  3.                         ApplicationContext parent) throws BeansException{
  4.         super(parent);
  5.         setConfigLocationins(configurations);
  6.         if(refresh){
  7.                 refresh();
  8.         }
  9. }
  10. refresh()中的第一个方法prepareRefresh()
  11. 调用initPropertySources()方法
  12. protected void prepareRefresh(){
  13.         initPropertySources();
  14. }
  15. 从以上的源码中
  16. 我们得知prepareRefresh()中
  17. initPropertySources()方法并没有任何参数
  18. 当进入initPropertySources()方法内部,也会发现该方法是空的,没有任何逻辑。
  19. Spring采用此种方式设计的目的
  20. 为了让用户根据自己的需要
  21.   重写initPropertySources()方法
  22.   并在其中实现自己的逻辑
复制代码
  1. <strong>定义项目启动时,必须拥有属性 fileCodingType
  2. 那么我们就可以通过扩展initPropertySources()方法的方式
  3. 使系统必须拥有此属性
  4. public class MyClassPathXmlApplicationContext extends ClassPathXmlApplicationContext{       
  5.         protected void initPropertySources(){
  6.                 //添加验证要求
  7.                 getEnvironment().setRequiredProperties("fileCodingType");
  8.         }
  9. }</strong>
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

乌市泽哥

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表