6

I have properties file config.properties where are stored some application wide properties. And I have imported it using property placeholder:

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

I need to store properties in XML file to pass some XML schema validations. My question is how to import XML file as properties file in spring,?

Thanks, Arsen

Arsen Alexanyan
  • 3,061
  • 5
  • 25
  • 45

2 Answers2

7

PropertyPlaceholderConfigurer already supports xml property files via the DefaultPropertiesPersister

The xml file format for the properties is as below.

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

you can use

  <context:property-placeholder 
  location="classpath:/com/myProject/spring_prop.xml" />
      <bean id="bean" class="org.MyBean">
         <property name="key1" value="${key1}" />
      </bean>
Aravind A
  • 9,507
  • 4
  • 36
  • 45
4

In addition to the other answer here, I have also seen xml properties loaded directly as named properties files:

The spring file contains:

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

This can then be accessed via springs expression language as:

"#{myXmlProps['key1']}"

And injected into Strings in classes with:

@Value("#{myXmlProps['key1']}")
private String aValueForKey1;
Carl Pritchett
  • 674
  • 6
  • 13