Spring properties file as xml

后端 未结 2 738
情话喂你
情话喂你 2021-01-06 02:08

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



        
相关标签:
2条回答
  • 2021-01-06 02:26

    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>
    
    0 讨论(0)
  • 2021-01-06 02:36

    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;
    
    0 讨论(0)
提交回复
热议问题