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.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Spring Boot Multi‑Environment Configuration and JVM Parameter Tuning Guide

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.jar

Modifying 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.jar

Command‑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 jvmparams

These options control memory allocation, garbage‑collection behavior, logging, and other runtime characteristics, enabling fine‑grained performance tuning for Spring Boot services.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JVMConfigurationSpring BootMulti-Environment
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.