<!-- JDBC参数配置 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreResourceNotFound" value="true" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="locations"> <list> <!-- 单元测试配置 --> <value>classpath:config/config.properties</value> <value>classpath:config/ofcard.properties</value> <!-- 自测配置 --> <value>/WEB-INF/conf/config.properties</value> <value>/WEB-INF/conf/ofcard.properties</value> <!-- 生产环境 --> <value>file:/apps/conf/config.properties</value> <value>file:/apps/conf/ofcard.properties</value> </list> </property> </bean>PropertyPlaceholderConfigurer 这个类是spring用来处理系统配置文件并且具有将spring配置文件中的以${properties}的形式替换成key为properties的value其中成员变量systemPropertiesModeName是用来指示后面的配置文件中的值是否会覆盖前面的值默认情况下如果配置文件中有不能解析的占位符则会抛出异常ignoreUnresolvablePlaceholders用来指示忽略此异常,ignoreResourceNotFound用来指示如果文件找不到则忽略以上locations中一个文件配置了3个地方是为了在单元测试和自测已经生产环境下使用这样配置文件的放置的位置就会更灵活。
有人会问systemPropertiesModeName 这可以设置的几个值分别代表什么含义通过阅读PropertyPlaceholderConfigurer中的setSystemPropertiesMode方法上面的注释可以发现:
* Set how to check system properties: as fallback, as override, or never.
* For example, will resolve ${user.dir} to the "user.dir" system property.
* <p>The default is "fallback": If not being able to resolve a placeholder
* with the specified properties, a system property will be tried.
* "override" will check for a system property first, before trying the
* specified properties. "never" will not check system properties at all.
* @see #SYSTEM_PROPERTIES_MODE_NEVER
* @see #SYSTEM_PROPERTIES_MODE_FALLBACK
* @see #SYSTEM_PROPERTIES_MODE_OVERRIDE
fallback模式:在解析一个占位符的变量的时候,如果不能获取到该变量的值,就会拿系统属性来尝试,
override模式:在解析一个占位符的时候,会先用系统属性来尝试,然后才会用指定的属性文件,
never模式:从来都不会使用系统属性来尝试。
本人在系统属性这个概念上纠结了很长时间,后来尝试的用System.getProperties的方法打印出所有的jvm的系统属性发现user.dir是jvm的一个内置系统属性,到了这边我才明白原来spring配置文件中时可以直接用${}的形式来使用jvm的系统属性的,