Unlock Spring Boot Internals: 8 Essential Tools to Inspect Your Application
This guide walks you through eight Spring Boot utilities—including ApplicationPid, ApplicationHome, JavaVersion, ApplicationTemp, SystemProperties, Instantiator, property loaders, and base package discovery—showing how to retrieve process IDs, directories, Java versions, temporary paths, system properties, instantiate beans, load resources, and inspect package information with concise code examples.
Environment: SpringBoot 3.2.5
1. Get Process ID
Use ApplicationPid to obtain the current process ID.
ApplicationPid pid = new ApplicationPid();
System.out.printf("Process ID: %s%n", pid.toString());Output example: Process ID: 24416 Alternative methods:
# Register listener in META-INF/spring.factories
org.springframework.context.ApplicationListener=\
org.springframework.boot.context.ApplicationPidFileWriterConfigure the file path:
spring:
pid:
file: d:/app.pidFile content (example):
Another way using the JMX bean:
String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];2. Application Home Directory
ApplicationHome provides the main directory of the application, handling JAR files, exploded archives, or directly run programs.
ApplicationHome home = new ApplicationHome();
System.out.printf("dir: %s, source: %s%n", home.getDir(), home.getSource());IDE run output: dir: D:\java\workspace\test-app, source: null JAR run output:
dir: D:\java\workspace\test-app\target,
source: D:\java\workspace\test-app\target\test-app-1.0.0.jar3. Get Java Version
Use JavaVersion to retrieve the Java version at runtime.
System.out.printf("Java Version: %s%n", JavaVersion.getJavaVersion());Output example: Java Version: 17 JavaVersion is an enum covering versions 17‑22; you can compare versions with isEqualOrNewerThan and isOlderThan.
4. Application Temporary Directory
ApplicationTemp gives access to a dedicated temporary directory for the Spring Boot application.
ApplicationTemp temp = new ApplicationTemp();
System.out.printf("Temp directory: %s%n", temp.getDir());Output example:
Temp directory: C:\Users\MSI-NB\AppData\Local\Temp\561929B2C764E67BCDA2DF9DAE26EF121F7E53655. System Properties / Environment Variables
Use SystemProperties to read system properties; if a property is missing, it falls back to environment variables.
System.out.printf("java.home=%s%n", SystemProperties.get("java.home"));Output example: java.home=D:\software\jre The get method accepts var‑args keys and returns the first non‑null value.
6. Instantiator – Bulk Bean Creation
Instantiator is a simple factory that creates multiple instances of a given type using supplied class names.
public interface DAO {}
public class A implements DAO {}
public class B implements DAO {}
Instantiator<DAO> instant = new Instantiator<>(DAO.class, p -> {});
List<DAO> ret = instant.instantiate(List.of("com.pack.A", "com.pack.B"));
System.out.printf("%s%n", ret);Output example:
[com.pack.A@3127cb44, com.pack.B@3234474]7. Resource Loading
Load .properties, .xml, or .yaml resources using PropertiesPropertySourceLoader and YamlPropertySourceLoader .
// Load properties file
PropertiesPropertySourceLoader propertyLoader = new PropertiesPropertySourceLoader();
List<PropertySource<?>> list = propertyLoader.load("pack", new ClassPathResource("pack.properties"));
System.out.printf("pack.*: %s%n", list.get(0).getSource());
// Load yaml file
YamlPropertySourceLoader yamlLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> yamls = yamlLoader.load("pack", new ClassPathResource("pack.yml"));
System.out.printf("pack.*: %s%n", yamls.get(0).getSource());The loaded PropertySource objects can be registered with the Environment for direct access.
8. Get Base Packages
Retrieve the base packages of the current application context using AutoConfigurationPackages .
private ConfigurableApplicationContext context;
System.out.printf("basePackages: %s%n", AutoConfigurationPackages.get(context));Output example: basePackages: [com.pack] The underlying bean is private, so this static helper is the only way to access it.
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.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
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.
