Autowiring
What is Autowiring?
Autowiring is a feature of the Spring Framework that allows automatic injection of bean dependencies without explicitly defining them in configuration.
Instead of writing:
Autowiring is a feature of the Spring Framework that allows automatic injection of bean dependencies without explicitly defining them in configuration.
Instead of writing:
<property name="engine" ref="engineBean" />
You just declare the dependency and Spring handles the wiring for you.
Key Point
- Spring automatically identifies the required dependency and injects it.
- It internally uses constructor or setter injection.
- Only reference types (like objects/beans) can be autowired. (Not for primitives or Strings)
- Reduces manual configuration and boilerplate code.
Advantages of Autowiring:
Disadvantages:
Autowiring Modes in Spring
- Requires less code - no need to define every ref manually.
- Easier to manage dependencies in large projects.
Disadvantages:
- Does not support autowiring of primitive or String values.
- Less control - difficult to debug or override default behavior.
Autowiring Modes in Spring
Mode | Description |
---|---|
no | Default. No autowiring is done. |
byName | Matches bean name with property name and injects. Uses setter internally. |
byType | Injects based on data type (class/interface) of the property. |
constructor | Injects via constructor using parameter types. |
autodetect | First tries constructor, then byType. (Deprecated from Spring 3.0+) |
Example: Using @Autowired Annotation
Let us see how annotation-based Autowiring works in a Spring Boot style configuration.
Project Structure:
com.quipoin
├── Engine.java
├── Vehicle.java
├── MyApplication.java
└── MyProgram.java
Engine.java
package com.quipoin;
public class Engine { private String model; private String capacity;
public String getModel() { return model; } public void setModel(String model) { this.model = model; }
public String getCapacity() { return capacity; } public void setCapacity(String capacity) { this.capacity = capacity; }}
Vechicle.java (with @Autowired)
package com.quipoin;
import org.springframework.beans.factory.annotation.Autowired;
public class Vehicle { private String vno; private String vname; private Engine engine;
public String getVno() { return vno; } public void setVno(String vno) { this.vno = vno; }
public String getVname() { return vname; } public void setVname(String vname) { this.vname = vname; }
public Engine getEngine() { return engine; }
@Autowired public void setEngine(Engine engine) { this.engine = engine; }}
MyApplicatin.java (Java-based Config)
package com.quipoin;
import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;
@Configurationpublic class MyApplication {
@Bean public Engine e1() { Engine e1 = new Engine(); e1.setModel("K10 Engine"); e1.setCapacity("1.2hpw"); return e1; }
@Bean public Vehicle v1() { Vehicle v1 = new Vehicle(); v1.setVno("Ka51"); v1.setVname("Swift"); return v1; }}
MyProgram.java (Main Class)
package com.quipoin;
import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyProgram { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(MyApplication.class);
Vehicle v1 = (Vehicle) context.getBean("v1");
System.out.println("Vehicle Number: " + v1.getVno()); System.out.println("Vehicle Name: " + v1.getVname()); System.out.println("Engine Model: " + v1.getEngine().getModel()); System.out.println("Engine Capacity: " + v1.getEngine().getCapacity()); }}
Output:
Vehicle Number: Ka51Vehicle Name: SwiftEngine Model: K10 EngineEngine Capacity: 1.2hpw