0

Can Spring's PropertyPlaceholderConfigurer class read an entire properties file and resolve all the placeholders automatically for you? I know that the normal method of doing this is to create a PropertyPlaceholderConfigurer bean and do this when the application context starts up, but I'm in a situation where I need to do it before I start up the context.

I was hoping for a convenience method like

// Return filtered properties from user input file
Properties getProperties(FileInputStream myPropertiesFile);

I don't see anything like that in the Spring docs but I can't tell if that's because I'm looking in the wrong place or it doesn't exist.

4

1 回答 1

0

Do you simply want to create an instance of the PropertyPlaceholderConfigurer give it the location of a load of resource files and then have it create a Properties instance containing all properties that it found?

If that is the case there is a protected method that you could potentially use.

/**
 * Return a merged Properties instance containing both the 
 * loaded properties and properties set on this FactoryBean.
 */
protected Properties mergeProperties() throws IOException

So you could create your own custom PropertyPlaceholderConfigurer and then use this method to return all properties. i.e.

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    public Properties getAllProperties() throws IOException {
        return super.mergeProperties();
    }
}

There might be a more efficient way to do this, but I couldn't find one at the moment. If I find anything else will let you know.

于 2012-08-14T21:13:49.183 回答