Spring Boot: Use database and application.properties for configuration

后端 未结 7 1532
无人及你
无人及你 2020-12-29 15:52

I need to save the configuration of the Spring Boot application in the database.

Is it possible to store the database information in the application.properties

7条回答
  •  臣服心动
    2020-12-29 16:11

    One possible solution that you could workout, is to use ConfigurableEnvironment and reload and add properties.

    @Configuration   
    public class ConfigurationPropertySource {
    
    private ConfigurableEnvironment env;
    
    private final ConfigurationRepository configurationRepository;
    
        @Autowired
        public ConfigurationPropertySource(ConfigurationRepository configurationRepository) {
            this.configurationRepository = configurationRepository;
        }
    
        @Autowired
        public void setConfigurableEnvironment(ConfigurableEnvironment env) {
    
            this.env = env;
       }
    
       @PostConstruct
       public void init() {
        MutablePropertySources propertySources = env.getPropertySources();
           Map myMap = new HashMap();
           //from configurationRepository get values and fill mapp
           propertySources.addFirst(new MapPropertySource("MY_MAP", myMap));
       }
    
    }
    

提交回复
热议问题