Spring PropertyPlaceholderConfigurer使用
来源:互联网 发布:雪人翻译软件 编辑:程序博客网 时间:2024/06/05 10:22
很多时候,大多数Spring开发人员只是把整个部署的详细信息(数据库的详细信息,日志文件的路径)写在XML 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/beanshttp://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.Driverjdbc.url=jdbc:mysql://localhost:3306/yiibai_dbjdbc.username=rootjdbc.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><beanclass="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 配置文件的属性值。 1 0
- Spring PropertyPlaceholderConfigurer的使用
- Spring PropertyPlaceholderConfigurer的使用
- spring propertyplaceholderconfigurer的使用
- Spring的PropertyPlaceholderConfigurer使用
- Spring:PropertyPlaceholderConfigurer的使用
- Spring PropertyPlaceholderConfigurer 简单使用
- Spring PropertyPlaceholderConfigurer使用
- Spring 中 PropertyPlaceholderConfigurer 的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- spring下PropertyPlaceholderConfigurer的使用
- spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring--PropertyPlaceholderConfigurer
- 16 down vote favorite 2 Here's a preview of the status running supervisorctl status every 2 seconds
- 每天工作4小时的程序员
- opencv 存储图像 & 递归搜索路径
- 检测键盘的出现跟消失
- main(int argc,char **argv)参数解析
- Spring PropertyPlaceholderConfigurer使用
- Swift 3.0 通知的注册,监听及销毁
- 回车登录兼容代码
- java之内省技术
- redis的中aof文件存储超出限制处理
- QtMetaObjectsysmtem详解之三:QMetaObject接口实现
- LeetCode 第二十六题(Remove Duplicates from Sorted Array) java
- PHP中的自动加载
- 还在用cornerRadius设置圆角么?快来试试效率更高的新方法。