This question might sound lame but I had a doubt so I wanted to clear it out. I am reading Getting Started with Spring Framework by Ashish Sarin and there is this simple code which fetches a spring bean of type FixedDepositService class
package sample.spring.chapter06.bankapp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import sample.spring.chapter06.bankapp.domain.FixedDepositDetails;
import sample.spring.chapter06.bankapp.service.FixedDepositService;
public class BankApp {
public static void main(String args[]) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:META-INF/spring/applicationContext.xml");
FixedDepositService fixedDepositService = context
.getBean(FixedDepositService.class);
fixedDepositService.createFixedDeposit(new FixedDepositDetails(1, 0,
12, "someemail@somedomain.com"));
fixedDepositService.createFixedDeposit(new FixedDepositDetails(1, 1000,
12, "someemail@somedomain.com"));
}
}
Now FixedDepositService is an interface and not a concrete class. It is implemented by another class called FixedDepositServiceImpl like this
....
@Service(value = "fixedDepositService")
public class FixedDepositServiceImpl implements FixedDepositService {
....
}
My question is, how does this code work? Shouldn't the BankApp class fetch a bean of FixedDepositServiceImpl class instead of FixedDepositService class since FixedDepositService is just an interface and contains no method definitions and FixedDepositServiceImpl is the class that actually implements it and has all the logic defined in it?