Spring PropertyPlaceholderConfigurer example
来源:互联网 发布:编程需要什么软件 编辑:程序博客网 时间:2024/06/08 12:15
Often times, most Spring developers just put the entire deployment details (database details, log file path) in XML bean configuration file as following :
<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.mkyong.customer.dao.impl.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="customerSimpleDAO" class="com.mkyong.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/mkyongjava" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean></beans>
But, in a corporate environment, deployment detail is usually only can ‘touch’ by your system or database administrator, they just refuse to access your bean configuration file directly, and they will request a separate file for deployment configuration, for example, a simple properties, with deployment detail only.
PropertyPlaceholderConfigurer
example
To fix it, you can use PropertyPlaceholderConfigurer
class to externalize the deployment details into a properties file, and access from bean configuration file via a special format – ${variable}
.
Create a properties file (database.properties
), include your database details, put it into your project class path.
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mkyongjava jdbc.username=root jdbc.password=password
Declare a PropertyPlaceholderConfigurer
in bean configuration file and map to the ‘database.properties
‘ properties file you created just now.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>database.properties</value> </property> </bean>
Full example
<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.mkyong.customer.dao.impl.JdbcCustomerDAO"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="customerSimpleDAO" class="com.mkyong.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>
Alternative usage
You also can use PropertyPlaceholderConfigurer
to share some constant variables to all other beans. For example, define your log file location in a properties file, and access the properties value from different beans configuration files via ${log.filepath}
.
- Spring PropertyPlaceholderConfigurer example
- Spring--PropertyPlaceholderConfigurer
- spring propertyplaceholderconfigurer
- Spring PropertyPlaceholderConfigurer
- spring PropertyPlaceholderConfigurer
- spring propertyplaceholderconfigurer
- spring propertyplaceholderconfigurer
- Spring PropertyPlaceholderConfigurer
- spring的好处--PropertyPlaceholderConfigurer
- spring的好处--PropertyPlaceholderConfigurer
- [Spring]PropertyPlaceholderConfigurer类相关
- Spring PropertyPlaceholderConfigurer Usage
- spring的 PropertyPlaceholderConfigurer配置
- Spring的PropertyPlaceholderConfigurer应用
- Spring容器扩展--PropertyPlaceholderConfigurer
- Spring配置之PropertyPlaceholderConfigurer
- Spring的PropertyPlaceholderConfigurer应用
- Spring PropertyPlaceholderConfigurer 自定义扩展
- Servlet 生命周期、工作原理
- C#连接ACCESS数据库的详细步骤!
- Unity 5.0 2D (1)
- sizeof运算符
- 快速掌握iOS API的一个小技巧
- Spring PropertyPlaceholderConfigurer example
- 安装GIT
- IOS APNS 含java服务器实现
- HDU 4540(威威猫系列故事——打地鼠)动态规划
- 内核注册流程
- java设计模式学习 ----- 抽象工厂模式(Abstract Factory)
- flash 3.0影片剪辑分层问题
- "Could not load file or assembly \u0027Newtonsoft.Json, Version=4.5.0.0
- Building OpenCV with Visual Studio 2015