Spring Boot 4 - Full Microservices Course - Home Energy Tracker - Migration Guide (Part 3)
Welcome to Part 3 of the Home Energy Tracker microservices course migration guide. In this chapter, we explore how to upgrade an existing Spring Boot application from version 3.5.7 to the newly released Spring Boot 4, adopting its modern modular design, updated starter naming conventions, and Hibernate 7 auto-dialect capabilities.
1. Why Do We Need to Upgrade? (The Spring Boot 4 Motivation)
- Cutting-Edge Tech Stack: Keeping microservices synchronized with the latest platform releases ensures security patches, performance upgrades, and compatibility with modern libraries.
- The Upgrade Trigger: Right after completing the user service code on Spring Boot
3.5.x, Spring Boot 4 was announced, prompting a clean forward-migration so all subsequent course microservices run natively on version 4. - Prerequisites Verification: Before initiating migration, the application must be verified against two foundational requirements:
- Spring Boot Version: Must be running the latest minor release of the previous generation (
3.5.x). - Java Runtime Environment: Must be running a minimum of Java 17 (the course project uses Java 21).
- Spring Boot Version: Must be running the latest minor release of the previous generation (
2. Core Architectural Changes in Spring Boot 4
Spring Boot 4 introduces a complete structural reimagining compared to previous generations, prioritizing lightweight footprints and decoupled testing.
- New Modular Design: Instead of massive monolithic starter packages bundling everything together, Spring Boot 4 splits dependencies into two clean categories:
- Core Implementation Dependency: Imports strictly what the production runtime needs.
- Dedicated Test Dependency: Separates test-scope utilities into distinct artifacts (e.g., adding
testsuffixes).
- Starter Deprecations & Renaming: Many legacy starters have been deprecated or renamed to better reflect their underlying framework wrappers (e.g.,
spring-boot-starter-webmoving tospring-boot-starter-web-mvc). - Hibernate 7 Integration: Spring Boot 4 upgrades to Hibernate 7, introducing advanced auto-detection for database dialects, drastically reducing boilerplate configuration in properties files.
3. Git Branching Strategy & Preparation
Before altering any build files or configurations in the user-service project, establish an isolated workspace branch to maintain clean version control history.
- Branch Name:
upgrade-user-service-to-spring-boot-4 - Action: Isolate all Maven dependency modifications and configuration changes away from production-ready baseline branches.
4. Upgrading pom.xml Dependencies
To transition the project from Spring Boot 3.5.7 to 4.0.0, navigate to pom.xml and systematically update the parent version, starters, and test scopes.
Before vs. After: pom.xml Evolution
Updated pom.xml Snippet
<!-- Spring Boot 4 Parent Project -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.0.0</version>
<relativePath/>
</parent>
<dependencies>
<!-- Modern Web MVC Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-mvc</artifactId>
</dependency>
<!-- Modern AspectJ Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aspect-j</artifactId>
</dependency>
<!-- Native Flyway Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-flyway</artifactId>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
5. Cleaning Up application.properties (Hibernate 7 Auto-Dialect)
With the integration of Hibernate 7 in Spring Boot 4, the framework is now intelligent enough to autodetect the database version and dialect directly from the connected datasource.
Naive vs. Clean Configuration Approach
-
Naive Approach (Spring Boot 3.x Style): Explicitly declaring cumbersome dialect versions and driver mappings:
PROPERTIESspring.jpa.database-platform=org.hibernate.dialect.MySQLDialect spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect -
Clean Approach (Spring Boot 4 Style): Letting Hibernate 7 handle introspection automatically or specifying a clean, unversioned dialect:
PROPERTIES# Hibernate 7 auto-detects dialect automatically. # Optional explicit config: spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
6. Codebase Compatibility & IntelliJ Validation
A major advantage of this specific migration path is that the existing business logic layer (UserController, UserService, logging aspects, and execution time aspects) utilized clean, modern idioms in 3.5.x.
- Zero Breaking Code Changes: Because deprecated methods were avoided during initial implementation, zero Java source files required rewriting.
- IDE Verification: Opening controllers, services, and aspect classes in IntelliJ confirms zero red underlines or deprecation warnings.
7. Execution, Verification & Runtime Smoke Testing
Once Maven synchronizes successfully and downloads the new Spring Boot 4 artifacts, execute the application to verify runtime health.
Startup & Request Flow Walkthrough
- Console Log Verification: Inspecting startup logs confirms that
Spring Boot v4.0.0is successfully initialized. - Aspect Verification: Making test
GETrequests to retrieve users successfully triggers both custom logging aspects and execution time measurement aspects in the console output.
8. Summary and Key Takeaways
- Immediate Version Alignment: Upgraded the
user-servicepom.xml parent from version3.5.7to the newly released Spring Boot 4.0.0. - Java Runtime Baseline: Confirmed the environment runs Java 21, satisfying Spring Boot 4's requirement of Java 17+.
- Modular Dependency Architecture: Replaced legacy monolithic starters with Spring Boot 4's decoupled core and test-scoped modular dependencies (e.g.,
spring-boot-starter-web-mvcandspring-boot-starter-aspect-j). - Starter Refactoring: Swapped deprecated starters such as
spring-boot-starter-webandspring-boot-starter-aopfor their modern counterparts. - Flyway Integration: Adopted the standardized
spring-boot-starter-flywaydependency to manage database migrations cleanly without manual version conflicts. - Hibernate 7 Auto-Dialect: Leveraged Hibernate 7's intelligent database autodetection, streamlining
application.propertiesand eliminating manual dialect version boilerplate. - Zero-Code Friction: Verified that cleanly written service and controller classes required zero source code modifications during the framework jump.
- Future-Proof Microservices: Established Spring Boot 4 as the baseline standard for all subsequent microservices built throughout the Home Energy Tracker course.