在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,例如:
但是好像在属性文件定义中却不支持多个属性文件的定义,比如不能这样用config/*.properties。
经过查看源码,发现可以使用locations属性定义多个配置文件:
使用外部属性后如下:
例子:
Spring的框架中为提供了一个BeanFactoryPostProcessor的实体类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
可以将一些动态参数,移出至.properties中,如此的安排可以让XML定义系统相关设定(不常变动),而.properties可以作为客户根据实际自定义一些相关参数。(如不同人数据库的不同密码)
applicationConfig.xml
db.properties
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/jdoserver.properties"/>
</bean>
但是好像在属性文件定义中却不支持多个属性文件的定义,比如不能这样用config/*.properties。
经过查看源码,发现可以使用locations属性定义多个配置文件:
<property name="locations">
<list>
<value>classpath:config/maxid.properties</value>
<value>classpath:config/jdoserver.properties</value>
</list>
</property>
使用外部属性后如下:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.agent.driver}"/>
<property name="url" value="${jdbc.agent.main.url}"/>
</bean>
例子:
Spring的框架中为提供了一个BeanFactoryPostProcessor的实体类别: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
可以将一些动态参数,移出至.properties中,如此的安排可以让XML定义系统相关设定(不常变动),而.properties可以作为客户根据实际自定义一些相关参数。(如不同人数据库的不同密码)
applicationConfig.xml
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.<SPAN class=hilite1>PropertyPlaceholderConfigurer</SPAN>">
<property name="location">
<value>/WEB-INF/db.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>${driver}</value>
</property>
<property name="driverUrl">
<value>${driverUrl}</value>
</property>
<property name="user">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<property name="alias">
<value>spring</value>
</property>
<property name="houseKeepingSleepTime">
<value>90000</value>
</property>
<property name="prototypeCount">
<value>5</value>
</property>
<property name="maximumConnectionCount">
<value>100</value>
</property>
<property name="minimumConnectionCount">
<value>10</value>
</property>
<property name="trace">
<value>true</value>
</property>
<property name="verbose">
<value>true</value>
</property>
</bean>
</beans>
db.properties
driver=com.mysql.jdbc.Driver
driverUrl=jdbc:mysql://localhost/securedoc?user=root&password=sqladmin&useUnicode=true&characterEncoding=GBK
user=root
password=sqladmin