I am still learning how the Java language works.
I have a interface called DataService
. which serves as a interface between different database services; MongoDbDataservice.java
and MySQLDataService.java
DataService.java
public interface DataService {
int[] retrieveData(); // Method to retrieve data from different databases
MongoDbDataService.java
@Component
public class MongoDbDataService implements DataService {
public int[] retrieveData() {
return new int[] {11, 22, 33, 44, 55};
}
}
MySQLDataService.java
@Component
@Primary
public class MySQLDataService implements DataService {
public int[] retrieveData() {
return new int[] {1,2,3,4,5};
}
}
BusinessCalculationService.java
@Component
public class BusinessCalculationService {
private DataService dataService;
public BusinessCalculationService(DataService dataService) {
this.dataService = dataService;
}
public int findMax() {
return Arrays.stream(dataService.retrieveData()).max().orElse(0);
}
}
My question is what exactly is happening in the background when this line of code is compiled / executed?
public BusinessCalculationService(DataService dataService) {
this.dataService = dataService;
}
How is the DataService
object being "tied" or "wired" to BusinessCalculationService
and what does it mean to be "wired"
Everything up to that point makes sense to me except this. I should mention I am using Spring framework.
Thank you in advanced.