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)));
}