Spring Autowiring Interview Questions and Answers

Master Spring Autowiring with interview questions covering @Autowired, constructor injection, setter injection, field injection, @Qualifier, @Primary, @Lazy, optional dependencies, circular dependencies, and production best practices.


Introduction

One of the most powerful features of the Spring Framework is Autowiring.

Autowiring allows the Spring IoC Container to automatically resolve and inject dependencies between beans.

Without autowiring, developers would manually create and connect every object.

Example without Spring:

CustomerRepository repository = new CustomerRepository();
CustomerService service = new CustomerService(repository);
CustomerController controller = new CustomerController(service);

With Spring, the IoC Container automatically creates and injects the required dependencies.

Autowiring helps build:

  • Loosely coupled applications
  • Testable code
  • Maintainable architecture
  • Production-ready enterprise applications

Spring Autowiring Architecture

flowchart LR

SpringContainer --> CustomerController

SpringContainer --> CustomerService

SpringContainer --> CustomerRepository

CustomerController --> CustomerService

CustomerService --> CustomerRepository

Q1. What is Autowiring?

Answer

Autowiring is the process by which the Spring IoC Container automatically injects required dependencies into a bean.

Instead of creating objects manually,

Spring searches the container for matching beans and injects them automatically.

Benefits

  • Less boilerplate code
  • Loose coupling
  • Easier testing
  • Cleaner architecture

Q2. How does Autowiring work?

During application startup:

  1. Spring scans components.
  2. Creates bean instances.
  3. Resolves dependencies.
  4. Injects matching beans.
  5. Makes the bean available.

Autowiring Flow

sequenceDiagram
Application->>Spring Container: Start
Spring Container->>CustomerRepository: Create Bean
Spring Container->>CustomerService: Create Bean
Spring Container->>CustomerService: Inject Repository
Spring Container->>CustomerController: Create Bean
Spring Container->>CustomerController: Inject Service
Spring Container-->>Application: Ready

Q3. What are the different types of Autowiring?

Spring supports three major approaches.

Type Recommended
Constructor Injection ✅ Yes
Setter Injection Sometimes
Field Injection ❌ Avoid

Constructor Injection

@Service

public class CustomerService {

    private final CustomerRepository repository;

    public CustomerService(CustomerRepository repository) {
        this.repository = repository;
    }
}

Setter Injection

@Autowired

public void setRepository(

CustomerRepository repository){

}

Field Injection

@Autowired

private CustomerRepository repository;

Constructor injection is preferred in enterprise applications.


Q4. What is @Autowired?

@Autowired instructs Spring to inject a matching bean automatically.

Example

@Autowired

private PaymentService

paymentService;

From Spring Framework 4.3 onward,

@Autowired is optional for classes having only one constructor.


Q5. What is @Qualifier?

When multiple beans implement the same interface,

Spring cannot determine which bean to inject.

Example

@Autowired

@Qualifier(

"creditCardProcessor")

private PaymentProcessor

processor;

Qualifier Selection

flowchart LR

PaymentProcessor --> CreditCard

PaymentProcessor --> UPI

PaymentProcessor --> NetBanking

Qualifier --> CreditCard

@Qualifier specifies exactly which bean should be injected.


Q6. What is @Primary?

@Primary defines the default bean among multiple candidates.

Example

@Component

@Primary

class CreditCardProcessor

implements PaymentProcessor{

}

Whenever Spring finds multiple implementations,

it selects the @Primary bean unless a @Qualifier is provided.


Q7. What is @Lazy?

Normally, singleton beans are created during application startup.

@Lazy delays bean creation until it is first needed.

Example

@Lazy

@Service

public class ReportService{

}

Benefits

  • Faster startup
  • Lower memory usage
  • Useful for expensive objects

Q8. How do Optional Dependencies work?

Some dependencies may not always exist.

Example

@Autowired(

required = false)

private AuditService

auditService;

Alternative approaches

  • Optional<T>
  • ObjectProvider<T>

These approaches are generally preferred over required=false.


Q9. What happens when multiple beans exist?

Example

PaymentProcessor

↓

CreditCardProcessor

↓

