jar 外部的 ResourceBundle 文件

pre*_*oid 3 java configuration spring-boot

我有一个 spring boot 应用程序/resources/application.properties,部署时,application.propertiesjar 旁边有一个(及其里面的)。

在我使用的代码中的某个位置ResourceBundle.getBundle("application").getString("x"),但总是返回 jar 内的属性中定义的值。

我的目标是从外部文件中获取值(如果存在),如果不存在,那么我需要从 jar 内部获取值。就像默认的弹簧行为一样,但我无法实现这一点。

编辑:

您可以使用下面正确答案中的解决方案,也可以Environment通过自动装配并使用来依赖弹簧getProperty()

dav*_*xxx 5

ResourceBundle.getBundle(String baseName)依赖类加载器而不是直接通过文件系统来查找资源。
这相当于调用重载的:

getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader()), 
Run Code Online (Sandbox Code Playgroud)

所以你的结果是预期的。
你要寻找的是PropertyResourceBundle.
要创建 的实例,您需要 aReaderInputStream.

您可以加载外部和内部文件。

ResourceBundle internalProps = ResourceBundle.getBundle("application");
ResourceBundle externalProps = new  PropertyResourceBundle(Files.newInputStream(Paths.get("application.properties"));
Run Code Online (Sandbox Code Playgroud)

您可以首先使用外部ResourceBundle,然后将第二个用作后备。