Spring Core Annotations Interview Questions and Answers

Master Spring Core annotations with interview questions covering @Component, @Service, @Repository, @Controller, @RestController, @Configuration, @Bean, @Autowired, @Qualifier, @Primary, @Value, @Lazy, @Profile, and production best practices.


Introduction

Spring uses annotations to configure applications instead of XML.

Annotations simplify development by allowing developers to declare metadata directly in Java classes.

Spring Core annotations help the framework:

  • Discover beans
  • Inject dependencies
  • Configure applications
  • Manage bean lifecycle
  • Activate profiles
  • Load configuration
  • Control bean creation

Modern Spring Boot applications are almost entirely annotation-driven.


Spring Annotation Architecture

flowchart TD

SpringContainer --> ComponentScan

ComponentScan --> Component

ComponentScan --> Service

ComponentScan --> Repository

ComponentScan --> Controller

Controller --> Service

Service --> Repository

Q1. What are Spring Annotations?

Answer

Spring Annotations provide metadata that tells the Spring Framework how to manage application components.

Instead of XML configuration,

annotations define

  • Bean creation
  • Dependency injection
  • Configuration
  • Bean lifecycle
  • Component scanning

Benefits

  • Less boilerplate
  • Cleaner code
  • Easier maintenance
  • Better readability

Q2. What is @Component?

@Component is the generic stereotype annotation.

Example

@Component

public class EmailUtil {

}

Spring automatically detects and registers the class as a bean.

Use it when the class does not belong to a specific application layer.


Q3. What is @Service?

@Service represents the business layer.

Example

@Service

public class PaymentService {

}

Benefits

  • Better readability
  • Clear business intent
  • Supports service-layer architecture

Functionally, it behaves like @Component.


Q4. What is @Repository?

@Repository represents the persistence layer.

Example

@Repository

public class CustomerRepository {

}

Additional benefit

Spring automatically translates database exceptions into Spring DataAccessException hierarchy.

Layered Architecture

flowchart LR

Controller --> Service

Service --> Repository

Repository --> Database

Q5. What is @Controller vs @RestController?

@Controller

Returns a View.

Example

@Controller

public class HomeController{

}

@RestController

Returns JSON or XML.

Example

@RestController

public class CustomerController{

}

@RestController is equivalent to

@Controller

+

@ResponseBody

Q6. What is @Configuration and @Bean?

@Configuration

Defines a configuration class.

@Bean

Registers an object as a Spring Bean.

Example

@Configuration

public class AppConfig {

    @Bean

    public ObjectMapper objectMapper(){

        return new ObjectMapper();

    }

}

Configuration Flow

flowchart LR

Configuration --> BeanMethod

BeanMethod --> SpringContainer

SpringContainer --> ObjectMapper

Q7. What are @Autowired, @Qualifier and @Primary?

@Autowired

Automatically injects dependencies.

@Autowired

PaymentService

service;

@Qualifier

Chooses a specific bean.

@Qualifier(

"upiProcessor")

@Primary

Marks the default bean.

@Primary

Together they resolve dependency ambiguity.


Q8. What are @Value and @PropertySource?

@Value

Injects property values.

Example

@Value(

"${server.port}")

private int port;

@PropertySource

Loads external property files.

@PropertySource(

"classpath:application.properties")

These annotations externalize configuration.


Q9. What are @Lazy and @Profile?

@Lazy

Creates beans only when first requested.

@Lazy

@Profile

Activates beans for specific environments.

@Profile(

"prod")

Profile Selection

flowchart LR

Application --> DevProfile

Application --> TestProfile

Application --> ProdProfile

Q10. Spring Annotation Best Practices

Use Layered Stereotypes

  • @Controller
  • @Service
  • @Repository

Instead of generic @Component.


Prefer Constructor Injection

Avoid field injection.


Use @Configuration

For configuration classes only.


Use @Profile

Separate environments cleanly.


Keep Components Focused

One responsibility per bean.


Banking Example

flowchart TD

CustomerController --> CustomerService

CustomerService --> CustomerRepository

CustomerRepository --> PostgreSQL

SpringContainer --> CustomerController

SpringContainer --> CustomerService

SpringContainer --> CustomerRepository

Each annotation clearly represents its architectural responsibility.


Common Interview Questions

  • What are Spring Annotations?
  • What is @Component?
  • @Service vs @Component?
  • What is @Repository?
  • @Controller vs @RestController?
  • What is @Configuration?
  • What is @Bean?
  • What is @Autowired?
  • What is @Value?
  • Spring annotation best practices?

Quick Revision

Annotation Purpose
@Component Generic Spring bean
@Service Business layer
@Repository Persistence layer
@Controller MVC Controller
@RestController REST API Controller
@Configuration Configuration class
@Bean Register bean
@Autowired Inject dependency
@Qualifier Select bean
@Primary Default bean
@Value Inject property
@Profile Environment-specific bean
@Lazy Lazy initialization

Component Scanning Lifecycle

sequenceDiagram
Application->>Spring Container: Start
Spring Container->>Component Scan: Scan Packages
Component Scan->>Controller: Register
Component Scan->>Service: Register
Component Scan->>Repository: Register
Spring Container->>Dependency Injection: Wire Beans
Dependency Injection-->>Application: Ready

Production Example – Banking Customer Management System

A banking platform follows a layered architecture.

Components

  • CustomerController exposes REST endpoints using @RestController.
  • CustomerService contains business rules using @Service.
  • CustomerRepository accesses the database using @Repository.
  • ApplicationConfig configures infrastructure beans using @Configuration and @Bean.
  • KafkaProducer is activated only in production using @Profile("prod").
  • FraudDetectionService is initialized lazily using @Lazy.
flowchart LR

SpringContainer --> CustomerController

SpringContainer --> CustomerService

SpringContainer --> CustomerRepository

SpringContainer --> ApplicationConfig

ApplicationConfig --> ObjectMapper

CustomerController --> CustomerService

CustomerService --> CustomerRepository

CustomerRepository --> PostgreSQL

ApplicationConfig --> KafkaProducer

This layered design keeps responsibilities clear, simplifies testing, and makes the application easier to maintain as it grows.


Key Takeaways

  • Spring annotations replace XML configuration with a concise, annotation-driven programming model.
  • @Component, @Service, @Repository, and @Controller are stereotype annotations used to identify different application layers.
  • @RestController combines @Controller and @ResponseBody to build REST APIs.
  • @Configuration and @Bean define Java-based configuration and custom bean creation.
  • @Autowired, @Qualifier, and @Primary work together to perform dependency injection and resolve bean ambiguity.
  • @Value and @PropertySource help externalize application configuration.
  • @Profile enables environment-specific beans, while @Lazy delays bean creation until it is needed.
  • Using the appropriate annotations consistently leads to clean, modular, maintainable, and production-ready Spring applications.