Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot: Make property non-configurable

In Spring Boot I have several options to externalize my configuration. However, how can I make such properties non-configurable, i.e. readonly.

Concretly, I want to set server.tomcat.max-threads to a fixed value and do not want somebody who is going to start the application to have the ability to change it. This could easily be done by passing it as a command line argument for instance.

It's probably not possible by default, maybe someone could suggest workarounds?

like image 210
s1m0nw1 Avatar asked Jan 19 '26 18:01

s1m0nw1


1 Answers

You have 2 options

  1. Set System.setProperty("prop", "value") Property hard coded
  2. Use properties that will override all other properties

  3. Set system property hard coded

        public static void main(String[] args) {
          System.setProperty("server.tomcat.max-threads","200");
          SpringApplication.run(DemoApplication.class, args);
        }
    
  4. Properties in secure.properties will override all others (see, Prevent overriding some property in application.properties - Spring Boot)

    @Configuration
    public class SecurePropertiesConfig {
    
    @Autowired
    private ConfigurableEnvironment env;
    
    @Autowired
    public void setConfigurableEnvironment(ConfigurableEnvironment env) {
      try {
        final Resource resource = new 
        ClassPathResource("secure.properties");
        env.getPropertySources().addFirst(new 
            PropertiesPropertySource(resource.getFilename(), 
            PropertiesLoaderUtils.loadProperties(resource)));
      } catch (Exception ex) {
        throw new RuntimeException(ex.getMessage(), ex);
      }
    }
    
like image 150
olahell Avatar answered Jan 21 '26 06:01

olahell