spring 分散配置,将一些相同的配置单独独立成一个文件,减少配置量
spring分离配置,使用spring的PropertyPlaceholderConfigurer告诉spring从外部装载配置信息。
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>jdbc.properties</value>
</property>
</bean>
</beans>
属性location 指定装载配置文件的路径
如果想配置多个配置文件,使用locations
jdbc.properties
database.url=jdbc:hsqldb:Training
database.driver=org.hsqldb.jdbcDriver
database.username=user
database.password=password
<beans>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>jdbc.properties</value>
<value>security.properties</value>
<value>application.properties</value>
</list>
</property>
</bean>
<!--引入数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.dataSource.DriverManagerDataSource">
<property name="url"><value>${database.url}</value></property>
<property name="driverClassName"><value>${database.driver}</value></property>
<property name="username"><value>${database.username}</value></property>
<property name="password"><value>${database.password}</value></property>
</bean>
</beans>
将配置文件载入后,可以采用占位符来表示值${var}