spring 读取属性文件的便捷方式:
<bean
class="
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="
locations">
<list>
<value>
classpath*:*.properties</value>
</list>
</property>
</bean>
替换上边PropertyPlaceholderConfigurer方式:
<context:property-placeholder location="classpath*:*.properties" />
PropertyPlaceholderConfigurer内置的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。
bean配置文件加上这句代码,下边如需要取properties文件中的值就可以用"${properties_key}"
以下是得到C3P0数据库连接池,spring的配置信息。
<bean id="dataSource" class="
com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="
${c3p0.driverClass}"></property>
<property name="jdbcUrl" value="
${c3p0.url}"></property>
<property name="user" value="
${c3p0.user}"></property>
<property name="password" value="
${c3p0.password}"></property>
<property name="acquireIncrement" value="
${c3p0.acquireIncrement}"></property>
<property name="initialPoolSize" value="
${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="
${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="
${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="
${c3p0.minPoolSize}"></property>
<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
PorpertyPlaceholderConfiger 还有一些常用的属性,在一些高级应用中,可能会用到
- locations
- fileEncoding 属性文件的编码格式
- order 文件中定义了多个PropertyPlaceholderConfigurer,则通过该属性指定优先顺序。
- placeholderPrefix 默认前缀占位符是"${"。可以根据需要改为其它的前缀符。
- placeholdersuffix 默认后缀占位符是“}”。