【问题标题】:xml Spring config using values from properties files使用属性文件中的值的 xml Spring 配置
【发布时间】:2020-03-05 08:44:45
【问题描述】:

对于 xml 中的 DI,通过设置器使用属性文件中的值,我可以使用:

<beans>
..
   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="props.properties"/>
    </bean>
    <bean id="first" class="..">
        <property name="name" value="${values.name}"/>
    </bean>

</beans>

但PropertyPlaceholderConfigurer 类已弃用。

我尝试过使用

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

而不是它,但它没有工作

prop.properties位于resources,错误信息为no declaration can be found for element 'context:property-placeholder'

【问题讨论】:

  • 在下面发布了答案,让我们知道这是否有效

标签: java xml spring dependency-injection properties


【解决方案1】:

根据 PropertyPlaceholderConfigurer

的 Javadocs

从 5.2 开始;采用 org.springframework.context.support.PropertySourcesPlaceholderConfigurer 相反,通过利用 环境和PropertySource机制

你可以像下面这样加载属性,你必须在你的 beans 标签中包含 namespace & schemalocation。从下面复制 beans 标签

 <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
            xmlns:util="http://www.springframework.org/schema/util"
            xsi:schemaLocation="
              http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">


 <context:property-placeholder location="classpath:foo.properties,classpath:bar.properties"/>

    </bean>

</beans>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-15
    • 2015-03-22
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-05-27
    • 1970-01-01
    相关资源
    最近更新 更多