Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring properties file as xml

Tags:

spring

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

like image 775
Arsen Alexanyan Avatar asked Dec 14 '11 06:12

Arsen Alexanyan


2 Answers

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>
like image 126
Aravind A Avatar answered Oct 19 '22 21:10

Aravind A


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;
like image 45
Carl Pritchett Avatar answered Oct 19 '22 23:10

Carl Pritchett