Spring Framework Case Studies & Practical Application 4 — Questions and Answers
Question 1: A Spring Boot application experiences 'BeanCurrentlyInCreationException' at startup. What is the most common root cause?
- Missing @Autowired annotation on a required dependency
- A circular dependency where Bean A depends on Bean B and Bean B depends on Bean A (Correct answer)
- Two beans with the same name defined in different @Configuration classes
- A @Lazy bean being injected into an eager singleton
Correct answer: A circular dependency where Bean A depends on Bean B and Bean B depends on Bean A
Circular dependencies occur when two or more beans depend on each other, preventing the IoC container from instantiating either without the other already existing.
Question 2: You need to write an integration test for a Spring MVC controller without starting a full server. Which testing slice annotation should you use?
- @SpringBootTest(webEnvironment=RANDOM_PORT)
- @WebMvcTest(YourController.class) (Correct answer)
- @DataJpaTest
- @ContextConfiguration with an embedded server
Correct answer: @WebMvcTest(YourController.class)
@WebMvcTest loads only the web layer (controllers, filters, advice) and provides MockMvc, making it fast without starting a full embedded server.
Question 3: A scheduled Spring Batch job fails halfway through. On the next run, it should skip already-processed records. Which Batch feature supports this?
- Restarting the JVM with a different input file each run
- Spring Batch's JobRepository with restartable jobs — it tracks step execution state and resumes from the last checkpoint (Correct answer)
- Using @Transactional(propagation=NESTED) around each chunk
- Filtering processed records in application code by querying an audit table
Correct answer: Spring Batch's JobRepository with restartable jobs — it tracks step execution state and resumes from the last checkpoint
Spring Batch stores job and step execution metadata in its JobRepository, allowing a failed job to restart exactly where it left off at the chunk boundary.
Question 4: A Spring WebFlux endpoint must call two independent services in parallel and combine results. Which reactive operator achieves this?
- Mono.zip(serviceAMono, serviceBMono) (Correct answer)
- serviceAMono.flatMap(a -> serviceBMono)
- Flux.concat(serviceAMono, serviceBMono)
- serviceAMono.block() followed by serviceBMono.block()
Correct answer: Mono.zip(serviceAMono, serviceBMono)
Mono.zip subscribes to both Monos simultaneously and emits a tuple when both complete, achieving true parallel execution without blocking.
Question 5: A Spring Boot application must use different database credentials in dev, staging, and production environments. What is the recommended approach?
- Hard-code credentials in application.properties and override with system environment variables in higher environments
- Use Spring Profiles with separate application-{profile}.properties files and inject credentials via environment variables or Vault (Correct answer)
- Store credentials in a Git-tracked application-prod.properties file
- Use a single application.properties with placeholders filled at build time by Maven
Correct answer: Use Spring Profiles with separate application-{profile}.properties files and inject credentials via environment variables or Vault
Spring Profiles combined with externalized secrets (environment variables or HashiCorp Vault) keep credentials out of source control and environment-specific.
Question 6: After enabling Spring Cache with @EnableCaching and @Cacheable, a service method still executes on every call. What is a common misconfiguration cause?
- @Cacheable is not supported on @Service beans
- The method is called internally within the same class, bypassing the Spring proxy (Correct answer)
- The cache manager bean name must be exactly 'cacheManager' or it is ignored
- The @EnableCaching annotation must be on the @Service class, not @Configuration
Correct answer: The method is called internally within the same class, bypassing the Spring proxy
Spring AOP caching works via proxy; calling a @Cacheable method from within the same class skips the proxy and therefore skips the cache.
Question 7: A team needs to enforce a rule that every new Spring bean in the 'com.example.service' package is tested with an integration test. Which approach best enforces this architectural rule?
- Code review checklists that remind developers to write tests
- ArchUnit tests that assert every class in the service package has a corresponding test class (Correct answer)
- A custom Spring BeanPostProcessor that logs untested beans at startup
- A Checkstyle rule that requires a @Test annotation on every class
Correct answer: ArchUnit tests that assert every class in the service package has a corresponding test class
ArchUnit allows expressing and automatically enforcing architectural rules (such as test coverage requirements by package) as JUnit tests that fail the build if violated.
A Spring Boot application experiences 'BeanCurrentlyInCreationException' at startup.
What is the most common root cause?