在SPRING中提供了一个专门加载属性文件的类,我们只要给它指定属性文件所在的路径即可。这样我们就可以把一些经常变化的配置放入特定的属性文件中。下面为该类在SPRING中的配置:
- <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>classpath:com/starxing/test/database.properties</value>
- </property>
- <!-- 使用locations属性定义多个配置文件
- <property name="locations">
- <list>
- <value>classpath:config/mail.properties</value>
- <value>classpath:config/database.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.user}</value>
- </property>
- <property name="password">
- <value>${database.password}</value>
- </property>
- </bean>
database.properties
database.driver=org.postgresql.Driver
database.url=jdbc:postgresql://localhost/test
database.user=postgres
database.password=123456
propertyConfigurer加载database.properties文件,为创建dataSource提供参数。