Spring PropertyPlaceholderConfigurer实例 - Spring教程

很多时候,大多数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/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 配置文件的属性值。