Darshan Lila
Darshan Lila

Reputation: 5868

Autowiring a interface work without annotating it

I am working on Spring and hibernate project. For database communication we have conventional two layered implementation (i.e DAO layer and Service layer). I have following files:

DemoDao.java

package net.dao;
import java.util.List;
import net.domain.Demo;
public interface DemoDao 
{
 public List<Demo> get();
}

DemoDaoImpl.java

package net.dao;

import java.util.List;  

import net.domain.Demo;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class DemoDaoImpl implements DemoDao
{
    @Autowired
    SessionFactory sessionFactory;
    public List<Demo> get()
    {
        Session session=sessionFactory.openSession();
        List<Demo> list=session.createQuery("from Demo").list();
        session.close();
        return list;
    }
}

That was DAO layer

Follwing is from service layer:

DemoManager.java

package net.service;

import java.util.List;

import net.domain.Demo;

public interface DemoManager 
{
    public List<Demo> get();
}

DemoManagerImpl.java

package net.service;

import java.util.List;

import net.dao.DemoDao;
import net.domain.Demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DemoManagerImpl implements DemoManager 
{
    @Autowired
    DemoDao demoDao;
    public List<Demo> get()
    {
        List<Demo> list=demoDao.get();
        return list;
    }
}

Follwing is my controller

FromDualLayerView.java

package net.spring;

import java.util.List;

import net.domain.Demo;
import net.service.DemoManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FromDualLayerView 
{
    @Autowired
    DemoManager demoManager;

    @RequestMapping("/dualLayer")
    public ModelAndView toResult(ModelMap map)
    {
        List<Demo> list=demoManager.get();
        map.addAttribute("listData", list);
        return new ModelAndView("result");
    }
}

My Question

Actually everything works fine, but my question over here is that i am not annotating the DemoDao and DemoManager interface, but i am autowiring them. According to the autowiring definition the entities which are annotated are injected.

The how come the dependency is injected by Spring container?

And how does it work like an Impl class?

Thanks in advance.

Upvotes: 0

Views: 662

Answers (2)

zmf
zmf

Reputation: 9313

The @Repository annotation and the @Service annotation mean you are annotating them. It's actually a spring best practice to annotate the implementations and not the interfaces.

Your spring config file is scanning the classpath, thus those beans are detected. Turn spring logging up to TRACE and you'll probably see output along the lines of:

"scanning classpath, found target DemoDaoImpl"

Upvotes: 1

Michał Urbaniak
Michał Urbaniak

Reputation: 1344

DemoManagerImpl is annotated as a service and is the only qualifying bean to be injected in the FromDualLayerView class, as it's the only component which is instance of DemoManager. I suppose you have the component scan turned on as well.

Upvotes: 1

Related Questions