Modern Java & Spring Boot Guidance (CLI + Hook + Skill)
LLMs heavily default to Java 8 and Spring Boot 2.x idioms (javax.*, WebSecurityConfigurerAdapter, RestTemplate, @Autowired field injection, new Date(), list.get(list.size() - 1), Executors.newFixedThreadPool). Use this skill and the bundled mjg CLI to detect the project's actual versions and enforce modern equivalents without breaking legacy codebases.
Step 1: Detect Project Versions First
Before writing or refactoring Java code, run mjg detect (or inspect pom.xml / build.gradle.kts / build.gradle / .java-version):
mjg detect .
- Java <= 11: Respect the project's configured Java version. Do not emit Java 17/21 syntax (
record,sealed,SequencedCollection, Virtual Threads) or upgrade the build file unless explicitly requested. - Java 17: Use
record,sealedclasses/interfaces,switchexpressions, text blocks,List.of/Map.of,var, andjava.time. Do not use Java 21 APIs (SequencedCollection, Virtual Threads, Record Patterns). - Java 21+ (or greenfield code): Use Java 21+ idioms including Pattern Matching for
switch, Record Patterns,SequencedCollection(getFirst(),getLast(),reversed()), and Virtual Threads (Executors.newVirtualThreadPerTaskExecutor()). - Spring Boot 2.x: Keep
javax.*and existing Spring Security 5 /RestTemplateAPIs unless migrating to Boot 3. - Spring Boot 3.0–3.1: Use
jakarta.*,SecurityFilterChain+ Lambda DSL +requestMatchers(), and constructor injection. (RestClientis not available yet; keepWebClientorRestTemplate.) - Spring Boot 3.2–3.3: Also prefer
RestClient/@HttpExchangeoverRestTemplate, and extracted/layered JARs (-Djarmode=tools) for containers. - Spring Boot 3.4+: Also replace deprecated
@MockBean/@SpyBeanwith@MockitoBean/@MockitoSpyBean.
Step 2: Legacy vs. Modern Decision Matrix
| Category | ❌ Legacy (Java 8 / Boot 2.x) | ✅ Modern Java 21+ / Spring Boot 3.x+ |
|---|---|---|
| DTO / Value Objects | Verbose POJO or @Data on immutable DTOs | public record UserDto(Long id, String email) {} |
| Type Dispatch | if (x instanceof A) { A a = (A) x; ... } | Pattern Matching for switch + sealed interface |
| First / Last Element | list.get(0), list.get(list.size() - 1) | list.getFirst(), list.getLast(), list.reversed() |
| I/O Concurrency | Executors.newFixedThreadPool(...) for blocking I/O | Executors.newVirtualThreadPerTaskExecutor() |
| Date & Time | new Date(), SimpleDateFormat | Instant.now(), LocalDate, DateTimeFormatter (java.time) |
| Collections | Collections.unmodifiableList(Arrays.asList(...)) | List.of(...), Set.of(...), Map.of(...) |
| EE Packages (Boot 3+) | import javax.persistence.*, javax.validation.* | import jakarta.persistence.*, jakarta.validation.* |
| Spring Security 6 | extends WebSecurityConfigurerAdapter, .antMatchers(), .csrf().disable() | @Bean SecurityFilterChain, .authorizeHttpRequests(a -> a.requestMatchers(...)), .csrf(AbstractHttpConfigurer::disable) |
| HTTP Client (Boot 3.2+) | new RestTemplate() | RestClient.builder() or @HttpExchange |
| Dependency Injection | @Autowired field injection | Constructor injection with private final fields |
Step 3: On-Demand Guides & References
For token-efficient lookups without loading full reference files, query the built-in catalog:
mjg search "<topic>" # e.g. mjg search "security" or mjg search "virtual-threads"
mjg retrieve <GUIDE_ID> # e.g. mjg retrieve BOOT-002
- Java 17/21/25 Language & Stdlib Patterns: Read references/java-21-25-core.md
- Spring Boot 3.x+ & Spring Security 6 Patterns: Read references/spring-boot-3x.md
Step 4: Verify With mjg check Before Finishing
After writing or editing .java files, run the version-aware scanner (or let the PostToolUse hook run it automatically):
mjg check src/
Fix all error findings (e.g. javax.*, WebSecurityConfigurerAdapter) and address warning findings unless an external ABI or concurrency constraint (e.g., CPU-bound bounded pool) requires keeping the existing pattern (suppress intentional exceptions inline with // mjg:ignore <RULE-ID>). To enforce the same version-gated rules in CI without mjg, export a Semgrep Java AST ruleset via mjg export --format semgrep --path . -o .semgrep/modern-java.yaml.