Master Spring Boot Startup Parameters: Tips, Options, and Best Practices
Learn how to launch Spring Boot applications with custom JVM and Spring options, understand common parameters like server port, profiles, and property overrides, explore code examples for retrieving values, and grasp the loading order and priority of configuration sources.
Spring Boot Startup Parameters
After completing Java web development, traditional projects are packaged as WAR files and deployed to containers such as Tomcat or Jetty. Spring Boot applications are packaged as executable JARs that embed Tomcat, allowing the same effect with a simpler startup command.
java [options] -jar *.jar [arguments]Common Configurations
--server.port : specify application port.
--spring.profiles.active : set active profile.
--spring.config.additional-location : additional config file path.
--Xms : JVM initial heap size.
--Xmx : JVM maximum heap size.
--XX:PermSize : JVM permanent generation size.
--XX:MaxPermSize : JVM maximum permanent generation size.
--Xdebug : enable remote JDWP debugging.
-D : define system properties.
Options
Options can override system property values when passed on the command line. java -Dfile.encoding=UTF-8 -jar app.jar Retrieve the value in code:
String fileEncoding = System.getProperties("file.encoding"); // UTF-8Values supplied via startup parameters have higher priority than those in the system.
Arguments
Configuration is usually placed in application.yml inside the JAR. In production you can change the configuration file location with --spring.config.location=/application.yml or override specific items:
java -Dfile.encoding=UTF-8 -jar app.jar --server.port=8080Access arguments in the main method:
log.info(">>>>> args: {}", Arrays.toString(args));Priority
Configuration sources are applied in the following order (higher overrides lower):
Startup parameters
System properties (set via -D)
Environment variables
Application configuration files (application.yml, properties)
Default Spring Boot settings
EnvironmentAware
Spring provides the EnvironmentAware interface to access configuration values without worrying about their origin.
public class MyService implements ApplicationContextAware, EnvironmentAware {
@Override
public void setEnvironment(Environment environment) {
// Can read system properties or env data
log.info(">>>>>> From system property: {}", environment.getProperty("file.encoding"));
}
}During startup, Spring loads property sources via
ConfigurableEnvironment environment = prepareEnvironment(...);and adds systemProperties and systemEnvironment sources.
public class StandardEnvironment extends AbstractEnvironment {
public static final String SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME = "systemEnvironment";
public static final String SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME = "systemProperties";
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}
}Values can also be injected with SpEL:
@Value("#{systemProperties['file.encoding']}") private String fileEncoding;
@Value("#{systemEnvironment['JAVA_HOME']}") private String javaHome;Configuration Loading Order
Default Spring Boot configuration (inside the JAR).
User-defined configuration files on the classpath.
Command line arguments.
Environment variables.
System properties.
Spring Boot first loads PropertiesPropertySourceLoader then YamlPropertySourceLoader, so properties files are read before YAML files.
Conclusion
Understanding Spring Boot’s configuration loading and parameter priority helps resolve issues where settings do not take effect and enables rapid configuration changes.
Avoid defining the same property name with different values across multiple configuration files, as later files override earlier ones.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
