spring读取项目外部property配置文件

来源:互联网 发布:内增高女鞋皮面淘宝 编辑:程序博客网 时间:2024/05/21 02:35

相较于传统的读取property配置文件的方法,为了数据的安全常把数据源配置与工程分类,放在项目的外部。
配置方法:
在spring的applicationContext文件中添加如下代码:

  <context:property-placeholder location="file:C:/conf/config.properties" order="1" ignore-resource-not-found="true" ignore-unresolvable="true" />  <context:property-placeholder location="classpath*:/config.properties" order="2" ignore-resource-not-found="true" ignore-unresolvable="true" />

order代表加载顺序,当1加载成功 2不会覆盖1中的内容,1加载不成功加载2。
也可以通过bean的方式:

  <bean id="property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="order" value="1"/>    <property name="ignoreResourceNotFound" value="true"/>    <property name="ignoreUnresolvablePlaceholders" value="true" />    <property name="location" value="file:C:/conf/config.properties"/> </bean>
0 0