Spring的PropertyPlaceholderConfigurer可以在代码中读取整个属性文件,而不是bean吗?

时间:2012-08-14 20:24:29

标签: java spring

Spring的PropertyPlaceholderConfigurer类可以读取整个属性文件并自动解析所有占位符吗?我知道这样做的常规方法是创建一个PropertyPlaceholderConfigurer bean,并在应用程序上下文启动时执行此操作,但是我需要在启动上下文之前执行此操作。

我希望有一个方便的方法,比如

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

我在Spring文档中没有看到类似的东西,但是我不知道是不是因为我在错误的地方看到它或者它不存在。

1 个答案:

答案 0 :(得分:0)

您是否只想创建PropertyPlaceholderConfigurer的实例,为其提供一堆资源文件的位置,然后让它创建一个包含它找到的所有属性的Properties实例?

如果是这种情况,则可以使用protected方法。

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

因此,您可以创建自己的自定义PropertyPlaceholderConfigurer,然后使用此方法返回所有属性。即。

public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

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

可能有更有效的方法来做到这一点,但我目前找不到一个。如果我发现其他任何事情会让你知道。