Hi i am newbie to Spring. While i was writing some sample application using spring annotation using spring annotation i had a query,
Interface Sample{
public void abc();
}
@Service(name = "sample")
Class SampleImpl{
public void abc(){
}
Class MAin{
@Autowired
Sample sam;
My question is can we autowire the implementation class directly,
for example
@Autowired
SampleImpl sampImpl;
if not then why?
Yes, you can, as long as you annotate (or declare in xml) the class you want to autowire.
Spring will find the best match in context by type (and qualifier, if specified).
It’s not the best idea though, as it makes testing/mocking more difficult and generally makes components too closely coupled.