Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring在运行时更改属性文件_Java_Spring_Spring Boot_Properties File - Fatal编程技术网

Java Spring在运行时更改属性文件

Java Spring在运行时更改属性文件,java,spring,spring-boot,properties-file,Java,Spring,Spring Boot,Properties File,我正在使用Spring Boot,我有一个属性文件p.properties: p1 = some val1 p2 = some val2 配置类: @Configuration @PropertySource("classpath:p.properties") public class myProperties { public myProperties () { super(); } @Bean public static Property

我正在使用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时,它将包含新值,而无需重新启动应用程序。 可能吗

谢谢,
Avi

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

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);
            }
        }
    }
}
公共类MyPropertyConfigure扩展了PropertyPlaceHolderConfigure{
受保护的属性(属性道具){
枚举propertyNames=props.propertyNames();
while(propertyNames.hasMoreElements()){
String propertyName=(String)propertyNames.nextElement();
字符串propertyValue=props.getProperty(propertyName);
if(propertyName.indexOf(“db.password”)!=-1){
decryptAndSetPropValue(props、propertyName、propertyValue);
}
}
}
}

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

您只需使用
弹簧启动执行器即可。
只需在
maven/gradle config
中添加执行器依赖项,当您更新
属性
文件时,应该会看到实时重新加载


注意:您不必重新启动应用程序,但actuator将自行执行
实时重新加载操作。

如果您想在运行时更改属性,但不想重新启动服务器,请执行以下步骤:

  • 应用程序属性

    app.name=xyz

    management.endpoints.web.exposure.include=*

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

    org.springframework.boot 弹簧靴起动器执行器 org.springframework.cloud spring云上下文 2.0.1.1发布 3) 将application.properties放在config文件夹中。config文件夹必须位于运行jar的位置

  • 添加ApplcationProperties.java

    @刷新范围 @组成部分 @配置属性(prefix=“app”) 公共类applicationproperties{ 私有字符串名称; //接二连三 }

  • 编写ApplicationController.java并注入ApplcationProperties

    @自动连线 ApplcationProperties ApplcationProperties

    @RestController 公共类应用程序控制器 { @GetMapping(“/find”) 字符串getValue() { 返回applicationProperties.getName(); } }

  • 运行spring引导应用程序

  • 从浏览器中调用
    localhost:XXXX/find

    输出:xyz

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

  • 使用邮递员向
    localhost:XXXX/exactor/refresh发送看跌期权请求
    --注:此请求应为卖出/期权

  • 从浏览器中调用
    localhost:XXXX/find

    产出:abc


  • 动态更改属性文件是生产代码的一部分,还是您只想在开发环境中这样做?可能有帮助。@ShanuGupta它不是代码的一部分。我想从文件本身更改它。它是生产代码的一部分吗?它不是生产代码的一部分吗?如果我们在运行时更改加密密码会怎么样?是否需要重新启动应用程序?如果您在运行时更改属性文件中的密码,则需要重新启动。正如答案中所指定的,当应用程序启动时,属性文件只加载并读取一次。