How to disable command line properties in spring boot How to disable command line properties in spring boot

Spring Boot Application converts any command line arguments starting with --, such as --spring.profiles.active=dev to a property by default and adds them to the Spring Environment. Command line properties always take precedence over other property sources.

If you do not want command line properties to be added to the Environment, you can disable them from SpringBootApplication main method as follows:-

@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {

        SpringApplication app = new SpringApplication(SpringBootDemoApplication.class);
        app.setAddCommandLineProperties(false);
        app.run(args);
    }
}

setAddCommandLineProperties(false) must be called before app.run(args) — it configures how SpringApplication builds its Environment, so it has no effect if set afterwards.

Why This Property Source Has Such High Precedence

Under the hood, --key=value arguments are parsed into a PropertySource named commandLineArgs. Spring Boot deliberately gives it one of the highest precedence levels among all externalized configuration sources — higher than application.properties, application.yml, and OS environment variables. That ordering is intentional: it lets you override any configured value at deploy or launch time (--server.port=9090, --spring.profiles.active=prod) without repackaging the jar or touching a config file baked into it.

Why You Might Disable It

  • A CLI tool with its own arguments. If your Spring Boot app is a command-line tool that takes its own positional or --flag-style arguments unrelated to Spring configuration, you may not want every -- argument silently reinterpreted as a Spring property.
  • Locking down configuration in a sensitive environment. If operators launch the jar with a fixed set of arguments and you want to guarantee no one can override, say, spring.datasource.url from the command line, disabling this source closes that path.
  • Avoiding accidental overrides. Because command-line properties beat everything else, a stray --server.port=... passed by whatever process launches the app (a shell script, a supervisor, a CI runner) can silently override a value you expect to come from application.yml. Disabling it removes that surprise entirely.

The Gotcha

This setting is all-or-nothing — it disables every -- argument as a property, not selected ones. That includes properties you’d normally rely on, like --spring.profiles.active=prod. If you disable command-line properties and then deploy with a script that activates profiles via --spring.profiles.active, that flag is now silently ignored rather than raising an error — the application just starts with the wrong (or default) profile. If you still need profile selection or other overrides at launch time after disabling this, use an environment variable (SPRING_PROFILES_ACTIVE=prod) or a JVM system property (-Dspring.profiles.active=prod) instead — both are separate property sources unaffected by this setting.