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....");
}
}
Comments
Post a Comment