What is Auto-wiring?

Auto-wiring is a feature in Spring that automatically injects required dependencies into a bean at runtime. Instead of manually creating and connecting objects, Spring's IoC container identifies the required dependencies and wires them together, resulting in cleaner, more maintainable, and loosely coupled code.

Key Points: • Auto-wiring reduces manual dependency creation and configuration. • It is commonly implemented using the @Autowired annotation. • Spring resolves dependencies by type by default. • It promotes loose coupling between application components. • It works with constructor, setter, and field injection.

Why Do We Need Auto-wiring?

Without auto-wiring, developers must manually create and connect objects.

Example:

EmployeeService service =
        new EmployeeService(
                new EmployeeRepository());

As the application grows, managing dependencies manually becomes difficult.

Spring solves this by automatically creating and injecting required beans.

How Auto-wiring Works

Spring Container Starts | Creates Beans | Identifies Dependencies | Injects Required Beans | Application Ready

The developer focuses on business logic while Spring handles object creation and dependency management.

Types of Auto-wiring

1. Constructor Injection (Recommended)

Code Example:

@Service
public class EmployeeService {

    private final EmployeeRepository repository;

    @Autowired
    public EmployeeService(
            EmployeeRepository repository) {

        this.repository = repository;
    }
}

Spring automatically injects EmployeeRepository into EmployeeService.

Benefits:

• Immutable dependencies • Easier testing • Recommended for production applications

2. Field Injection

Code Example:

@Service
public class EmployeeService {

    @Autowired
    private EmployeeRepository repository;
}

Spring injects the dependency directly into the field.

Although simple, constructor injection is generally preferred.

3. Setter Injection

Code Example:

@Service
public class EmployeeService {

    private EmployeeRepository repository;

    @Autowired
    public void setRepository(
            EmployeeRepository repository) {

        this.repository = repository;
    }
}

Useful when dependencies are optional or may change.

Auto-wiring by Type

Spring searches for a bean whose type matches the required dependency.

Example:

@Autowired
private EmployeeRepository repository;

Spring looks for a bean of type:

EmployeeRepository

and injects it automatically.

Handling Multiple Bean Candidates

If multiple beans of the same type exist, Spring cannot determine which one to inject.

Example:

@Repository
class MySqlRepository { }

@Repository
class OracleRepository { }

To resolve ambiguity:

Code Example:

@Autowired
@Qualifier("mySqlRepository")
private EmployeeRepository repository;

@Qualifier tells Spring which bean should be injected.

Example: Suppose an EmployeeController depends on EmployeeService.

EmployeeController |

          v
EmployeeService

|

          v
EmployeeRepository

Spring automatically creates and wires all these beans together during application startup.

Benefits of Auto-wiring

• Less boilerplate code • Better maintainability • Loose coupling • Easier testing • Improved readability • Faster development

Real-World Example

In an e-commerce application:

OrderController | OrderService | OrderRepository

Instead of manually creating every object, Spring automatically injects all required dependencies, making the application easier to develop and maintain.

Interview Tip: A concise interview answer is:

"Auto-wiring is a Spring feature that automatically injects required dependencies into a bean. It is commonly achieved using the @Autowired annotation and helps reduce manual object creation, promotes loose coupling, and simplifies dependency management within the Spring IoC container."