Spring annotations Notes Quiz

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.

  1. Identifying Generic Components

    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?

    1. @Component
    2. @Service
    3. @Repository
    4. @Controller

    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.

  2. Dependency Injection Choice

    If there are multiple beans of the same type, which annotation allows you to specify exactly which one to inject?

    1. @Lazy
    2. @Qualifier
    3. @Autowired
    4. @Scope

    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.

  3. Combining MVC and REST

    What annotation combines the features of a controller and automatic JSON/XML response conversion for RESTful APIs in Spring?

    1. @ResponseBody
    2. @RestController
    3. @Controller
    4. @Service

    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.

  4. Loading External Property Files

    If you need to load properties from an external properties file into your Spring configuration, which annotation should you use?

    1. @PropertySource
    2. @Value
    3. @DependsOn
    4. @ImportResource

    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.

  5. Aspect Definition

    Which annotation defines an aspect for handling cross-cutting concerns like logging in Spring's AOP module?

    1. @After
    2. @Aspect
    3. @Around
    4. @Pointcut

    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.

  6. Default Bean in Multiple Candidates

    Which annotation marks a bean as the default when there are multiple beans of the same type available for injection?

    1. @Primary
    2. @Bean
    3. @EnableAutoConfiguration
    4. @Autowired

    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.

  7. Scheduling a Method

    To schedule a method to run at fixed intervals using a cron expression, which annotation would you use?

    1. @Async
    2. @EnableScheduling
    3. @Scheduled
    4. @Bean

    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.

  8. Injecting Property Value

    What annotation would you use to inject a value from a properties file (for example, 'app.name') into a field?

    1. @Value
    2. @Qualifier
    3. @PropertySource
    4. @Scope

    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.

  9. Defining a Configuration Class

    Which annotation marks a class as a source of bean definitions for the Spring container (i.e., a configuration class)?

    1. @ComponentScan
    2. @Configuration
    3. @Bean
    4. @Import

    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.

  10. HTTP GET Request Mapping

    Which annotation should you use to map an HTTP GET request to a specific method in a Spring REST controller?

    1. @RequestBody
    2. @PostMapping
    3. @GetMapping
    4. @RequestMapping

    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.

  11. Controller-Level Exception Handling

    What annotation is used to handle exceptions at the controller level for a specific exception type?

    1. @RestController
    2. @ExceptionHandler
    3. @ControllerAdvice
    4. @ResponseStatus

    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.

  12. Injecting Beans Lazily

    Which annotation is used to delay the initialization of a bean in Spring until it is first needed?

    1. @Primary
    2. @DependsOn
    3. @Scope
    4. @Lazy

    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.

  13. Combining Multiple Configuration Classes

    How can you import another Java-based configuration class into your primary configuration class in Spring?

    1. @PropertySource
    2. @Import
    3. @Autowired
    4. @ComponentScan

    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.

  14. Pointcut Expression Definition

    Which annotation specifies a reusable set of join points (method executions) for advice in Aspect-Oriented Programming?

    1. @Around
    2. @Pointcut
    3. @Aspect
    4. @AfterThrowing

    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.

  15. Handling Values from URL Paths

    When you need to extract a variable (such as an 'id') from a URL path in a Spring controller, which annotation should be used?

    1. @ModelAttribute
    2. @RequestParam
    3. @RequestMapping
    4. @PathVariable

    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.