How do I configure Spring to partially and optionally override properties?

后端 未结 2 1371
有刺的猬
有刺的猬 2021-02-11 04:25

I would like to have a properties setup which can, on certain environments, override specific properties. For example, our default JDBC properties for dev are:

  • db
2条回答
  •  伪装坚强ぢ
    2021-02-11 04:52

    I had a similar issue with overwriting already existing properties in integration tests

    I came up with this solution:

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringApplicationConfiguration(classes = {
        SomeProdConfig.class,
        MyWebTest.TestConfig.class
    })
    @WebIntegrationTest
    public class MyWebTest {
    
    
        @Configuration
        public static class TestConfig {
    
            @Inject
            private Environment env;
    
    
            @PostConstruct
            public void overwriteProperties() throws Exception {
                final Map systemProperties = ((ConfigurableEnvironment) env)
                    .getSystemProperties();
                systemProperties.put("some.prop", "test.value");
            }
        }
    

提交回复
热议问题