Spring属性占位符PropertyPlaceholderConfigurer的使用
项目启动后读取属性文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 使用location属性定义单个配置文件 <property name="location"> <value>classpath:/com/zsw/config/jdbc.properties</value> </property> --> <!-- 使用locations属性定义多个配置文件 --> <property name="locations"> <list> <value>classpath:/com/zsw/config/jdbc.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> </beans>
或者
<!-- 引入配置文件 --> <context:property-placeholder location="classpath:config.properties"/>
进行声明bean对象及属性文件后,既可以在本属性文件中通过${}进行调用属性文件中的变量值