Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Stacks and tools

Spring Boot Best Practices

Opens with credits

A practical guide to Spring Boot best practices for Java developers building REST services. It covers versions and the new modular starters, dependency injection and structure, typed configuration, the web layer and error responses, JPA and transactions, virtual threads, securing Actuator, testing with slices and Testcontainers, and running cleanly in production.

Written by Tauseef Fayyaz9 chapters, 37 practicesAbout 21 min31 quiz questionsReviewed 13 October 2026

What you will learn

9 chapters · about 21 min

  1. 1Versions, starters and dependenciesWhich Spring Boot and Java versions to run, how the modular starters in Spring Boot 4 work, and how to keep dependency versions consistent.4 practices · 3 min
  2. 2Structure and dependency injectionHow to organise packages and wire beans so the code stays testable and the application context stays easy to reason about.4 practices · 2 min
  3. Configuration and profilesHow to bind settings into typed, validated objects, keep secrets out of the repository and use profiles without creating a separate app per environment.4 practices · 2 minOpens with credits
  4. The web layer: DTOs, validation and errorsHow to shape requests and responses, validate input, return consistent error bodies and call other services without hanging.5 practices · 3 minOpens with credits
  5. Data access with JPA and transactionsThe JPA defaults and patterns that cause most production performance and correctness problems, and what to use instead.5 practices · 3 minOpens with credits
  6. Virtual threads and performanceWhen to switch on virtual threads, what changes when you do, and the settings that matter more than any code tweak.3 practices · 2 minOpens with credits
  7. Security and ActuatorHow to keep Spring Security on and Actuator endpoints from becoming the easiest way into your service.4 practices · 2 minOpens with credits
  8. TestingHow to test Spring Boot apps quickly: unit tests without Spring, slice tests for one layer, and full tests against real services in containers.4 practices · 2 minOpens with credits
  9. Running in productionShutdown, health probes, observability and container images: the settings that decide how a Spring Boot service behaves on deploy day.4 practices · 2 minOpens with credits

About this guide

Spring Boot best practices are mostly about knowing what the framework does for you, and where its defaults stop being the right answer. Boot makes a working REST service a few annotations away, and the same convenience hides a lot: an open session that lazy-loads data during JSON rendering, @Transactional that silently does nothing on an internal call, or an Actuator endpoint that hands out every environment variable once someone widens the exposure list.

This guide starts with the setup: a supported Spring Boot and Java version and the modular starters that arrived in Spring Boot 4. It then covers constructor injection and package structure, typed configuration with @ConfigurationProperties, the web layer and RFC 9457 error responses, data access with JPA and transactions, virtual threads and connection pools, securing Actuator, testing with slices and Testcontainers, and shutting down cleanly in production.

Everything here was checked against the Spring Boot 4.1 reference documentation (current release 4.1.1), which requires Java 17 and supports up to Java 26, and against the Spring Boot 4.0 migration guide. If you are still on 3.x, the version chapter explains the upgrade path.

Common questions

What is the latest version of Spring Boot?

As of October 2026 the current release is Spring Boot 4.1.1. It requires Java 17 or later, supports up to Java 26 and builds on Spring Framework 7. Check spring.io for the current version before starting a project.

Should I use field injection or constructor injection in Spring?

Use constructor injection. Dependencies are visible in the constructor, fields can be final and tests can create the class without Spring. A class with a single constructor does not need @Autowired.

What replaces @MockBean in Spring Boot 4?

Spring Boot 4 removed @MockBean and @SpyBean. Use @MockitoBean and @MockitoSpyBean from Spring Framework on test class fields. For plain unit tests, use Mockito @Mock with MockitoExtension.

Should I turn off spring.jpa.open-in-view?

Usually yes. It is on by default in web apps and lets lazy loading run during view rendering, which hides extra queries and holds a database connection for the whole request. Turn it off and load what each response needs inside the service.

Are virtual threads worth enabling in Spring Boot?

For services that mostly wait on databases and HTTP calls, often yes. Set spring.threads.virtual.enabled=true on Java 21 or later; the Boot docs strongly recommend Java 24 or later. Then size the connection pool and downstream limits carefully, since thread pools no longer cap concurrency.

Which Actuator endpoints are safe to expose?

By default only health is exposed over HTTP. Add only what you use, such as info and a metrics endpoint, preferably on a separate management port. Never expose env, heapdump or * on a public port.

Sources (8)

Checked against these, last on 13 October 2026.

  1. System Requirements, Spring Boot docsdocs.spring.io/spring-boot/system-requirements.html
  2. Spring Boot 4.0 Migration Guide, Spring Bootgithub.com/spring-projects/spring-boot/wiki/Spring-Boot-4.0-Migration-Guide
  3. SpringApplication, Spring Boot docsdocs.spring.io/spring-boot/reference/features/spring-application.html
  4. Servlet Web Applications, Spring Boot docsdocs.spring.io/spring-boot/reference/web/servlet.html
  5. SQL Databases, Spring Boot docsdocs.spring.io/spring-boot/reference/data/sql.html
  6. Endpoints, Spring Boot docsdocs.spring.io/spring-boot/reference/actuator/endpoints.html
  7. Testcontainers, Spring Boot docsdocs.spring.io/spring-boot/reference/testing/testcontainers.html
  8. Graceful Shutdown, Spring Boot docsdocs.spring.io/spring-boot/reference/web/graceful-shutdown.html

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com