【问题标题】:Spring changing the properties file in runtimeSpring 在运行时更改属性文件
【发布时间】:2018-10-03 06:45:07
【问题描述】:

我正在使用 Spring Boot,并且我有一个属性文件 p.properties:

p1 = some val1
p2 = some val2

配置类:

@Configuration
@PropertySource("classpath:p.properties")
public class myProperties {

    public myProperties () {
        super();
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer placeholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

我正在使用它来访问该属性:

@Value("${p1}")
private String mProperty;

一切都很好。 我想从应用程序外部更改 p.properties 文件中的 p1,下次我将使用 mProperty 时,它将包含新值而无需重新启动应用程序。 有可能吗?

谢谢, 视频

【问题讨论】:

  • 动态更改属性文件是生产代码的一部分,还是您只想在开发环境中进行?
  • jeroenbellen.com/… 可能会有所帮助。
  • @ShanuGupta 它不是代码的一部分。我想从文件本身更改它。
  • 它是生产代码的一部分吗?
  • 它不是生产代码的一部分

标签: java spring spring-boot properties-file


【解决方案1】:

您可以简单地使用spring boot actuator。 只需在 maven/gradle config 中添加执行器依赖项,当您更新 property 文件时,您应该会看到实时重新加载。

注意:您不必重新启动应用程序,但执行器会自行执行live reloads

【讨论】:

    【解决方案2】:

    如果您想在运行时更改属性并且不想重新启动服务器,请按照以下步骤操作:

    1. Application.properties

      app.name=xyz

      management.endpoints.web.exposure.include=*

    2. 在 pom.xml 中添加以下依赖项

      org.springframework.boot 弹簧引导启动器执行器 org.springframework.cloud 春天云上下文 2.0.1.发布

      3) 将 application.properties 放在 config 文件夹中。 config 文件夹必须位于运行 jar 的位置。

    3. 添加 ApplcationProperties.java

      @RefreshScope @零件 @ConfigurationProperties(前缀=“应用程序”) 公共类 ApplcationProperties { 私有字符串名称; //getter 和 setter }

    4. 编写 ApplicationController.java 并注入 ApplcationProperties

      @自动连线 应用程序属性应用程序属性;

      @RestController 公共类应用控制器 { @GetMapping("/find") 字符串获取值() { 返回应用程序属性.getName(); } }

    5. 运行 Spring Boot 应用程序

    6. 从您的浏览器拨打localhost:XXXX/find

      输出:xyz

    7. 将 application.properties 中的值从 xyz 更改为 abc

    8. 使用邮递员发送一个 put /options 请求到localhost:XXXX/actuator/refresh --注意这个请求应该是PUT/OPTIONS

    9. 从您的浏览器拨打localhost:XXXX/find

      输出:abc

    【讨论】:

      【解决方案3】:

      我认为,在这种情况下,建议将其保存在数据库中,以便可以无缝更改和访问它。我们有一个类似的场景,我们将数据库的加密密码保存在属性文件中。在连接到 db 时,需要对其进行解密。我们通过扩展PropertyPlaceholderConfigurer 来做到这一点,如下所示。

      public class MyPropertyConfigurer extends PropertyPlaceholderConfigurer{
        protected void convertProperties(Properties props){
              Enumeration<?> propertyNames = props.propertyNames();
              while (propertyNames.hasMoreElements()) {
                  String propertyName = (String) propertyNames.nextElement();
                  String propertyValue = props.getProperty(propertyName);
                  if(propertyName.indexOf("db.password") != -1){
                      decryptAndSetPropValue(props,propertyName,propertyValue);
                  }
              }
          }
      }
      

      但是,这仅在加载属性文件时执行一次。

      【讨论】:

      • 如果我们在运行时更改加密密码怎么办?我们需要重启应用吗?
      • 如果您在运行时更改属性文件中的密码,则需要重新启动。如答案中所述,属性文件仅在应用程序启动时加载和读取一次。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2015-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      相关资源
      最近更新 更多