Test your knowledge of the most used annotations in the Spring Framework, including component scanning, dependency injection, configuration, AOP, MVC REST, exception handling, and microservices support. Learn to identify the right annotation for each scenario and understand their core functionalities within Spring-based applications.
Which annotation is used to mark a class as a generic Spring-managed component that does not fit into the specialized categories like service or repository?
Explanation: @Component is the basic annotation for marking a generic Spring-managed bean. @Service, @Repository, and @Controller are specialized forms of @Component for specific layers, so they are not suitable for generic components. Only @Component serves the generic purpose.
If there are multiple beans of the same type, which annotation allows you to specify exactly which one to inject?
Explanation: @Qualifier is used with @Autowired to indicate which specific bean to inject when more than one candidate is available. @Autowired simply injects dependencies but doesn't resolve ambiguity. @Scope defines bean scope, and @Lazy delays bean initialization but doesn't handle bean selection.
What annotation combines the features of a controller and automatic JSON/XML response conversion for RESTful APIs in Spring?
Explanation: @RestController combines @Controller and @ResponseBody, so controller methods return responses in JSON/XML directly. @Controller does not automatically convert responses. @ResponseBody alone doesn't define a controller, and @Service is meant for business logic, not HTTP handling.
If you need to load properties from an external properties file into your Spring configuration, which annotation should you use?
Explanation: @PropertySource imports external property files for use within a configuration class. @Value injects individual property values but does not load files. @ImportResource is for XML configs, and @DependsOn controls initialization order, not property sources.
Which annotation defines an aspect for handling cross-cutting concerns like logging in Spring's AOP module?
Explanation: @Aspect marks a class as an aspect containing advice for cross-cutting concerns. @Pointcut defines a method signature for pointcuts but does not declare aspects themselves. @After and @Around are for advice implementation, not aspect definition.
Which annotation marks a bean as the default when there are multiple beans of the same type available for injection?
Explanation: @Primary is used to select a default bean when more than one exists for a given type. @Autowired injects dependencies but doesn't pick a default automatically. @EnableAutoConfiguration is used for configuration in specific contexts, and @Bean defines beans but not their priority.
To schedule a method to run at fixed intervals using a cron expression, which annotation would you use?
Explanation: @Scheduled allows you to define cron expressions for scheduling methods to run at specific intervals. @Async indicates running methods asynchronously but not on a schedule. @EnableScheduling is required to activate scheduling but doesn't directly schedule methods, while @Bean creates beans and does not relate to time-based execution.
What annotation would you use to inject a value from a properties file (for example, 'app.name') into a field?
Explanation: @Value is used to inject individual values from property files into Spring beans. @PropertySource loads the property source but doesn’t perform injection. @Qualifier is for distinguishing beans, and @Scope defines bean scopes, neither of which inject property values.
Which annotation marks a class as a source of bean definitions for the Spring container (i.e., a configuration class)?
Explanation: @Configuration marks a class for bean definition and allows it to declare @Bean methods. @ComponentScan is used for scanning packages, @Bean defines beans within configuration classes, and @Import brings in other configuration classes but does not itself declare property sources.
Which annotation should you use to map an HTTP GET request to a specific method in a Spring REST controller?
Explanation: @GetMapping specifically maps HTTP GET requests to handler methods. @PostMapping is for POST requests, while @RequestMapping is more general and can be ambiguous without explicit method definition. @RequestBody binds request payloads but does not map requests.
What annotation is used to handle exceptions at the controller level for a specific exception type?
Explanation: @ExceptionHandler is used in a controller to handle specified exception types. @ControllerAdvice is for global exception handling. @ResponseStatus sets the HTTP status, and @RestController is for defining REST controllers rather than exception handling.
Which annotation is used to delay the initialization of a bean in Spring until it is first needed?
Explanation: @Lazy postpones bean instantiation until it’s actually required. @DependsOn manages the startup order of beans. @Scope sets the lifecycle and number of bean instances, and @Primary designates the default bean for autowiring with no effect on initialization timing.
How can you import another Java-based configuration class into your primary configuration class in Spring?
Explanation: @Import brings other configuration classes into the current one, extending configuration definitions. @PropertySource is for loading external property files, not classes. @ComponentScan is for finding beans, while @Autowired is for injecting dependencies, not configuration classes.
Which annotation specifies a reusable set of join points (method executions) for advice in Aspect-Oriented Programming?
Explanation: @Pointcut defines a reusable expression for selecting join points that advice can be applied to. @Aspect declares an aspect, while @AfterThrowing and @Around are specific types of advice rather than pointcut definitions.
When you need to extract a variable (such as an 'id') from a URL path in a Spring controller, which annotation should be used?
Explanation: @PathVariable is used for extracting values from the URL path (e.g., /users/{id}). @RequestParam retrieves query parameters, @ModelAttribute binds request data to a model, and @RequestMapping is used for routing but not for variable extraction.