Spring Framework Spring Framework 4 — Questions and Answers
Question 1: What happens when you annotate a Spring bean method with @PostConstruct?
- It runs the method after every HTTP request
- It runs the method once after dependency injection is complete (Correct answer)
- It marks the method as a factory method
- It runs the method when the application shuts down
Correct answer: It runs the method once after dependency injection is complete
@PostConstruct marks an initialization method that Spring calls after the bean is fully constructed and all dependencies are injected.
Question 2: Which interface should a Spring bean implement to perform cleanup when the application context is closed?
- InitializingBean
- ApplicationListener
- DisposableBean (Correct answer)
- BeanPostProcessor
Correct answer: DisposableBean
DisposableBean provides a destroy() method that Spring calls when the container closes, allowing resource cleanup.
Question 3: What is the purpose of Spring's @Profile annotation?
- To enable profiling and performance monitoring for a bean
- To activate beans only when a specific environment profile is active (Correct answer)
- To configure the bean's thread pool size
- To group related beans into a named category
Correct answer: To activate beans only when a specific environment profile is active
@Profile restricts bean registration to specific environment profiles (e.g., 'dev', 'prod') set via spring.profiles.active.
Question 4: In Spring WebFlux, what is a Mono?
- A single-threaded executor for reactive streams
- A publisher that emits 0 or 1 item asynchronously (Correct answer)
- A blocking HTTP client for REST calls
- A configuration class for reactive databases
Correct answer: A publisher that emits 0 or 1 item asynchronously
Mono is a reactive publisher from Project Reactor that represents a single asynchronous value or an empty result.
Question 5: Which Spring annotation is used to define a class as a Spring MVC controller that returns data directly instead of a view name?
- @Controller
- @Service
- @RestController (Correct answer)
- @Component
Correct answer: @RestController
@RestController combines @Controller and @ResponseBody, so every method return value is serialized directly to the HTTP response.
Question 6: What does setting spring.jpa.hibernate.ddl-auto=validate do in a Spring Boot application?
- Drops and recreates the schema on every startup
- Updates the schema to match entities without dropping data
- Validates that the database schema matches the entities but makes no changes (Correct answer)
- Disables all schema management
Correct answer: Validates that the database schema matches the entities but makes no changes
The 'validate' option causes Hibernate to verify that the existing database schema matches the entity mappings and throws an exception if it does not.
Question 7: Which Spring component is responsible for translating exceptions into HTTP responses in a Spring MVC application?
- @ExceptionHandler / @ControllerAdvice (Correct answer)
- ApplicationEventPublisher
- HandlerInterceptor
- MessageConverter
Correct answer: @ExceptionHandler / @ControllerAdvice
@ExceptionHandler methods, typically grouped in a @ControllerAdvice class, catch exceptions and map them to specific HTTP responses.
What happens when you annotate a Spring bean method with @PostConstruct?