Java 使用 Spring 覆盖外部属性文件中的属性(如果存在)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21015278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:57:28  来源:igfitidea点击:

Override properties from external property file if exists using Spring

javaspringproperties

提问by Saurab Parakh

I have the following property file defined in one of my Spring configuration file:

我在我的 Spring 配置文件之一中定义了以下属性文件:

<context:property-placeholder location="classpath:project.properties"/> 

Now I want to override few properties from some external property file that is not in the classpath.

现在我想覆盖一些不在类路径中的外部属性文件中的几个属性。

Let's say I have my project deployed somewhere and I need to some dynamic configuration change. I do not want to make updates to the project codebase in the container(tomcat or any thing).

假设我将我的项目部署在某个地方,我需要进行一些动态配置更改。我不想更新容器(tomcat 或任何东西)中的项目代码库。

1.) So I need a way that updates (overrides) the values of spring's loaded properties file with my recent updates in the external property file.

1.) 所以我需要一种方法来更新(覆盖)spring 加载的属性文件的值,以及我最近在外部属性文件中的更新。

2.) It would be great if somebody could also share the way to refresh the properties that are preloaded.

2.) 如果有人也可以分享刷新预加载属性的方法,那就太好了。

采纳答案by René Link

So I need a way that updates (overrides) the values of spring's loaded properties file with my recent updates in the external property file.

所以我需要一种方法来更新(覆盖)spring 加载的属性文件的值,以及我最近在外部属性文件中的更新。

You can use the PropertyPlaceholderConfigurer.

您可以使用PropertyPlaceholderConfigurer

Either this way If you want to use the contextnamespace

无论哪种方式 如果您想使用context命名空间

<context:property-placeholder location="classpath:yourClasspath.properties,file:/some/resource/path/filePropertiesToOverride.properites"/> 

or this way

或者这样

<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:yourClasspath.properties</value>
            <value>file:/some/resource/path/filePropertiesToOverride.properites</value>
        </list>
    </property>
</bean>

According to the javadoc of PropertiesLoaderSupport.setLocations(Resource[])

根据PropertiesLoaderSupport.setLocations(Resource[])的 javadoc

... Note: Properties defined in later files will override properties defined earlier files, in case of overlapping keys. Hence, make sure that the most specific files are the last ones in the given list of locations.

... 注意:在重叠键的情况下,稍后文件中定义的属性将覆盖先前文件中定义的属性。因此,请确保最具体的文件是给定位置列表中的最后一个。

.

.

It would be great if somebody could also share the way to refresh the properties that are preloaded.

如果有人也可以分享刷新预加载属性的方式,那就太好了。

At the moment you are using a PropertyPlaceholderConfigurer. Since a PropertyPlaceholderConfigureris a BeanFactoryPostProcessorit traverses the bean definitions (object representation of the beans.xml) and replaces the property strings (such as ${someProp}). After that the beans get instantiated and initialized. Thus there is no way to 'reload' the properties.

目前您正在使用PropertyPlaceholderConfigurer. 由于 aPropertyPlaceholderConfigurer是 aBeanFactoryPostProcessor它遍历 bean 定义( 的对象表示beans.xml)并替换属性字符串(例如 ${someProp})。之后 bean 被实例化和初始化。因此,无法“重新加载”属性。

There is even more to consider if you want to build an application that can react to property changes at runtime:

如果您想构建一个可以在运行时对属性更改做出反应的应用程序,还需要考虑更多:

  • How can you trigger the change at Runtime? E.g. a timer that polls a property file for changes, JMX, ...?
  • How are classes that depend on properties are informed about the update? E.g. a listener implementation.
  • How do I synchronize the updates of many dependent properties? E.g. imagine what will happen if properties get updated during a web app request without synchronization. A part of the request might use the old and another the new properties.
  • 如何在运行时触发更改?例如,轮询属性文件更改的计时器,JMX,...?
  • 依赖属性的类如何获知更新?例如一个监听器实现。
  • 如何同步许多依赖属性的更新?例如,想象一下如果在没有同步的 Web 应用程序请求期间更新属性会发生什么。请求的一部分可能使用旧属性,另一部分使用新属性。

At least I would recommend to use apache commons configuration. But it is only a framework that solves a few problems and you still have to think about solutions to the questions above.

至少我会推荐使用apache commons 配置。但它只是一个框架,解决了一些问题,你仍然需要考虑以上问题的解决方案。

回答by krmanish007

in spring boot 2, it is

在spring boot 2中,它是

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

You can get full details in https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

您可以在https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html 中获取完整详细信息