Spring初始化读取配置文件与placeholder使用

Spring初始化读取配置文件与placeholder使用

来自:http://www.codejava.net/frameworks/spring/reading-properties-files-in-spring-with-propertyplaceholderconfigurer-bean

  • 实现功能

在spring context配置文件中使用占位符${key}读取本地配置文件中的配置key=value

  • org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

这个类负责在应用启动时候读取配置文件中的配置并解析placeholders

  • 配置文件位置

    1. 指定目录
    2. 指定 classpath:
    3. 指定绝对路径 file:
  • systemPropertiesModeName

PropertyPlaceholderConfigurer的属性systemPropertiesModeName指定了解析placeholder时针对系统属性的策略,默认为SYSTEM_PROPERTIES_MODE_FALLBACK

Spring provides three modes as follows:
SYSTEM_PROPERTIES_MODE_FALLBACK:  if a placeholder could not be resolved by the specified properties files, Spring will try to resolve it with a system property
SYSTEM_PROPERTIES_MODE_OVERRIDE: In this mode, Spring will resolve the placeholders to system properties first. If a system property does exist, its value will override the value in the properties file.
SYSTEM_PROPERTIES_MODE_NEVER: Spring will not take system properties into consideration when resolving the placeholders.
  • 异常

当找不到配置文件或无法解析placeholder时会抛出相应异常,以下两个属性控制是否ignore,一般不会置为true

ignoreUnresolvablePlaceholders:无法解析直接用placeholder的名字,没人这样用的吧

ignoreResourceNotFound:忽略资源文件找不到异常

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值