Auto-wiring in Spring
- The Spring framework can inject dependencies automatically.
- The Spring container detects those dependencies specified in the configuration file and the relationship between the beans. This is referred to as Autowiring in Spring.
Key points:
- In other words, the Autowiring feature of the Spring framework enables you to inject the object dependency implicitly.
- It Internally uses the setter or constructor injection.
- Autowiring can't be used to inject primitive and String values.
- It works with reference only.
Advantages of Autowiring:
- We don't need to write code to inject the dependency explicitly. So it requires less code.
Disadvantages of Autowiring:
- It can't be used for primitive and String values.
- No control of the programmer.
Modes of Autowiring
There are five modes of auto wiring:
1. No
- This mode tells the framework that auto-wiring is not supposed to be done. It is the default mode used by Spring.
2.byName
- It uses the name of the bean for injecting dependencies. However, it requires that the name of the property and bean must be the same.
- It invokes the setter method internally for auto wiring.
3.byType
- It injects the dependency according to the type of bean. It looks up in the configuration file for the class type of the property. If it finds a bean that matches, it injects the property. If not, the program throws an error.
4. constructor
- It injects the required dependencies by invoking the constructor.
- It works similarly to the “byType” mode but looks for the constructor arguments' class type.
5. autodetect
- The autodetect mode uses two other ways for auto wiring – constructor and byType. It first tries to auto-wire via the constructor mode and if it fails, it uses the byType way for auto-wiring.
- It works in Spring 2.0 and 2.5 but is deprecated from Spring 3.0 onwards.
Example:
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;
}
}
Vehicle.java
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;
}
}
MyApplication.java
package com.quipoin;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public 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
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("Vehicale Number: "+v1.getVno());
System.out.println("Vehicle Name: "+v1.getVname());
System.out.println("Vehicle Engine Model: "+v1.getEngine().getModel());
System.out.println("Vehicle Engine Capacity: "+v1.getEngine().getCapacity());
}
}
Output:
Vehicale Number: Ka51
Vehicle Name: Swift
Vehicle Engine Model: K10 Engine
Vehicle Engine Capacity: 1.2hpw