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
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>
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With