Spring Framework Professional Standards & Competencies 4 — Questions and Answers
Question 1: A senior Spring developer is reviewing code that uses @Autowired on a field marked as non-final. What concern should they raise?
- Field injection prevents circular dependency detection
- Field injection hides dependencies and complicates unit testing (Correct answer)
- Field injection is forbidden by the Spring specification
- Field injection causes runtime NullPointerExceptions always
Correct answer: Field injection hides dependencies and complicates unit testing
Field injection hides required dependencies from the class API and requires a Spring container even for simple unit tests.
Question 2: What is the correct professional approach to validate request body data in a Spring MVC REST controller?
- Write if-null checks in controller logic
- Use Bean Validation with @Valid and a @ControllerAdvice for error handling (Correct answer)
- Validate in the database layer only
- Use @RequestParam for all inputs to avoid validation
Correct answer: Use Bean Validation with @Valid and a @ControllerAdvice for error handling
@Valid triggers Bean Validation (JSR-380) on the request body, and @ControllerAdvice centralizes error responses.
Question 3: Which approach is the Spring professional standard for caching expensive method results?
- Storing results in a static HashMap
- Using @Cacheable with a configured CacheManager (Correct answer)
- Writing custom proxy classes for each service
- Relying solely on HTTP cache headers
Correct answer: Using @Cacheable with a configured CacheManager
@Cacheable backed by a configured CacheManager (e.g., Caffeine, Redis) provides declarative, consistent caching across the application.
Question 4: A professional is designing a Spring application that must publish and consume domain events within the same application. What is the recommended approach?
- Use a third-party message broker only
- Use ApplicationEventPublisher with @EventListener (Correct answer)
- Use direct method calls on all listeners
- Poll a shared database table for events
Correct answer: Use ApplicationEventPublisher with @EventListener
ApplicationEventPublisher and @EventListener provide Spring's built-in in-process event mechanism for decoupled component communication.
Question 5: When profiling a Spring Boot application in production, which embedded actuator endpoint set should a professional expose with caution?
- /actuator/health and /actuator/info
- /actuator/env and /actuator/heapdump (Correct answer)
- /actuator/metrics only
- /actuator/loggers only
Correct answer: /actuator/env and /actuator/heapdump
/actuator/env can expose sensitive properties and /actuator/heapdump can expose heap data; both require strict security controls in production.
Question 6: What is the professional standard practice for defining a Spring Data JPA repository?
- Implement JpaRepository manually with all CRUD methods
- Extend JpaRepository<Entity, ID> and add query methods by naming convention (Correct answer)
- Write native SQL queries in every method
- Use EntityManager directly everywhere
Correct answer: Extend JpaRepository<Entity, ID> and add query methods by naming convention
Extending JpaRepository lets Spring Data generate CRUD and query implementations automatically, reducing boilerplate significantly.
Question 7: Which professional strategy should be used to prevent over-fetching related entities in Spring Data JPA?
- Always use EAGER fetch for all associations
- Use LAZY fetching and load associations only when needed via specific queries or DTOs (Correct answer)
- Load all data in a single massive JOIN always
- Disable JPA lazy loading globally
Correct answer: Use LAZY fetching and load associations only when needed via specific queries or DTOs
LAZY fetching prevents unnecessary SQL queries; professionals load only required data through targeted queries or DTO projections.
A senior Spring developer is reviewing code that uses @Autowired on a field marked as non-final.
What concern should they raise?