Spring Framework Spring Framework 5 — Questions and Answers
Question 1: What is the primary purpose of Spring's BeanPostProcessor interface?
- To define initialization logic within a bean
- To intercept and modify beans after instantiation but before use (Correct answer)
- To configure the data source for Spring Data
- To register custom scopes with the container
Correct answer: To intercept and modify beans after instantiation but before use
BeanPostProcessor allows custom logic to run before and after Spring initializes each bean, enabling proxying and annotation processing.
Question 2: In Spring Security, which filter is responsible for processing form-based login submissions?
- BasicAuthenticationFilter
- UsernamePasswordAuthenticationFilter (Correct answer)
- DigestAuthenticationFilter
- BearerTokenAuthenticationFilter
Correct answer: UsernamePasswordAuthenticationFilter
UsernamePasswordAuthenticationFilter intercepts POST requests to the login URL and delegates authentication to the AuthenticationManager.
Question 3: Which annotation enables Spring's caching abstraction, allowing methods to cache their return values?
- @EnableScheduling
- @EnableAsync
- @EnableCaching (Correct answer)
- @EnableRetry
Correct answer: @EnableCaching
@EnableCaching activates Spring's annotation-driven cache management, enabling @Cacheable, @CachePut, and @CacheEvict.
Question 4: What does the @Async annotation do when applied to a Spring bean method?
- Marks the method as synchronized across threads
- Executes the method in a separate thread from the caller (Correct answer)
- Schedules the method to run at a fixed rate
- Retries the method on failure
Correct answer: Executes the method in a separate thread from the caller
@Async causes Spring to submit the method call to a TaskExecutor, running it asynchronously so the caller does not block.
Question 5: What is a Spring Boot 'starter' dependency?
- A generated project template from Spring Initializr
- A curated set of dependencies that simplify adding a feature to your project (Correct answer)
- An auto-configured in-memory database for testing
- A Spring Security configuration for starter projects
Correct answer: A curated set of dependencies that simplify adding a feature to your project
Starters are convenience dependency descriptors (e.g., spring-boot-starter-web) that pull in all required libraries for a feature with compatible versions.
Question 6: Which Spring annotation marks a method to be executed repeatedly on a fixed schedule?
- @Async
- @Timed
- @Scheduled (Correct answer)
- @Repeatable
Correct answer: @Scheduled
@Scheduled, combined with @EnableScheduling, allows Spring to trigger a method at fixed rates, fixed delays, or cron expressions.
Question 7: In Spring Data JPA, what does the @Query annotation allow you to do?
- Validate query results against a schema
- Define a custom JPQL or native SQL query directly on a repository method (Correct answer)
- Enable query caching for a repository
- Map query results to a DTO automatically
Correct answer: Define a custom JPQL or native SQL query directly on a repository method
@Query lets you write a custom JPQL or native SQL query on a repository method, overriding the derived query mechanism.
What is the primary purpose of Spring's BeanPostProcessor interface?