How Auto wiring Works In Springboot ?

Auto wiring & Its types .

Jahid Momin
3 min readJan 17, 2023

Auto-wiring, also known as automatic wiring, is a feature of the Spring Framework that automatically wires beans together by matching the types of the properties, constructor arguments, or method arguments of the beans. Auto-wiring eliminates the need for explicit configuration of the relationships between beans in the application context configuration file, thus reducing the amount of XML configuration and making the code more readable.

Auto-wiring can be applied to the following types of beans:

  • Properties: Spring can automatically set the properties of a bean by matching the type of the property with the beans available in the Application Context

Autowiring byType:

@Service
public class MyService {
@Autowired
// it will find out MyRepository Type In Application Context
// then wire it to property .
private MyRepository repository;
}

//Spring will automatically wire the MyRepository
//bean to the repository property of the MyService bean by matching the types.

Autowiring byName:


@Repository
@Primary // to indicate that it should be used as the primary candidate for autowiring,
// in case multiple beans with the same name are present in the Application Context
@Qualifier("myRepositoryBean1")
public class MyRepository {
public void save() {
//Code to save data
}
}

@Repository
@Qualifier("myRepositoryBean2")
public class MyRepository {
public void save() {
//Code to save data
}
}


@Service
public class MyService {
@Autowired
//spring ioc container find out bean with name myRepositoryBean1 and wire to this property of MyService
@Qualifier("myRepositoryBean1")
private MyRepository repository;
}
  • Constructor arguments: Spring can automatically call the appropriate constructor by matching the constructor argument types with the beans available in the Application Context

Autowiring by constructor:

@Service
public class MyService {
private MyRepository repository;

@Autowired
//find out based on type and wiring will be done using constructor
public MyService(MyRepository repository) {
this.repository = repository;
}
}

// this is based on byName using constructor

@Service
public class MyService {
private MyRepository repository;

@Autowired
//it will find bean By name and then wire
public MyService(@Qualifier("myRepositoryBean1") MyRepository repository) {
this.repository = repository;
}
public void doSomething() {
repository.save();
}
}

Spring will automatically wire an instance of MyRepository to the constructor of MyService class.

  • Method arguments: Spring can automatically call the appropriate setter methods by matching the method argument types with the beans available in the Application Context.

Autowiring by setter methods

@Service
public class MyService {
private MyRepository repository;


//Spring will automatically wire the MyRepository bean to the setRepository method of the MyService bean.
@Autowired
public void setRepository(MyRepository repository) {
this.repository = repository;
}
}


// --------
@Service
public class MyService {
private MyRepository repository;
@Autowired
@Qualifier("myRepositoryBean1")
//wiring of bean using setter method with byName
public void setRepository(MyRepository repository) {
this.repository = repository;
}
public void doSomething() {
repository.save();
}
}

In this example, MyService class has a setter method that takes a MyRepository object as an argument. The @Autowired annotation on the setter method and @Qualifier("myRepositoryBean1") annotation on the argument tells Spring to automatically wire an instance of MyRepository bean with the name "myRepositoryBean" to this setter method when creating an instance of MyService.

Additionally, you can use the @Primary annotation on a bean to indicate that it should be used as the primary candidate for autowiring, in case multiple beans with the same name are present in the Application Context.

Thank you , happy learning . Keep following .

--

--

Jahid Momin

Team Lead | Sr Software Engineer | Spring boot | Microservices | JavaScript | CodeIgniter | HTML | CSS | ReactJS | NextJS | Youtuber | Writer