How can I override Spring Boot application.properties programmatically?

后端 未结 10 1660
失恋的感觉
失恋的感觉 2020-11-29 18:33

I have jdbc property files which I take from external configuration web-service In spring boot in order to set mysql props it\'s easy as adding those to application.properti

10条回答
  •  渐次进展
    2020-11-29 19:13

    This is how you can set properties during startup if you are running spring boot application.

    The easiest way is to set the properties before you even started an app.

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication application = new SpringApplication(Application.class);
            ConfigurableEnvironment env = new ConfigurableEnvironment();
            env.setActiveProfiles("whatever");
    
            Properties properties = new Properties();
            properties.put("server.port", 9999);
            env.getPropertySources()
                .addFirst(new PropertiesPropertySource("initProps", properties));
    
            application.setEnvironment(env);
            application.run(args);
        }
    }
    

提交回复
热议问题