SoFunction
Updated on 2025-04-13

This article will help you understand the various usages of startup parameters in SpringBoot

When developing applications using Spring Boot, we usually need to adjust startup parameters according to different environments (development, testing, production) or specific needs, for example:

  • Modify the default port
  • Specify different configuration files
  • Control the JVM memory size
  • Pass custom parameters

So, what ways does Spring Boot provide to configure these startup parameters? Today we will analyze the various usages of Spring Boot startup parameters in detail, and accompany code examples, so that you can flexibly control the startup process of the application.

1. Common ways to pass startup parameters

Spring Boot allows us to pass startup parameters in the following ways:

Way Applicable scenarios Example
Command line parameters Suitable for temporary configuration modification java -jar --=8081
/yml Applicable to project default configuration =8081
Environment variables Suitable for Docker/K8S deployments export SERVER_PORT=8081
JVM startup parameters Suitable for tuning JVM runtime behavior -Xmx512m -=prod
() Read Suitable for custom dynamic parameters ("")

Next, we parse the usage and sample code of these methods one by one.

2. Pass the startup parameters through the command line parameters

The easiest way is to use the format of --parameter name=value when starting the jar file.

Example: Modify Spring Boot Port

java -jar  --=8081

Effect: After the application is started, the listening port will change to 8081.

If you want to pass multiple parameters, you can write this:

java -jar  --=8081 --=myapp

Note: The priority ratio of the command line parameters is higher. Even if =8080 is defined in , the 8081 passed by the command line will still overwrite it.

3. Use or configure parameters

Spring Boot provides (or ) to manage application configuration, which works for default parameter configurations.

Example:

=8082
=myapp
=INFO

Example:

server:
  port: 8082
spring:
  application:
    name: myapp
logging:
  level:
    root: INFO

advantage:

  • Suitable for long-term configurations, without passing parameters every time you start.
  • Clear configuration, centralized management of parameters of multiple environments.

shortcoming:

  • The configuration is static, and the application needs to be restarted when modifying it during runtime.
  • Unable to pass parameters dynamically.

4. Pass parameters using environment variables

In container environments such as Docker or Kubernetes (K8S), parameters are usually passed using environment variables. Spring Boot supports reading parameters from environment variables by default.

Example: Set environment variables and start the application

export SERVER_PORT=9090
export SPRING_APPLICATION_NAME=MySpringApp
java -jar 

Or in Windows cmd:

set SERVER_PORT=9090
java -jar 

Effect: Spring Boot will automatically read SERVER_PORT and take it as a value, that is, the application will run on port 9090.

5. Use JVM to start parameters to pass parameters

If we want to pass parameters at the JVM level, we can use the -D option.

Example: Specify the running environment

java -=prod -jar 

In the code, you can get it like this:

String profile = ("");
("Current environment:" + profile);

Example: Limiting JVM memory

java -Xmx512m -Xms256m -jar 
  • -Xmx512m: Maximum memory 512MB
  • -Xms256m: Initial memory 256MB

Application scenarios:

  • Suitable for parameter switching for different environments (development, testing, production).
  • Suitable for JVM-level optimization (such as GC, thread management, etc.).

6. Spring Boot code examples for reading startup parameters

In Spring Boot applications, we can read startup parameters through @Value, Environment, or ().

1. Read parameters via @Value

import ;
import ;
import ;
import ;

@RestController
@RequestMapping("/config")
public class ConfigController {

    @Value("${}")
    private String serverPort;

    @Value("${}")
    private String appName;

    @GetMapping("/info")
    public String getConfigInfo() {
        return "App: " + appName + ", Running on Port: " + serverPort;
    }
}

2. Read parameters through Environment

import ;
import ;
import ;

@RestController
public class EnvController {

    private final Environment env;

    public EnvController(Environment env) {
         = env;
    }

    @GetMapping("/env")
    public String getEnvInfo() {
        return "Profile: " + ("") +
               ", Server Port: " + ("");
    }
}

3. Read JVM-level parameters through ()

@GetMapping("/jvm")
public String getJvmParams() {
    return "JVM Param: " + ("", "default value");
}

If we start the application in the following way:

java -=myValue -jar 

Then the /jvm interface will return:

JVM Param: myValue

7. Summary: How to choose the appropriate startup parameter method

Way Applicable scenarios Is it dynamically modified Priority
Command line parameters Suitable for temporary configurations such as testing 🟢 Highest
Applicable to default configuration 🔵 Low
Environment variables Suitable for containerized deployment 🟢 High
JVM parameters Suitable for tuning JVM behavior 🟢 High

Best Practices

1. Development environment: Use as the default configuration.

2. Test environment: Pass the test configuration using command line parameters or environment variables.

3. Production environment: Use -=prod + environment variables to implement dynamic configuration management.

Master Spring Boot startup parameters, and your application will be more flexible and efficient!

This is the article about this article that will introduce you to the various usages of startup parameters in SpringBoot. For more related contents of SpringBoot startup parameters, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!