• 8 -- 深入使用Spring -- 1...4 重写占位符配置器


          8.1.5 重写占位符配置器 (PropertyOverrideConfigurer)

            PropertyOverrideConfigurer是Spring提供的另一个容器后处理器。PropertyOverrideConfigurer的属性文件指定的信息可以直接覆盖Spring配置文件中的元数据。 

            使用PropertyOverrideConfigurer的属性文件,每条属性应保持如下格式:beanId.property = value

            beanId 是属性占位符试图覆盖的Bean的id,Property是试图覆盖的属性名(对应于调用setter方法)。

            XML : 

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Spring 配置文件的根元素,使用Spring-beans-4.0.xsd语义约束 -->
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans" 
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer">
            <property name="locations">
                <list>
                    <value>dbconn.properties</value>
                    <!-- 如果有多个属性文件,依次在下面列出来 -->
                    <!-- <value>wawa.properties</value> -->
                </list>
            </property>
        </bean>
        
        <!-- 对于采用基于XML Schema的配置文件而言,如果导入了context:命名空间,则可采用如下方式来配置该属性占位符 -->
        <!-- <context:property-override location="classpath:wawa.properties"/> -->
    
        <!-- 定义数据源Bean,使用C3P0数据源实现 -->
        <!-- 配置该Bean时没有指定任何信息,但Properties文件里的信息将会直接覆盖该Bean的属性值。 -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
            destroy-method="close">
        </bean>
    
    </beans>

            Properties : 

    dataSource.jdbc.driverClassName=com.mysql.jdbc.Driver
    dataSource.jdbc.url=jdbc:mysql://localhost:3306/spring
    dataSource.jdbc.username=root
    dataSource.jdbc.password=system

            由于PropertyOverrideConfigurer容器后处理器,Spring容器使用ApplicationContext作为容器时,容器会自动检测容器后处理器,并使用该容器后处理器来处理Spring容器。

            beanId必须是容器中真是存在的Bean的id,否则程序将出错。

            如果有多个PropertyOverrideConfigurer对同一个Bean属性进行了覆盖,最后一次覆盖将会获胜。

          啦啦啦

  • 相关阅读:
    朴素贝叶斯算法(python)
    《python数据分析基础》之图与图表
    《机器学习实战》之K-近邻算法
    《机器学习实战》之决策树
    决策树算法(python)
    图像的处理
    K-近邻算法(python)
    python实现机器学习的小项目-鸢尾花
    统计学习方法概述
    《python数据分析基础》之数据库
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/6388021.html
Copyright © 2020-2023  润新知