Spring Boot Multi‑Environment Configuration and JVM Parameter Tuning Guide
This article demonstrates how to configure Spring Boot for multiple environments, retrieve custom parameters, package and run the application, modify startup settings via system variables or command‑line arguments, and explains the priority and detailed JVM options for optimizing a Spring Boot service.
Purpose
The goal is to become familiar with Spring Boot multi‑environment configuration and understand the meaning of parameters when launching a Spring Boot JAR.
Configuration Files
# springboot multi‑environment configuration
server:
port: 8080
servlet:
context-path: /springboot-params-demo
spring:
profiles:
active: test
logging:
level:
root: INFO
org:
springframework:
security: WARN
web: ERROR
file:
path: ./logs
name: './logs/springboot-params-demo.log'
pattern:
file: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n'
console: '%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}:%L - %msg%n' # application-test.yml
myParam: 'on' # application-prod.yml
myParam: 'close'Retrieving Custom Parameters
package com.demo.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Component
@Slf4j
public class MyParamRunner implements CommandLineRunner {
@Value("${myParam}")
String myParam;
@Autowired
Environment environment;
@Override
public void run(String... args) throws Exception {
log.info("本地设置的参数myParam为:{}", myParam);
log.info(environment.toString());
log.info(String.valueOf(Arrays.asList(environment.getActiveProfiles())));
}
}Packaging and Running
After building, the JAR springboot-jvm-params-1.0-SNAPSHOT.jar is placed in the target directory. Run it with:
java -jar /usr/local/springboot_demos/springboot-port8001/springboot-jvm-params-1.0-SNAPSHOT.jarModifying Startup Configuration
Two ways to set the active profile:
System variable:
java -jar -Dspring.profiles.active=prod /usr/local/springboot_demos/springboot-port8001/springboot-jvm-params-1.0-SNAPSHOT.jarCommand‑line argument:
java -jar /usr/local/springboot_demos/springboot-port8001/springboot-jvm-params-1.0-SNAPSHOT.jar --spring.profiles.active=prod --myParam='test'Note that command‑line arguments override JVM system variables, which in turn override values from configuration files (priority: file < < JVM < < command line).
JVM Startup Parameters Explained
The article lists extensive JVM options such as heap settings, GC logging, CMS collector tuning, thread counts, network timeouts, and Spring‑specific properties. Example snippet:
/usr/local/jdk/jdk1.8.0_261/bin/java -jar -server \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/usr/local/springboot_demos/springboot-port8001/dump/heap/oom.hprof \
-Djava.io.tmpdir=/usr/local/springboot_demos/springboot-port8001/tmp/ \
-Dserver.port=8001 \
-XX:+PrintGCDetails \
-XX:+PrintGCDateStamps \
-Dspring.profiles.active=prod \
/usr/local/springboot_demos/springboot-port8001/springboot-jvm-params-1.0-SNAPSHOT.jar jvmparamsThese options control memory allocation, garbage‑collection behavior, logging, and other runtime characteristics, enabling fine‑grained performance tuning for Spring Boot services.
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.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.
