Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.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 Cloud - Fatal编程技术网

Java 以编程方式更改Spring启动属性

Java 以编程方式更改Spring启动属性,java,spring,spring-cloud,Java,Spring,Spring Cloud,我正在尝试为使用@RefreshScope的应用程序编写测试。我想添加一个测试,实际更改属性并断言应用程序响应正确。我已经找到了触发刷新的方法(在RefreshScope中自动连接并调用refresh(…)),但还没有找到修改属性的方法。如果可能的话,我希望直接写入属性源(而不必处理文件),但我不确定要在哪里查找 更新 以下是我正在寻找的一个例子: public class SomeClassWithAProperty { @Value{"my.property"} privat

我正在尝试为使用
@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 SomeOtherBean otherBean;

    @Autowired
    public RefreshScope refreshScope;

    @Test
    public void testRefresh() {
        assertEquals("Hello World!", otherBean.getGreeting());

        [DO SOMETHING HERE TO CHANGE my.property TO "Mars"]
        refreshScope.refreshAll();

        assertEquals("Hello Mars!", otherBean.getGreeting());
    }
}

应用程序中使用的属性必须是带有@Value注释的变量。这些变量必须属于由Spring管理的类,就像在带有@Component注释的类中一样

如果要更改属性文件的值,可以设置不同的配置文件,并为每个配置文件设置不同的.properties文件

我们应该注意,这些文件是静态的,只需加载一次,因此以编程方式更改它们有点超出了预期用途的范围。但是,您可以在spring boot应用程序中设置一个简单的REST端点,该端点修改主机文件系统上的文件(最有可能在您正在部署的jar文件中),然后在原始spring boot应用程序上调用Refresh。

您可以这样做(我假设您错误地忽略了示例顶部的JUnit注释,因此我将为您添加它们):

但是您并没有真正测试您的代码,只测试SpringCloud的刷新范围特性(已经针对这种行为进行了广泛的测试)


我很确定您也可以从现有的刷新范围测试中获得此信息。

谢谢您的评论!我想我可能还不清楚,所以我已经更新了问题以显示我在寻找什么。可能可以使用配置文件将云配置指向属性文件并编辑该文件,但我更愿意以某种方式要么托管模拟云配置服务,要么直接拼接到加载过程中,以避免在测试期间写出属性文件。它们根本不必是
@Value
。可以在
@ConfigurationProperties
中,也可以通过
环境直接使用。也许我应该指定它处于启用状态感谢你给我指明了正确的方向——我最终使用了一个非常类似的涉及MockPropertySource的解决方案。虽然我同意这是在Spring Cloud中完全测试过的,但我想确保我们的代码正确使用了它。(在这种情况下,测试没有通过,因为@Config类正在注入值。)(通过@Value)而不是在刷新范围bean中包含值。因为我们的一些开发人员在刷新范围的文档中遇到了问题,我需要拿出一个解决方案来测试他们的代码,以确保它正确使用Spring。再次感谢!
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class MyAppIT {

    @Autowired
    public ConfigurableEnvironment environment;

    @Autowired
    public SomeOtherBean otherBean;

    @Autowired
    public RefreshScope refreshScope;

    @Test
    public void testRefresh() {
        assertEquals("Hello World!", otherBean.getGreeting());

        EnvironmentTestUtils.addEnvironment(environment, "my.property=Mars");
        refreshScope.refreshAll();

        assertEquals("Hello Mars!", otherBean.getGreeting());
    }
}