SoFunction
Updated on 2025-04-14

Spring Boot Startup Parameters How to Elegantly Control Your Application (Latest Recommendations)

Spring Boot startup parameters ultimate analysis: How to control your application gracefully?

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 startjarWhen using the file--parameter name=valueto pass parameters.

Example: Modify Spring Boot Port

java -jar  --=8081

Effect: After the application is started, the listening port will become8081

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

java -jar  --=8081 --=myapp

Notice: Priority ratio of command line parametersHigher, evenDefined in=8080, passed on the command line8081It will still be covered.

3. Use or configure parameters

Spring Boot provides(or) to manage application configuration, this method is suitable for default parameter configuration.

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, centrally manage parameters of multiple environments.

shortcoming

  • The configuration isStatic, Runtime modification requires restarting the application.
  • 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 on Windowscmdmiddle:

set SERVER_PORT=9090
java -jar 

Effect: Spring Boot will automatically readSERVER_PORTAnd take it asThe value of the application will run on9090port.

5. Use JVM to start parameters to pass parameters

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

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 in 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 app, we can@ValueEnvironmentor()Read the startup parameters.

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 

So/jvmThe 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

Summary: Best Practices

1️⃣ Development environment: UseAs 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 article about Spring Boot boot parameters ultimate analysis: How to elegantly control your application. This is all about this article. For more related Spring Boot boot parameters, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!