Java and Spring Boot¶
Scope: back-end services and web applications built with Spring Boot 4 on a Java LTS release, with Spring Security, and Spring Data JPA and Hibernate for relational data. On 2026-12-03, Spring Boot 4.2 (released on 30 November 2026), 4.1 and 4.0 have open-source support, 4.0 until 31 December 2026; they run on Spring Framework 7 and need Java 17 or later. For configuration, health checks, telemetry and resilience, follow general/operations-reliability.md; for secrets, authorization and the supply chain, general/security.md; for sign-in and sessions, general/authentication.md; for data ownership, migrations and messaging, general/data.md; for architecture tests, general/testing-delivery.md. Container images, Kubernetes, telemetry pipelines and PostgreSQL have their own folders: Docker, Kubernetes, OpenTelemetry and PostgreSQL. The rules here cover what is specific to Spring Boot.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 9 Spring Boot reference documentation (Spring): the baseline. Code structure, dependency injection, externalized configuration, profiles, data access, web and security, Actuator, observability, testing, packaging and deployment, with how-to guides for common tasks.
- 8 Spring Security reference (Spring): the baseline for security: the filter chain architecture, authentication, request and method authorization, and protection against exploits such as CSRF.
- 8 Spring Modulith reference (Spring): application modules in a Spring Boot application, verifying their structure, events between modules, module integration tests and documentation. Use it for modular monoliths.
- 8 A Short Guide to Hibernate 7 (Hibernate): mapping entities and associations, sessions and transactions, queries, and tuning performance, including association fetching and the N+1 selects problem. Spring Boot 4.1 uses Hibernate 7.4.
- 7 Spring Framework reference (Spring): the core container and dependency injection, transaction management, and the web frameworks that Spring Boot configures.
- 7 Spring Data JPA reference (Spring): repositories, query methods, projections, entity graphs and transactionality.
- 7 Spring Boot support timeline and support policy (Spring): the open-source and commercial support end date of each Spring Boot generation, and how long major and minor releases are supported.
Key rules¶
Structure and dependency injection¶
- 7
SPRING-1Inject dependencies through constructors, not into fields. The Spring Boot and Spring Framework docs recommend constructor injection: dependencies can befinal, required ones can't benull, and the object is complete when it's created. A class with a single constructor needs no@Autowired. (Spring Boot, Spring Beans and Dependency Injection, Spring Framework, Constructor-based or setter-based DI?) - 6
SPRING-2Package by feature: put the main application class in a root package, give each business capability its own subpackage, and once there are several, enforce the boundaries with Spring Modulith. Spring Modulith treats each direct subpackage of the main package as an application module, rejects references to another module's internal subpackages and cycles between modules, and checks this in a test withApplicationModules.of(Application.class).verify(). This is how Spring applications applySTYLE-5andSTYLE-7in general/architecture-styles.md. (Spring Boot, Structuring Your Code, Spring Modulith, Application Modules, Verifying Application Module Structure)
Configuration¶
- 9
SPRING-3Keep secrets out ofapplication.properties,application.yamland every other file in the repository: mount them as files and import them withspring.config.import=configtree:, or load them from a secrets manager, for example with Spring Cloud Vault. Spring Boot has no built-in encryption for property values. This is how Spring Boot applications followSEC-2in general/security.md. (Spring Boot, Using Configuration Trees, Encrypting Properties) - 6
SPRING-4Bind your own settings to@ConfigurationPropertiesclasses instead of scattering@Valueannotations, and annotate them with@Validatedand Jakarta Bean Validation constraints, so that Spring Boot validates the values when it binds them.@ConfigurationPropertiesadds full relaxed binding and configuration metadata, and gives the code one structured, type-safe object to inject. (Spring Boot, @ConfigurationProperties vs. @Value, @ConfigurationProperties Validation)
Security¶
- 9
SPRING-5Declare the authorization of every request in eachSecurityFilterChain: permit the public paths explicitly, require roles or authorities for the rest, and end withanyRequest().authenticated()oranyRequest().denyAll(). Spring Security calls denying by default a healthy practice, because it turns the rules into an allow list. A customSecurityFilterChainreplaces Spring Boot's default web security, including the protection of Actuator endpoints, so it has to cover every request. Permit static resources withpermitAll()rather than ignoring them, so that they still get security headers. This appliesSEC-1in general/security.md. (Spring Security, Authorize HttpServletRequests: matching by HTTP method, Favor permitAll over ignoring, Spring Boot, MVC Security) - 9
SPRING-6Expose only the Actuator endpoints you need over HTTP, and secure every exposed endpoint other thanhealth, with Spring Security rules built onEndpointRequestor by keeping the endpoints behind a firewall. Onlyhealthis exposed by default, because endpoints can contain sensitive information, and the Spring Boot docs strongly recommend securing the endpoints of a publicly exposed application. (Spring Boot, Exposing Endpoints, Actuator Security) - 8
SPRING-7Keep Spring Security's CSRF protection, which is on by default, for every endpoint that browsers call with cookies or HTTP Basic credentials, and exempt only paths that only non-browser clients call, such as APIs authenticated with bearer tokens, withcsrf.ignoringRequestMatchers()instead of turning the protection off withcsrf.disable(). This appliesAUTH-17, which also asks forGET,HEADandOPTIONSrequests without side effects. (Spring Security, When to use CSRF protection, CSRF and stateless browser applications, Safe methods must be read-only, Disable CSRF protection) - 7
SPRING-8Use method security (@EnableMethodSecuritywith@PreAuthorizeand@PostAuthorize) for checks that depend on a method's arguments or return value, such as ownership of a record, and keep a catch-all rule in theSecurityFilterChain. Methods without an annotation aren't secured, and Spring Boot doesn't enable method security for you. (Spring Security, Method Security, Comparing request-level and method-level authorization)
Data¶
- 8
SPRING-9Manage the schema with Flyway or Liquibase migrations, which Spring Boot runs at startup, and leavespring.jpa.hibernate.ddl-autoatnoneor set it tovalidate.noneis the default when a migration tool is present or the database isn't embedded;validatemakes Hibernate check the mappings against the migrated schema. This is how Spring Boot applications followDATA-2in general/data.md. (Spring Boot, Use a Higher-level Database Migration Tool, Initialize a Database Using Hibernate, Hibernate, Testing persistence logic) - 7
SPRING-10Map every association withfetch = LAZY, fetch the data each use case needs up front withjoin fetchor an entity graph (@EntityGraphin Spring Data JPA), and setspring.jpa.open-in-view=false. JPA fetches@ManyToOneeagerly by default, and lazy loading while code walks the results causes N+1 selects. Spring Boot turns on Open EntityManager in View by default in web applications, which lets views and serializers run such queries after the service's transaction has ended, while holding a database connection. This is how JPA appliesDATA-10. (Hibernate, Many-to-one, Association fetching, Join fetching, Spring Data JPA, Configuring Fetch- and LoadGraphs, Spring Boot, Open EntityManager in View, Vlad Mihalcea, The Open Session In View Anti-Pattern) - 7
SPRING-11Define transaction boundaries with@Transactionalon the methods of service classes that carry out one use case across several repository calls, and call those methods from other beans. With Spring's default proxy mode, a call from one method of a class to another of the same class bypasses the proxy and runs without the transaction. Put the annotation on concrete classes, not on interfaces. (Spring Data JPA, Transactionality, Spring Framework, Using @Transactional)
Operations¶
- 7
SPRING-12On Kubernetes, point the liveness and readiness probes at/actuator/health/livenessand/actuator/health/readiness, keep checks of external systems out of the liveness group, and give the pod apreStopsleep and a termination grace period long enough for the sleep andspring.lifecycle.timeout-per-shutdown-phase. Graceful shutdown is on by default, but traffic can still reach a pod that has begun shutting down. If Actuator runs on a separate management port, also expose the probes on the main port withmanagement.endpoint.health.probes.add-additional-paths=true. This appliesK8S-2andK8S-3in Kubernetes. (Spring Boot, Kubernetes Probes, Checking External State With Kubernetes Probes, Graceful Shutdown, Kubernetes Container Lifecycle) - 7
SPRING-13Instrument through Micrometer Observation, which Spring Boot uses for metrics and traces, export both over OTLP to an OpenTelemetry Collector, and set the service name withOTEL_SERVICE_NAMEormanagement.opentelemetry.resource-attributes. Addspring-boot-starter-opentelemetryfor traces, and Micrometer's OTLP registry for metrics. Spring Boot samples only 10% of traces by default, so setmanagement.tracing.sampling.probabilitydeliberately (OTEL-8). The OpenTelemetry Java agent and Spring Boot starter, maintained by the OpenTelemetry community, are the alternatives. This appliesOPS-11in general/operations-reliability.md, andOTEL-1andOTEL-11in OpenTelemetry. (Spring Boot, Observability: OpenTelemetry Support, Tracing: OpenTelemetry With OTLP, Tracing: Sampling, Metrics: OTLP)
Versions and runtime¶
- 8
SPRING-14Run a Spring Boot generation that still has open-source support, and upgrade to the next minor release before its support ends; run it on a Java LTS release that your JDK distribution still updates. Each Spring Boot minor release gets at least 13 months of open-source support; for example, open-source support for Spring Boot 3.5 ended on 30 June 2026, and for 4.0 it ends on 31 December 2026. Java 17, 21 and 25 are LTS releases, and Spring Boot 4.1 runs on Java 17 to 27. This is how Spring Boot applications followSEC-17in general/security.md. (Spring, Spring Boot support timeline, Spring support policy, Spring Boot, System Requirements, Oracle Java SE Support Roadmap) - 5
SPRING-15Enable virtual threads withspring.threads.virtual.enabled=trueonly on Java 21 or later, preferably Java 24 or later, and when you do, setspring.main.keep-alive=trueand limit concurrency with semaphores or connection pools rather than pools of threads. Virtual threads are daemon threads, so scheduled tasks alone no longer keep the JVM running, and thread pool settings stop having an effect. Watch for pinned virtual threads, which lower throughput. (Spring Boot, Virtual threads, Oracle, Java 25 Virtual Threads)
Testing¶
- 7
SPRING-16Test controllers with@WebMvcTestand repositories with@DataJpaTest, and run at least the repository and integration tests against the database engine you use in production, started by Testcontainers and connected with@ServiceConnection. Each test slice loads only the configuration for one layer, so the tests stay fast. In-memory databases are fast, but the Hibernate guide advises testing against the database system you use in production. (Spring Boot, Auto-configured Tests, Testcontainers: Service Connections, Hibernate, Testing persistence logic)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 7 Spring Modulith verification:
ApplicationModules.of(Application.class).verify()in a unit test fails on cycles between modules and on references to another module's internal packages. ChecksSPRING-2. - 7 GitHub secret scanning push protection: blocks pushes that contain credentials, for example in
application.yaml. See general/security.md for more secret scanners. Partly checksSPRING-3. - 6 CodeQL, Disabled Spring CSRF protection, Exposed Spring Boot actuators and Exposed Spring Boot actuators in configuration file: queries in the default code-scanning suite that flag
csrf().disable(), security configurations that open Actuator endpoints to everyone, andmanagement.endpoints.web.exposure.include=*. Partly checksSPRING-6andSPRING-7. - 6 ArchUnit GeneralCodingRules and ProxyRules:
NO_CLASSES_SHOULD_USE_FIELD_INJECTIONfails a test when a class uses field injection, andno_classes_should_directly_call_other_methods_declared_in_the_same_class_that_are_annotated_with(Transactional.class)when a class calls its own@Transactionalmethods. ChecksSPRING-1, and partlySPRING-11. - 6 OWASP Dependency-Check (also for Gradle): scans the dependencies, including Spring Boot and the libraries it manages, for known vulnerabilities, and fails the build above the CVSS score set in
failBuildOnCVSS. Partly checksSPRING-14.
Tutorials and further reading¶
- 6 Developing Your First Spring Boot Application (Spring Boot docs): the official tutorial, from the build file to a running web application and an executable jar.
- 6 Spring guides (Spring): short getting-started guides and tutorials for tasks such as building REST services, securing web applications, and accessing data with JPA.
- 5 Spring PetClinic (Spring): the reference sample application, with Spring Boot, Spring Data JPA and tests.