【问题标题】:Using properties file inside the xml在 xml 中使用属性文件
【发布时间】:2011-12-24 06:48:04
【问题描述】:

我正在使用 Spring Jdbc 模板,为此我有 context.xml

    <property name="driverClassName" value="com.informix.jdbc.IfxDriver" />
    <property name="url"
        value="jdbc:informix-sqli://testdb:1111/dddd:informixserver=linuxdev" />
    <property name="username" value="test" />
    <property name="password" value="test" />
</bean>

因为我已经硬编码了像“driverClassName”这样的值,所以我想从像 ${test.driverName} 这样的属性文件中加载它们。

有人知道我该怎么做吗?

【问题讨论】:

    标签: xml spring file properties loading


    【解决方案1】:

    在 Spring 3 中,您可以设置属性占位符位置并立即使用 ${key} 表示法:

    <context:property-placeholder location="classpath:config.properties"/>
    

    在 Spring 2(我认为)中,您将需要像这样引入一个 propertyConfigurer bean:

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:config.properties</value>
            </list>
        </property>
    </bean>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多