Spring PropertyPlaceholderConfigurer example

本文介绍如何使用Spring框架中的PropertyPlaceholderConfigurer将部署细节(如数据库连接信息)从XML配置文件中分离出来,放置到独立的属性文件中进行管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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}.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值