相关疑难解决方法(0)

以编程方式更改 Spring Boot 属性

我正在尝试为使用@RefreshScope. 我想添加一个实际更改属性并断言应用程序正确响应的测试。我已经想出了如何触发刷新(自动装配RefreshScope和调用refresh(...)),但我还没有想出修改属性的方法。如果可能,我想直接写入属性源(而不是必须处理文件),但我不确定在哪里查看。

更新

这是我正在寻找的示例:

public class SomeClassWithAProperty {
    @Value{"my.property"}
    private String myProperty;

    public String getMyProperty() { ... }
}

public class SomeOtherBean {
    public SomeOtherBean(SomeClassWithAProperty classWithProp) { ... }

    public String getGreeting() {
        return "Hello " + classWithProp.getMyProperty() + "!";
    }
}

@Configuration
public class ConfigClass {
    @Bean
    @RefreshScope
    SomeClassWithAProperty someClassWithAProperty() { ...}

    @Bean
    SomeOtherBean someOtherBean() {
        return new SomeOtherBean(someClassWithAProperty());
    }
}

public class MyAppIT {
    private static final DEFAULT_MY_PROP_VALUE = "World";

    @Autowired
    public …
Run Code Online (Sandbox Code Playgroud)

java spring spring-cloud

2
推荐指数
1
解决办法
9015
查看次数

标签 统计

java ×1

spring ×1

spring-cloud ×1