spring-framework 配置文件读取系统变量

spring-framework 配置文件读取系统变量

使用过spring-boot的同学都知道,boot内置了很多特性,比如:配置文件随机值;其实boot是建立在framework之上的,底层用的都一样只是boot进行更多封装

配置文件处理类PropertyPlaceholderConfigurer

spring读取配置的内部实现类,简单使用可看 https://docs.spring.io/spring/docs/4.3.15.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#expressions-beandef-xml-based, 深入了解可以看源码

下面描述摘取自spring官方文档:

The PropertyPlaceholderConfigurer not only looks for properties in the Properties file you specify. 
By default it also checks against the Java System properties if it cannot find a property in the specified properties files. 
You can customize this behavior by setting the systemPropertiesMode property of the configurer with one of the following three supported integer values:

never (0): Never check system properties
fallback (1): Check system properties if not resolvable in the specified properties files. This is the default.
override (2): Check system properties first, before trying the specified properties files. This allows system properties to override any other property source.
Consult the PropertyPlaceholderConfigurer javadocs for more information.

进入上面文档搜索"systemProperties"关键字

简单使用
== *.properties ==

local.username = #{systemProperties['user.name']}
xx.queue = xx.${local.username}
yy.queue = yy.${local.username}
系统变量
public static void main(String[] args) {
        Properties properties = System.getProperties();
        properties.stringPropertyNames().forEach(e -> System.out.println(e + ": " + properties.getProperty(e)));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值