UPIProcessor

↓

NetBankingProcessor

Resolution order

  1. @Qualifier
  2. @Primary
  3. Bean name
  4. Exception if ambiguous

Resolution Flow

flowchart TD

MultipleBeans --> Qualifier

Qualifier --> Primary

Primary --> BeanName

BeanName --> Success

BeanName --> Exception

Q10. Autowiring Best Practices

Prefer Constructor Injection

Supports immutability and testing.


Avoid Field Injection

Makes testing more difficult.


Use Interfaces

Depend on abstractions.


Use @Primary Carefully

Only one default implementation should exist.


Avoid Circular Dependencies

Refactor rather than relying on @Lazy.


Banking Example

flowchart TD

CustomerController --> CustomerService

CustomerService --> PaymentProcessor

PaymentProcessor --> CreditCardProcessor

PaymentProcessor --> UPIProcessor

CreditCardProcessor --> BankAPI

UPIProcessor --> UpiGateway

SpringContainer --> CustomerController

SpringContainer --> CustomerService

SpringContainer --> CreditCardProcessor

SpringContainer --> UPIProcessor

Spring injects the appropriate payment processor based on @Primary or @Qualifier.


Common Interview Questions

  • What is Autowiring?
  • How does Autowiring work?
  • What is @Autowired?
  • Constructor vs Setter vs Field Injection?
  • What is @Qualifier?
  • What is @Primary?
  • What is @Lazy?
  • What are optional dependencies?
  • How does Spring resolve multiple beans?
  • Autowiring best practices?

Quick Revision

Topic Summary
Autowiring Automatic dependency injection
@Autowired Inject matching bean
Constructor Injection Recommended approach
Setter Injection Optional dependencies
Field Injection Avoid in production
@Qualifier Select specific bean
@Primary Default bean
@Lazy Lazy bean creation
Optional Optional dependency injection
Multiple Beans Resolved using Qualifier or Primary

Autowiring Lifecycle

sequenceDiagram
Application->>Spring Container: Start
Spring Container->>Component Scan: Find Beans
Component Scan-->>Spring Container: Bean Definitions
Spring Container->>CustomerRepository: Create Bean
Spring Container->>CustomerService: Create Bean
Spring Container->>CustomerService: Inject Repository
Spring Container->>CustomerController: Create Bean
Spring Container->>CustomerController: Inject Service
Spring Container-->>Application: Ready

Production Example – Banking Payment System

A banking platform supports multiple payment methods:

  • Credit Card
  • UPI
  • Net Banking
  • Wallet

All implementations implement the PaymentProcessor interface.

Configuration

  • CreditCardProcessor is marked with @Primary.
  • UPIPaymentProcessor is injected explicitly using @Qualifier where required.
  • Constructor injection is used throughout the application.
  • FraudDetectionService is marked with @Lazy because it is initialized only for high-value transactions.
flowchart LR

SpringContainer --> PaymentService

SpringContainer --> CreditCardProcessor

SpringContainer --> UPIProcessor

SpringContainer --> WalletProcessor

PaymentService --> PaymentProcessor

PaymentProcessor --> CreditCardProcessor

PaymentProcessor --> UPIProcessor

PaymentProcessor --> WalletProcessor

FraudDetectionService --> LazyInitialization

This design provides loose coupling, simplifies testing with mock implementations, and allows new payment methods to be added without changing existing business logic.


Key Takeaways

  • Autowiring enables the Spring IoC Container to automatically resolve and inject bean dependencies.
  • Constructor Injection is the preferred approach because it supports immutability, mandatory dependencies, and easier unit testing.
  • @Autowired automatically injects matching beans, while @Qualifier and @Primary resolve ambiguity when multiple implementations exist.
  • @Lazy delays bean creation until first use, reducing startup time for expensive components.
  • Optional dependencies should be handled using Optional<T> or ObjectProvider<T> instead of relying on required=false.
  • Avoid field injection and circular dependencies in production applications.
  • Depend on interfaces rather than concrete implementations to promote loose coupling and extensibility.
  • Following these autowiring best practices results in clean, maintainable, testable, and enterprise-ready Spring applications.