In Spring Data JPA, what does the @Repository annotation signify?
AIt marks a class as a data access object and enables exception translation
BIt automatically generates SQL queries from method names
CIt configures the database connection pool
DIt enables caching for database queries
Correct Answer:
A. It marks a class as a data access object and enables exception translation
EXPLANATION
@Repository marks a class as a data access component and enables Spring to translate persistence-specific exceptions into Spring's DataAccessException hierarchy.
What is the role of PropertySource annotation in Spring Framework?
ATo define external property files and load their values into the environment
BTo create new properties dynamically at runtime
CTo encrypt sensitive properties automatically
DTo validate property values against defined constraints
Correct Answer:
A. To define external property files and load their values into the environment
EXPLANATION
@PropertySource allows loading properties from external files (like .properties or .yml) into the Spring Environment for use throughout the application.
Consider a scenario where you have two bean implementations of the same interface. How would you specify which one to inject using Spring annotations?
AUse @Primary on one bean and @Autowired on the injection point
BUse @Qualifier annotation with the bean name at the injection point
CUse @Autowired with @Primary or @Qualifier; both approaches work
DUse @Bean with explicit configuration in @Configuration class
Correct Answer:
C. Use @Autowired with @Primary or @Qualifier; both approaches work
EXPLANATION
Both @Primary (default choice) and @Qualifier (explicit selection by name) can resolve bean ambiguity. @Qualifier is more explicit and takes precedence over @Primary.
C@Component requires manual bean registration, while @Service auto-registers
DThere is no functional difference; they are completely interchangeable
Correct Answer:
A. @Component is generic, while @Service is semantically used for business logic classes
EXPLANATION
@Component is a generic stereotype, while @Service is a specialized annotation indicating the class contains business logic, improving code readability.
Which of the following correctly describes the lifecycle of a singleton-scoped Spring bean?
ACreated once per HTTP request and destroyed after the request completes
BCreated once when the ApplicationContext is initialized and persists throughout the application lifetime
CCreated every time it is referenced and destroyed immediately after use
DCreated in a separate thread context and shared across multiple threads
Correct Answer:
B. Created once when the ApplicationContext is initialized and persists throughout the application lifetime
EXPLANATION
Singleton beans are instantiated once per Spring container and reused for the application's lifetime, making them memory-efficient for stateless objects.
A microservices architecture uses Spring with circular dependency between ServiceA and ServiceB. What is the best solution?
AIncrease heap memory to handle circular references
BRefactor to introduce a third service or use setter injection with @Lazy
CUse @Autowired on both constructor and setter
DSwitch to manual bean management
Correct Answer:
B. Refactor to introduce a third service or use setter injection with @Lazy
EXPLANATION
Circular dependencies indicate design issues. Best solutions: refactor code, use setter injection (instead of constructor) combined with @Lazy, or introduce an intermediary service to break the cycle.