Spring PropertyPlaceholderConfigurer使用
2016-11-02 11:24
531 查看
很多时候,大多数Spring开发人员只是把整个部署的详细信息(数据库的详细信息,日志文件的路径)写在XML bean配置文件如:
但是,在企业环境中,部署的细节通常只可以由系统管理员或数据库管理员来'触碰',他们可能会拒绝直接访问你的bean的配置文件,它们会要求部署配置一个单独的文件,例如,一个简单的性能(properties)文件,仅具有部署细节。
创建一个属性文件(database.properties),包括数据库的详细信息,把它放到你的项目类路径。
在声明bean配置文件和提供一个PropertyPlaceholderConfigurer映射到 刚才创建的“database.properties”属性文件。
完整的示例
可替代用法
还可以使用 PropertyPlaceholderConfigurer 于某个常量,分享给所有其他bean。例如,定义在一个属性文件中的日志文件的位置,并通过 ${log.filepath} 访问不同的 bean 配置文件的属性值。
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="customerSimpleDAO" class="com.yiibai.customer.dao.impl.SimpleJdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/yiibaijava" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> </beans>
但是,在企业环境中,部署的细节通常只可以由系统管理员或数据库管理员来'触碰',他们可能会拒绝直接访问你的bean的配置文件,它们会要求部署配置一个单独的文件,例如,一个简单的性能(properties)文件,仅具有部署细节。
PropertyPlaceholderConfigurer示例
为了解决这个问题,可以使用 PropertyPlaceholderConfigurer 类通过一个特殊的格式在外部部署细节到一个属性(properties )文件,以及访问bean的配置文件 – ${variable}.创建一个属性文件(database.properties),包括数据库的详细信息,把它放到你的项目类路径。
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/yiibai_db jdbc.username=root jdbc.password=123456
在声明bean配置文件和提供一个PropertyPlaceholderConfigurer映射到 刚才创建的“database.properties”属性文件。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>database.properties</value> </property> </bean>
完整的示例
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>database.properties</value> </property> </bean>
<bean id="customerDAO" class="com.yiibai.customer.dao.impl.JdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="customerSimpleDAO"
class="com.yiibai.customer.dao.impl.SimpleJdbcCustomerDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
</beans>
可替代用法
还可以使用 PropertyPlaceholderConfigurer 于某个常量,分享给所有其他bean。例如,定义在一个属性文件中的日志文件的位置,并通过 ${log.filepath} 访问不同的 bean 配置文件的属性值。
相关文章推荐
- spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- Spring中PropertyPlaceholderConfigurer的使用(二)
- Spring:PropertyPlaceholderConfigurer的使用
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题处理方法
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- spring中使用外部属性文件(关于PropertyPlaceholderConfigurer)
- 使用Spring PropertyPlaceholderConfigurer 配置中文出现乱码的解决方法
- spring+mybatis使用MapperScannerConfigurer引起的PropertyPlaceholderConfigurer无效问题
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- spring_使用占位符连接数据库_PropertyPlaceholderConfigurer_DataSource配置创建获取
- Spring中PropertyPlaceholderConfigurer的使用介绍
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- 配置环境spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- 【转】spring管理属性配置文件properties——使用PropertiesFactoryBean|spring管理属性配置文件properties——使用PropertyPlaceholderConfigurer
- spring中PropertyPlaceholderConfigurer的使用,引入外部资源文件
- Spring中PropertyPlaceholderConfigurer的使用
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置