VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-autowiring/

⇱ Spring - Autowiring - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Spring - Autowiring

Last Updated : 16 Jan, 2026

Spring Autowiring is a feature of the Spring Framework that automatically injects dependent objects into a bean, reducing the need for explicit configuration. It helps make applications loosely coupled, cleaner, and easier to maintain.

  • Automatically resolves and injects dependencies between Spring beans
  • Reduces boilerplate code and XML/annotation configuration

Types of Autowiring in Spring

Spring supports the following types of autowiring:

1. no (Default)

Spring does not perform autowiring. Dependencies must be defined explicitly.

Key Points:

  • Default behavior
  • Full control over dependency injection
  • More boilerplate configuration

2. byName

Spring injects the dependency by matching the bean ID with the property name.

<bean id="city" class="sample.City" autowire="byName"/>

Key Points:

  • Uses setter-based injection
  • Property name must match the bean ID
  • Requires a public setter method

3. byType

Spring injects the dependency based on the class type

<bean id="city" class="sample.City" autowire="byType"/>

Key Points:

  • Setter-based injection
  • Throws an error if multiple beans of the same type exist
  • Bean name does not matter

4. constructor

Spring injects dependencies through the constructor.

<bean id="city" class="sample.City" autowire="constructor"/>

Key Points:

  • Constructor-based injection
  • Type-based resolution
  • Error occurs if no matching or multiple constructors are found

5. autodetect

Spring first tries constructor, then byType.

<bean id="city" class="sample.City" autowire="autodetect"/>

Note: This mode is deprecated in newer Spring versions and should be avoided.

Example: Autowiring Using ByName

This example demonstrates how Spring automatically injects a dependency by matching the bean ID with the property name using setter-based injection.

Step 1: Create the Project

Create a new Maven project in IntelliJ IDEA with the required GroupId, ArtifactId, and Java version.

  • Open IntelliJ -> New Project
  • Select Maven
  • JDK -> Java 8 or Java 11
  • GroupId: sample
  • ArtifactId: SpringAutowireDemo
  • Click Finish

Step 2: Project Structure

Organize the project into standard Maven directories for Java source files and resource files.

👁 Structure

Step 3: Add Dependencies (pom.xml)

Add the spring-context dependency to enable core Spring features and dependency injection.

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.3.30</version>

</dependency>

</dependencies>

Step 4: Create State Class

Create a simple POJO class representing the State with private fields and public getter/setter methods.

State.java

Step 5: Create City Class (Dependent Bean)

Create a City class that depends on the State class and exposes a setter method for byName autowiring.

City.java

Step 6: Configure Spring Beans (XML)

Define Spring beans in applicationContext.xml and enable autowiring using the byName mode.

applicationContext.xml

Step 7: Create Main Class

Load the Spring IoC container, retrieve the City bean, and invoke methods to verify dependency injection.

DemoApplication.java

Step 8: Run the Application

Run the main class to execute the program and observe the injected dependency output.

  • Right-click DemoApplication
  • Click Run

Output:

👁 Out
output

Advantages of Autowiring

  • Reduces boilerplate code
  • Improves readability and maintainability
  • Simplifies dependency management

Disadvantages of Autowiring

  • Less control over dependency injection
  • Not suitable for primitive and String values
  • Can cause ambiguity when multiple beans exist

Related Articles

Comment
Article Tags:
Article Tags: