java-spring-patterns
Skill1 starsChanged 2 months agoMITView on GitHub
Spring Boot patterns and the traps a general model gets wrong - constructor injection over field @Autowired, @ConfigurationProperties with validation, the @Transactional self-invocation proxy failure, @ControllerAdvice error handling, and test slices (@WebMvcTest, @DataJpaTest, @SpringBootTest). Use when building or reviewing a Spring Boot service, when a transaction "is not rolling back", when a bean is unexpectedly a proxy, or when a user asks about DI, "why isn't my transaction working", or "how do I test just the controller". Triggers on "@Transactional", "@Autowired", "@ConfigurationProperties", "ApplicationContext", "proxy", "self-invocation", "ControllerAdvice", "WebMvcTest".
In this repository126 other files it gives its agents.
- eu-data-backend-job-hunterSkill
- job-application-matcherSkill
- llm-agents-and-tool-useSkill
- llm-application-architectureSkill
- llm-cost-and-latency-optimizationSkill
- llm-evaluationSkill
- llm-fine-tuning-and-adaptersSkill
- llm-privacy-and-safetySkill
- llm-prompt-engineeringSkill
- llm-rag-systemsSkill
- structured-output-and-jsonSkill
- graphql-api-designSkill
- pagination-and-filteringSkill
- rest-api-designSkill
- webhooks-and-integrationsSkill
- caching-architecture-multi-layerSkill
- resilience-patternsSkill
- service-boundaries-and-monolithsSkill
- system-design-interviewSkill
- go-concurrencySkill
- go-error-handlingSkill
- nodejs-server-performanceSkill
- python-async-patternsSkill
- python-data-modelingSkill
- python-packaging-and-toolingSkill
- python-performanceSkill
- rest-api-implementationSkill
- rust-error-handlingSkill
- rust-ownership-patternsSkill
- typescript-type-safetySkill
- ci-caching-and-speedupSkill
- ci-pipeline-designSkill
- deployment-strategiesSkill
- github-actions-hardeningSkill
- release-versioning-and-changelogsSkill
- reproducible-buildsSkill
- aws-iam-and-securitySkill
- aws-serverless-architectureSkill
- azure-fundamentalsSkill
- cloud-cost-optimizationSkill
- cloud-networking-and-vpcSkill
- gcp-cloud-architectureSkill
- code-review-skillsSkill
- design-review-and-refactoring-architectureSkill
- refactoring-techniqueSkill
- understanding-unknown-codebaseSkill
- buildkit-and-cacheSkill
- container-image-debuggingSkill
- container-runtime-securitySkill
- docker-compose-workflowsSkill
- dockerfile-authoringSkill
- docker-image-optimizationSkill
- database-connection-managementSkill
- database-transactionsSkill
- distributed-databasesSkill
- nosql-data-modelingSkill
- postgres-query-tuningSkill
- redis-and-cachingSkill
- schema-migrationsSkill
- data-modeling-for-analyticsSkill
- The first 60 shown.
The file
We split this skill into the rules it gives an agent
5 rules. About 0 are common, 0 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 96
Add spring-boot-configuration-processor (annotation processor) so the IDE autocompletes and you get a compile-time hint when a property is …
- Very specific: line 210
Do not catch Exception in a controller and return 200 with an error body.
- Very specific: line 248
Use @MockitoBean on 3.4+; on older versions use @MockBean and accept the deprecation.
- Very specific: line 258
Use Testcontainers (not H2) when the query is dialect-specific (native SQL, Postgres jsonb, window functions) — H2 in a compatibility mode …
- Very specific: line 275
A
@ControllerAdvicescoped to a base package: only handles exceptions from controllers in (or under) that package.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Java Spring Patterns
Spring's magic is a proxy in front of your object. Almost every "Spring
doesn't work" bug is a consequence of that: the proxy only intercepts calls
that arrive through it, so a self-invocation, a private method, or a
new of your own class bypasses the behaviour you annotated. Know where the
proxy boundaries are and most Spring confusion resolves.
Workflow
- 1. Constructor-inject every dependency; no field
@Autowired - 2.
@ConfigurationProperties+@Validatedfor every config class, so bad config fails at startup - 3.
@Transactionalon the public method that starts the transaction, and understand the self-invocation trap - 4. One
@RestControllerAdvicethat turns exceptions into one response shape - 5. Pick the narrowest test slice that can fail the build
- 6.
mvn -q verify/./gradlew buildwith the test slice in CI
DI: constructor injection, always
@Service
public class OrderService {
private final OrderRepository repo; // final
private final PaymentGateway payments;
public OrderService(OrderRepository repo, PaymentGateway payments) { // one ctor, no @Autowired needed
this.repo = repo;
this.payments = payments;
}
}- A single constructor is autowired implicitly — do not add
@Autowiredto it. This is the strongest reason to have exactly one. - Field injection (
@Autowiredon a field) works but hides the dependency, makes the class untestable without a container, preventsfinal, and lets you instantiate a half-built object. Reserve it for framework-required field injection (rare). Use constructor injection everywhere else. - Inject interfaces at the boundary when you want a test double; a concrete class can still be injected, which is fine.
@Component/@Service/@Repository/@Controllerare the same mechanism with different stereotypes and default exception translation.@Repositoryis the one that matters: it enablesPersistenceExceptionTranslationPostProcessor, converting JPA's checked-ish exceptions into Spring'sDataAccessExceptionhierarchy. Use it for anything that touches a repository.- Bean scope defaults to singleton — one instance for the whole
application, shared across all threads. A singleton holding mutable
request state is a data race. Keep beans stateless; pass per-request data as
method arguments.
@RequestScopecreates a proxy that resolves per request (note: the bean is proxied, so a self-invocation on a request-scoped bean hits a different instance). - Circular dependencies fail at startup by default
(
BeanCurrentlyInCreationException) since Boot 2.6 — by design, it usually indicates a design problem. Extract a shared collaborator; do not reach for@Lazyto hide it. (spring.main.allow-circular-references=truere-enables it, at the cost of proxies that may not be what you expect.)
@ConfigurationProperties with validation
Bind config to a typed class and validate it at startup, so a bad environment variable fails the deploy immediately, not the first request that reads it.
@ConfigurationProperties(prefix = "app")
@Validated // required for @NotNull etc to fire
public record AppProperties(
@NotBlank String databaseUrl,
@Min(1) @Max(64) int workers,
@NotNull @Valid PaymentProperties payment, // @Valid cascades into nested
Duration timeout // "30s" binds automatically
) {}
// Enable it: @EnableConfigurationProperties(AppProperties.class) on a @Configuration,
// or @ConfigurationPropertiesScan on the app class, or spring-boot-configuration-processor
// for IDE completion.- In Boot 3, constructor/record binding means a single
@ConstructorBindingconstructor; setters are not needed. Records work well. - Annotate with
@Validated(jakarta.validation) or thejakarta.validationconstraints on fields/record components are ignored — noBindValidationExceptionat startup, and you get a null at runtime instead. - Add
spring-boot-configuration-processor(annotation processor) so the IDE autocompletes and you get a compile-time hint when a property is removed.@ConfigurationPropertiesclasses are validated at startup; a bad value gives a clearConfigurationPropertiesBindExceptionnaming the property — far better than anullthree hours into a deploy. - Property name mapping:
app.payment.stripe-key→PaymentProperties.stripeKey(relaxed binding: kebab, snake, camel all work). Environment variables map viaAPP_PAYMENT_STRIPE_KEY. @Value("${app.x}")for one-off values is fine; for a group of related settings, use@ConfigurationPropertiesso they are typed and validated together.
Transactions: the self-invocation trap
@Transactional is implemented with a proxy (JDK or CGLIB) around your
bean. The advice runs only when the call enters through the proxy. Therefore:
@Service
public class OrderService {
@Transactional // starts a transaction
public Order place(OrderRequest req) {
repo.save(order);
this.applyDiscount(order); // internal call — BYPASSES the proxy
return order;
}
@Transactional // this annotation is IGNORED for the internal call
protected void applyDiscount(Order o) { ... }
}this.applyDiscount(...) is a plain Java call on this; there is no proxy,
so no new transaction starts and no advice runs. This is the single most
common Spring transaction bug, and it fails silently — the code "works" and
just is not transactional.
Fixes, in order of preference:
- Move the annotated method to a different bean and call it through the injected reference. Now the call goes through that bean's proxy. This is the clean fix.
AopContext.currentProxy()— requiresexposeProxy = true; it is a workaround, and it is easy to forget. Avoid unless you must.- Make the call go through
self = (OrderService) AopContext.currentProxy();. - Restructure so the transactional boundary is the public entry point and internal methods just do work. Usually the best answer.
Other transaction traps:
@Transactionalon aprivatemethod does nothing — Spring's proxy (even CGLIB) cannot intercept a private call. Put it onpublic(orprotected, CGLIB only).@Transactionalon a method that returnsvoid/a non-future rolls back on a thrown RuntimeException by default. Checked exceptions do not roll back unless you add@Transactional(rollbackFor = Exception.class)orrollbackFor = IOException.class. This surprises people writing@Transactionalmethods that catch and log.finalclasses andfinal/privatemethods cannot be proxied by CGLIB; Spring falls back to a JDK proxy which then only exposes interfaces. Boot'sproxy-target-class=true(the default) uses CGLIB, so afinalclass silently loses the advice. Do not mark a@Servicefinal.- Self-invocation also defeats
@Async,@Cacheable,@Retryable,@PreDestroyand every other proxy-based advice — same root cause. - Read-only transactions (
@Transactional(readOnly = true)) let the JPA provider skip dirty checking and route to a replica if configured. Use them for queries; do not mutate inside one. @Transactionalon a private or on a repository method called from a non-transactional service is fine (the repository is proxied), but the transaction then covers only the repository call unless the service is also@Transactional. Understand your actual boundary.
Exception handling and error responses
One @RestControllerAdvice maps exceptions to one response shape, so every
error looks the same. Pair it with the RFC 9457 problem-details contract from
rest-api-implementation.
@RestControllerAdvice
public class ApiExceptionHandler {
@ExceptionHandler(OrderNotFoundException.class)
ResponseEntity<ProblemDetail> notFound(OrderNotFoundException e) {
ProblemDetail pd = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage());
pd.setTitle("Order not found");
pd.setProperty("code", "order_not_found");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(pd);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
ResponseEntity<ProblemDetail> invalid(MethodArgumentNotValidException e) {
ProblemDetail pd = ProblemDetail.forStatus(HttpStatus.UNPROCESSABLE_ENTITY);
pd.setTitle("Validation failed");
List<FieldError> errors = e.getBindingResult().getFieldErrors().stream()
.map(f -> new FieldError(f.getField(), f.getCode(), f.getDefaultMessage()))
.toList();
pd.setProperty("errors", errors);
return ResponseEntity.unprocessableEntity().body(pd);
}
}- Spring 6 / Boot 3 has
ProblemDetail(RFC 9457) built in. Use it rather than a hand-rolled error class. @ExceptionHandlerdoes not catch async exceptions from a different thread. An exception in an@Asyncmethod, aCompletableFuture, or a scheduler does not route through@ControllerAdvice. Wrap the async work and return the error through the future, or catch inside the task.- A missing handler means a
500with Spring's default error body (or, ifserver.error.include-stacktrace=always, a stack trace in the response — never enable that in production). Every custom exception class you throw from a controller needs a handler, or it is a 500. - Do not catch
Exceptionin a controller and return 200 with an error body. Let it propagate to the advice. - Validation errors:
@Validon the controller method argument triggersMethodArgumentNotValidException(a400by default). Set the status in the handler (commonly422for well-formed-but-invalid, per the API contract).
Testing: the right slice
Boot test slices start only the beans you need, so a unit test runs in milliseconds instead of booting the whole app.
| Annotation | Boots | Use for |
@WebMvcTest(OrderController.class) | Web layer + your controller, with mocked service beans | Controller routing, status codes, validation, serialisation, @ControllerAdvice |
@DataJpaTest | JPA + an embedded/replaced DataSource (usually H2/Testcontainers) | Repository queries, custom JPQL/SQL, mappings, constraints |
@JsonTest | Jackson only | (De)serialisation of DTOs |
@WebFluxTest | Reactive web layer | WebFlux controllers |
@SpringBootTest | Everything | A handful of true integration tests |
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired MockMvc mvc; // MockMvc, no real port
@MockitoBean OrderService orders; // Boot 3.4/SF 6.2+; @MockBean before
@Test void place_returns201() throws Exception {
given(orders.place(any())).willReturn(new Order("o_1"));
mvc.perform(post("/orders").contentType(APPLICATION_JSON)
.content("{\"sku\":\"A1\"}"))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value("o_1"));
}
}- In Spring Framework 6.2 / Boot 3.4,
@MockBeanis deprecated in favour of@MockitoBean(org.springframework.test.context.bean.override.mockito). Use@MockitoBeanon 3.4+; on older versions use@MockBeanand accept the deprecation. Check your Boot version — do not mix them. - A
@WebMvcTestdoes not load@Servicebeans or a real database, so a controller that autowires a repository directly fails to start. That is the slice telling you the layering is wrong. @DataJpaTestis@Transactionaland rolls back by default — great for speed, but a test that needs to see committed data or useTestEntityManagerflush semantics must account for it. Use@Commitor@Transactional(propagation = NOT_SUPPORTED)if you need real commits.- Use Testcontainers (not H2) when the query is dialect-specific (native
SQL, Postgres
jsonb, window functions) — H2 in a compatibility mode will pass a test that fails on the real database. - Constructor-injected beans are trivial to unit-test with plain Mockito, no Spring at all. Use a slice only when you need the framework behaviour (validation, routing, transaction proxies).
Gotchas
@Transactionalis invisible on self-calls,private/finalmethods, and objects younewyourself. If in doubt, put a breakpoint and checkAopUtils.isAopProxy(bean).@Valueon a field is not validated and fails late (or is null). Use@ConfigurationProperties+@Validatedfor required config.- Bean names can collide across packages (
application.ymlin two jars); the app fails to start with aConflictingBeanDefinitionException. Give explicit names or move config to a properties file. - A
@ControllerAdvicescoped to a base package only handles exceptions from controllers in (or under) that package. A controller in a different package will not get your handler. - Lazy beans and
@Lazyproxies delay initialisation, so a config error in a@Lazybean surfaces on first use, not at startup — which defeats fail-fast. Keep required config eager. @Asyncneeds@EnableAsync, and it must be called from a different bean (same self-invocation proxy issue).@Transactionaland@Asyncon the same method order unpredictably; split them across beans.- Jackson serialises by getter, not field, so a Lombok
@Getter-less field or a record component is not serialised. Use records or DTOs with explicit accessors; do not serialise your JPA entity directly (it drags lazy relations and internals into JSON — use a DTO). spring.jpa.open-in-viewdefaults to true in Boot 2/3 (it was disabled by default in some setups but is on by default in the auto-config), keeping a persistence context open for the whole request, which holds DB connections longer and causesLazyInitializationExceptionin serialisation. Setspring.jpa.open-in-view=falseand fetch what you need in the service.- Actuator endpoints are exposed but secured separately.
management.endpoints.web.exposure.include=*plus default security exposes/envand/heapdump— never do that in production.
House defaults
- Java 21, Spring Boot 3.x, Jakarta namespace, constructor injection,
records for DTOs,
@ConfigurationProperties+@Validatedfor config. - One
@RestControllerAdvicereturningProblemDetail. - Narrowest test slice that can fail the build; Testcontainers for anything dialect-specific.
spring.jpa.open-in-view=false.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

