Java – propertyplaceholderconfigurer propertiesfactorybean only resolves location properties

I cannot have propertyplaceholderconfigurer work in my current configuration Give ApplicationContext The following code blocks in XML:

<bean id="myProperties" 
    class="org.springframework.beans.factory.config.Propertiesfactorybean">
    <property name="locations">
        <list>
            <!-- Order matters,last one to create a property wins! -->
            <value>classpath:default.properties</value>
            <value>file:${MYAPP_PROPERTIES_LOCATION:badurl}/application.properties</value>
            <value>file:${user.home}/developer.properties</value>
        </list>
    </property>
    <property name="ignoreResourceNotFound" value="true"/>

<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertiesPlaceholderConfigurer">
    <property name="properties" ref="myProperties"/>
    <property name="systemPropertiesModeName" value="SYstem_PROPERTIES_MODE_OVERRIDE"/>
    <property name="searchSystemEnvironment" value="true"/>
</bean>

Then in default In the properties file, I have the following contents (these are test properties):

property1=prop1val
property2=${property1}

Valid: propertyconfigurator correctly parses the environment variable myapp_ PROPERTIES_ Location and system variable user home. However, the final property object is created by the factory bean, and the result property is [property1 = prop1val, property2 = ${property1}]

With this configuration, how do I get the properties in the myproperties bean to resolve its placeholders? I've done a lot of research, including tracking spring code - I can see how and why not I hope there are some settings I don't know! This is my first article, so it's easy for me:)

Solution

You have the correct spring and property file configuration This is why you can read data from files Everything in spring is right. Let me explain

As described below, it is your property file,

property1=prop1val
property2=${property1}

Remember, it's a nice text file with key value pairs, and it can't accept variables Here, if you intend to dynamically copy the value of property1 to property2, this will not happen This is not the way we should use property files

The properties file should be a simple text file with key value pairs Therefore, the retention key is atomic so that you can get it from the application or ApplicationContext Logically construct the required data in the XML file

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>