How can I override Spring Boot application.properties programmatically?

后端 未结 10 1717
失恋的感觉
失恋的感觉 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:04

    Just to provide another option to this thread for reference as when I started to look for an answer for my requirement this came high on the search list, but did not cover my use case.

    I was looking to programmatically set spring boot property at start up, but without the need to work with the different XML/Config files that spring supports.

    The easiest way is to set the properties at the time the SpringApplication is defined. The basic example below sets the tomcat port to 9999.

    @SpringBootApplication
    public class Demo40Application{
    
        public static void main(String[] args){
            SpringApplication application = new SpringApplication(Demo40Application.class);
    
            Properties properties = new Properties();
            properties.put("server.port", 9999);
            application.setDefaultProperties(properties);
    
            application.run(args);
        }
    }
    

提交回复
热议问题