Java 使用属性文件中的值进行xml Spring配置

Java 使用属性文件中的值进行xml Spring配置,java,xml,spring,dependency-injection,properties,Java,Xml,Spring,Dependency Injection,Properties,对于xml中的DI,通过setter使用属性文件中的值,我可以使用: <beans> .. <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="props.properties"/> </bean> <bean id="f

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

<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>
而不是它,但它不起作用


prop.properties
位于
resources
中,错误消息是
根据PropertyPlaceholderConfigurer的Javadocs,找不到元素“context:property placeholder”的声明

截至5.2;使用 org.springframework.context.support.propertySourcesplacePlaceholderConfigurer 相反,通过利用 环境属性源机制

您可以像下面这样加载属性,您必须在bean标记中包含名称空间和模式位置。从下面复制bean标记

 <?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>

将答案张贴在下面,让我们知道这是否有效
 <?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>