xml Spring配置,使用属性文件中的值

对于通过使用属性文件中的值的设置器在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'

aiai0525 回答:xml Spring配置,使用属性文件中的值

根据 PropertyPlaceholderConfigurer

的Javadocs
  

自5.2起;采用   org.springframework.context.support.PropertySourcesPlaceholderConfigurer   而是通过利用   环境 PropertySource 机制

您可以加载如下所示的属性,您必须在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>
本文链接:https://www.f2er.com/3136108.html

大家都在问