Spring属性文件为xml

时间:2011-12-14 06:31:51

标签: spring

我有属性文件 config.properties ,其中存储了一些应用程序范围的属性。我使用属性占位符导入它:

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

我需要在XML文件中存储属性以传递一些XML模式验证。 我的问题是如何在春天导入XML文件作为属性文件,?

谢谢, 阿尔森

2 个答案:

答案 0 :(得分:6)

PropertyPlaceholderConfigurer已通过DefaultPropertiesPersister

支持xml属性文件

属性的xml文件格式如下。

   <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
        <entry key="key1">Value 1</entry>
        <entry key="key2">Value 2</entry>
    </properties>

你可以使用

  <context:property-placeholder 
  location="classpath:/com/myProject/spring_prop.xml" />
      <bean id="bean" class="org.MyBean">
         <property name="key1" value="${key1}" />
      </bean>

答案 1 :(得分:3)

除了这里的另一个答案,我还看到xml属性直接作为命名属性文件加载:

spring文件包含:

<util:properties id="myXmlProps" location="classpath:/com/myProject/spring_prop.xml" />

然后可以通过spring表达式语言访问它:

"#{myXmlProps['key1']}"

并在以下类中注入Strings:

@Value("#{myXmlProps['key1']}")
private String aValueForKey1;