Java Spring Boot Deployment
In this page:
Fat Jar Executables
Spring Boot builds fat executable jars that bundle your application's compiled bytecode together with an embedded servlet web server (Tomcat by default). This means a single jar file is enough to run the app anywhere a JVM is installed, with no separate application-server setup required.
Example: Fat Jar Executables
public class Main {
public static void main(String[] args) {
// mvn package produces target/app.jar bundling Tomcat inside -- run with: java -jar app.jar
System.out.println("A single jar bundles the app and an embedded server");
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Profiles
Profiles let you separate configuration between environments -- lightweight, permissive settings for local development versus stricter, high-security settings for staging or production -- and Spring Boot activates the right property set automatically based on which profile is active.
Example: Dynamic Profiles
public class Main {
public static void main(String[] args) {
String activeProfile = System.getProperty("spring.profiles.active", "dev"); // dev vs prod settings
System.out.println("Active profile: " + activeProfile);
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Environment Configuration
Injecting configuration through environment variables lets you change database URLs, API keys, or feature flags at deploy time without rebuilding the application, which is essential for keeping secrets and per-environment settings out of your source code.
Example: Dynamic Environment Configuration
public class Main {
public static void main(String[] args) {
String dbUrl = System.getenv().getOrDefault("DATABASE_URL", "jdbc:default"); // changed at deploy time, no rebuild
System.out.println(dbUrl);
}
}
Login to try C/C++/Java/PHP code in the editor
Actuator Monitoring
Spring Boot Actuator exposes built-in monitoring endpoints that report on application health, active thread counts, and other performance indicators, giving operators visibility into a running service without needing custom instrumentation code.
Example: Actuator Monitoring
public class Main {
public static void main(String[] args) {
// GET /actuator/health, /actuator/metrics -- built-in monitoring endpoints
System.out.println("Actuator exposes health and metrics without custom instrumentation");
}
}
Login to try C/C++/Java/PHP code in the editor
Docker Containerization
Packaging a fat executable jar into a Docker image is the standard way to prepare a Spring Boot app for cloud-native, containerized deployment pipelines, since the resulting image needs nothing installed on the host besides a container runtime.
Example: Docker Containerization
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
String dockerfile = "FROM eclipse-temurin:17-jre\nCOPY app.jar app.jar\nENTRYPOINT [\"java\",\"-jar\",\"app.jar\"]";
// Print a line to the console
System.out.println(dockerfile);
}
}
Login to try C/C++/Java/PHP code in the editor
- Building a jar without the
spring-boot-maven-plugin, sojava -jarfails with 'no main manifest attribute' because it is not an executable fat jar. - Leaving all Actuator endpoints exposed publicly, which can leak internal details; expose only what you need and secure it.
- Hard-coding database URLs and passwords in
application.propertiesinstead of using environment variables or profiles for each environment.
- Spring Boot simplifies building Java applications and provides a quick way to set up a project.
- You can build REST APIs, work with databases through Spring Data JPA, and add security.
- Spring Boot also covers testing and deployment.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: