Java 21 LTS Features
Virtual Threads with Project Loom:
Use try-with-resources on Executors.newVirtualThreadPerTaskExecutor. Call IntStream.range from 0 to 10000 and forEach to submit tasks that sleep for one second and return the iteration value.
Structured Concurrency Preview Pattern:
Use try-with-resources on new StructuredTaskScope.ShutdownOnFailure. Fork tasks for fetching user and orders by calling scope.fork with lambda expressions. Call scope.join then throwIfFailed. Return new composite object with results from both task suppliers.
Pattern Matching for Switch:
Create describe method taking Object parameter. Use switch expression with cases for Integer i with guard condition i greater than 0 returning positive integer message, Integer i returning non-positive message, String s returning length message, List with wildcard returning size message, null returning null value, and default returning unknown type.
Record Patterns and Sealed Classes:
Define Point record with int x and int y. Define Rectangle record with Point topLeft and Point bottomRight. Create area method that uses switch with Rectangle pattern destructuring both Point components into variables, returning absolute value of width times height. Define sealed Shape interface permitting Circle and Rectangle. Implement Circle record with area method using PI times radius squared.
Spring Boot 3.3
REST Controller Pattern:
Create UserController with RestController annotation, RequestMapping for api/users, and RequiredArgsConstructor. Inject UserService. Create getUser method with GetMapping and PathVariable for id, returning ResponseEntity that maps findById result to ok or returns notFound. Create createUser method with PostMapping, Valid annotation, and RequestBody for CreateUserRequest. Create user, build URI location, return created response with body. Create deleteUser method with DeleteMapping that returns noContent or notFound based on service result.
Service Layer Pattern:
Create UserService with Service, RequiredArgsConstructor, and Transactional readOnly true annotations. Inject UserRepository and PasswordEncoder. Create findById method returning Optional. Create transactional create method that checks for duplicate email throwing DuplicateEmailException, builds User with builder pattern encoding password, and saves to repository.
Spring Security 6
Security Configuration Pattern:
Create SecurityConfig with Configuration and EnableWebSecurity annotations. Define filterChain Bean taking HttpSecurity. Configure authorizeHttpRequests with permitAll for public API paths, hasRole ADMIN for admin paths, and authenticated for all other requests. Configure oauth2ResourceServer with jwt default. Set sessionManagement to STATELESS and disable csrf. Define passwordEncoder Bean returning BCryptPasswordEncoder.
JPA/Hibernate Patterns
Entity Definition Pattern:
Create User entity with Entity and Table annotations. Add Lombok Getter, Setter, NoArgsConstructor, and Builder annotations. Define id with Id and GeneratedValue IDENTITY. Define name and email with Column nullable false, email also unique. Define status with Enumerated STRING. Define orders with OneToMany mappedBy user, cascade ALL, and orphanRemoval true.
Repository with Custom Queries Pattern:
Create UserRepository extending JpaRepository. Add findByEmail returning Optional. Add existsByEmail returning boolean. Add Query annotation for JPQL with LEFT JOIN FETCH for findByIdWithOrders using Param annotation. Add findByNameContainingIgnoreCase with Pageable for pagination.
DTOs as Records Pattern:
Create UserDto record with id, name, email, and status. Add static from factory method that constructs record from User entity. Create CreateUserRequest record with NotBlank and Size annotations for name, NotBlank and Email for email, NotBlank and Size min 8 for password.
---