Skip to main content

Autowiring

Autowiring is used to perform dependency injection.

-> The process of injecting one class object into another class object is called as dependency injection.

-> In Spring framework IOC container will perform dependency injection.

-> We will provide instructions to IOC to perform DI in 2 ways

1) Manual Wiring  (using ref attribute in beans config xml file)

2) Autowiring

-> Autowiring means IOC will identify dependent object and it will inject into target object.

-> Autowiring will use below modes to perform Dependency Injection.

1) byName

2) byType

3) constructor (internally it will use byType only)

-> To perform Autowiring we will use @Autowired annotation.

-> @Autowired annotation we can use at 3 places in the program

1) variable level   (Field injection - FI )

2) setter method level (Setter Injection - SI )

3) constructor (Constructor Injection - CI )

Autowiring Example with @Qualifer

+++++++++++++++++++++++++

-> When we use @Autowired annotation it will use byType mode to identify dependent object.

-> If our interface having more than one impl then IOC will get confused to perform DI (Ambiguity).

-> To resolve that ambiguity problem we will use @Qualifier to speicify bean name to inject.

Note: When we use @Qualifier it will use byName mode to inject dependent object

Note: If we don't want to use @Qualifier then we should specify @Primary for one bean to get injected.



public interface ReportDao {

public String findData();

}

@Repository("oracle")

public class OracleReportDaoImpl implements ReportDao {

public OracleReportDaoImpl() {

System.out.println("OracleReportDaoImpl :: Constructor");

}

@Override

public String findData() {

System.out.println("fetching report data from oracle db...");

return "Report data";

}

}

@Repository("mysql")

//@Primary //It is used by type

public class MysqlReportDaoImpl implements ReportDao {

public MysqlReportDaoImpl() {

System.out.println("MysqlReportDaoImpl :: Constructor");

}

@Override

public String findData() {

System.out.println("fetching report data from mysql db...");

return "Report data";

}

}

@Service

public class ReportService {

private ReportDao reportDao;

@Autowired  // IOC Automatic detect and provided the dependencies

@Qualifier("oracle")  //use for solve Ambigute problem // byname then we use Qualifier

public void setReportDao(ReportDao reportDao) {

System.out.println("setReportDao() method called...");

this.reportDao = reportDao;

}

public void generateReport() {

reportDao.findData();

System.out.println("generating report....");

}

}

@SpringBootApplication

public class Application {

public static void main(String[] args) {

ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);

ReportService reportService = context.getBean(ReportService.class);

reportService.generateReport();

}

}

@Autowired at Constructor Level

++++++++++++++++++++

-> If we have both 0-Param constructor and parameterized constructor then ioc will use 0-param constrictor to create object.

-> If we want IOC to choose Param-Constructor to create obj then we should write @Autowired annotation at param-constructor

Note: If we have only param constructor in our class then @Autowired is optional.

@Service

public class ReportService {

private ReportDao reportDao;

public ReportService(ReportDao reportDao) {

System.out.println("ReportService :: Param Constructor called...");

this.reportDao = reportDao;

}

public void generateReport() {

reportDao.findData();

System.out.println("generating report....");

}

}

@Autowired with Field Injection

+++++++++++++++++++++++

-> IOC will use Reflection api to internally to perform Field Injection.

@Service

public class ReportService {

@Autowired

private ReportDao reportDao;

public void generateReport() {

reportDao.findData();

System.out.println("generating report....");

}

}

Note: Field Injection is not recommended because it is violating oops principles and it breaks Single Responsibility Principles.
-> When objects injected using field injection code review tools can't identify complexity.

SI vs CI vs FI
++++++++++++
-> Setter Injection will be performed through setter method.
-> It is mandatory to specify @Autowired annotation at setter method.
-> If we don't specify @Autowired annotation then DI will not happen (Partial Injection).
-> If DI not happend, when we call methods then we will get NullPointerException.
-> Target Bean will be created first then setter method will be called to inject dependent.


-> Constructor injection will be performed through constructor.
-> If we have more than one constructor then we have to specify @Autowired at constructor level.
-> If we have only one parameterized constructor then @Autowired is optional.
-> In CI, dependent bean will be created first then target bean will be created.
-> Partial Injection is not possible in CI.


-> FieldInjection will be performed through Reflection API.
-> FI violating OOPS principles (private variable getting initialized from outside using Reflecion).
-> It is simple to use.
-> Most of the programmers will use FI in projects because it us juts oneline code.


Note: Out of all these Dependency Injections, Constructor Injection is recommended because Partial Injection not possible and target bean will be created if dependent bean is available.




Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.