0

My purpose is to implement custom DAO interface and that's what I do, but it seems does not work.

My interface is:

public interface AbstractDAO<T> {

    void add(T object);
    T load(Integer ID);
    List<T> list();
    void update(T object);
    void delete(Integer ID);
}

And it's implementation:

@Repository
public class AccountDAO implements AbstractDAO<Account> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void add(Account object) {
        ...
    }
...
}

as I told, it doesn't work due Spring scanner cannot find the dao class.

How to let this work? Without implements it works fine.

Spring scanner configuration:

<bean ...>
   <context:component-scan base-package="dao" />
   <context:component-scan base-package="tmp" />
   <context:component-scan base-package="services" />
   <context:component-scan base-package="entities" />

   <mvc:resources mapping="/resources/**" location="/resources/" />

   <mvc:annotation-driven />
   <tx:annotation-driven />
   <task:annotation-driven />

   <import resource="spring-beans.xml"/>
</bean>

Error:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [dao.AccountDAO] found for dependency: expected
at least 1 bean which qualifies as autowire candidate for this dependency
WildDev
  • 2,250
  • 5
  • 35
  • 67
  • can you share your spring scanner configurations ? – Udy Sep 06 '15 at 07:32
  • also, what is the error you get ?\ – Udy Sep 06 '15 at 07:43
  • It occurs when I try to use `AccountDAO` inside controllers. Controllers are inside `tmp` directory which is under scan as well. – WildDev Sep 06 '15 at 07:52
  • looks like you are auto-wiring the `AccountDao` try to auto-wire `AbstractDAO` with `qualifier` – Udy Sep 06 '15 at 07:59
  • Is there a specific reason you're not using Spring Data? – chrylis -cautiouslyoptimistic- Sep 06 '15 at 08:01
  • @Udy, I'd tried what you suggested but still no effect. I'd renamed `AccountDAO` to `AccountDao` and the parent interface as well. Still the same. – WildDev Sep 06 '15 at 08:04
  • @chrylis, no there's not. I just never heard about this – WildDev Sep 06 '15 at 08:06
  • This has to do with Spring using JDK-based interface proxies by default, and the type of the bean thus being `AbstractDAO`, and not `AccountDAO`. You should autowire the interface, not the concrete implementation. – JB Nizet Sep 06 '15 at 08:17
  • @JBNizet, thanks it's clear now. So, there's no way to use such inheritance with Spring components? – WildDev Sep 06 '15 at 08:30
  • Why would there be no way? You just need to program on interfaces, not on concrete classes. How is that not using inheritance? You can also ask spring to proxy target classes with CGLib. Check the doc. – JB Nizet Sep 06 '15 at 08:39

1 Answers1

1

Change the DAO implementation to:

@Repository("accountDAO")
public class AccountDAO implements AbstractDAO<Account> {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Transactional
    public void add(Account object) {
        ...
    }
    ...
}

and the usage of the DAO like:

@Controller
@RequestMapping(value = "")
public class Controller

    @Autowired
    @Qualifier("accountDAO")
    private AbstractDAO accountDAO;

    .....
}
Udy
  • 2,492
  • 4
  • 23
  • 33
  • Unfortunatelly still the same. I think this issue rather about spring inheritance rules than about the scanner, since it works without inheritance – WildDev Sep 06 '15 at 08:08
  • @Codemaster, im sorry but i had a mistake where the reference in the `Controller` was `AccountDAO` instead of `AbstractDAO`. try now.. – Udy Sep 06 '15 at 08:47
  • actually it didn't utill I refused from interfaces usage – WildDev Sep 06 '15 at 10:36