Spring Framework Spring Framework 2 — Questions and Answers
Question 1: Which annotation in Spring Boot enables auto-configuration, component scanning, and configuration in a single annotation?
- @EnableAutoConfiguration
- @SpringBootApplication (Correct answer)
- @ComponentScan
- @Configuration
Correct answer: @SpringBootApplication
@SpringBootApplication combines @EnableAutoConfiguration, @ComponentScan, and @Configuration into one meta-annotation.
Question 2: What is the purpose of the @Transactional annotation in Spring?
- To mark a method as asynchronous
- To define a REST endpoint
- To manage database transactions declaratively (Correct answer)
- To enable caching for a method
Correct answer: To manage database transactions declaratively
@Transactional tells Spring to wrap the method in a transaction, committing on success and rolling back on unchecked exceptions.
Question 3: Which Spring module provides support for creating RESTful web services?
- Spring Data
- Spring MVC (Correct answer)
- Spring Batch
- Spring Integration
Correct answer: Spring MVC
Spring MVC (part of Spring Web) provides @RestController and @RequestMapping to build RESTful APIs.
Question 4: What does Spring's @Value annotation do?
- Validates a field value at runtime
- Injects a value from a property source or SpEL expression (Correct answer)
- Marks a field as required in a form
- Sets a default return value for a method
Correct answer: Injects a value from a property source or SpEL expression
@Value injects externalized configuration values or SpEL expressions directly into fields or method parameters.
Question 5: In Spring Security, what is the role of a UserDetailsService?
- To hash passwords before storing them
- To load user-specific data during authentication (Correct answer)
- To generate JWT tokens for users
- To configure HTTP security rules
Correct answer: To load user-specific data during authentication
UserDetailsService is a core interface that Spring Security uses to retrieve a UserDetails object during authentication.
Question 6: Which propagation behavior in @Transactional means the method must always run within an existing transaction, throwing an exception if none exists?
- REQUIRED
- REQUIRES_NEW
- MANDATORY (Correct answer)
- NESTED
Correct answer: MANDATORY
MANDATORY propagation requires an active transaction to already exist, throwing IllegalTransactionStateException if not.
Question 7: What is Spring's ApplicationContext responsible for?
- Compiling Spring beans at build time
- Managing the lifecycle and dependencies of Spring beans (Correct answer)
- Providing HTTP request/response handling
- Generating database schemas from entities
Correct answer: Managing the lifecycle and dependencies of Spring beans
ApplicationContext is the central interface that manages bean instantiation, configuration, dependency injection, and lifecycle.
Which annotation in Spring Boot enables auto-configuration, component scanning, and configuration in a single annotation?