gene b.
gene b.

Reputation: 11974

Spring Data: Can't Autowire @Repository JpaRepository Interface: "expected at least 1 bean which qualifies as autowire candidate"

My question is almost identical to this one but not the same, because I'm NOT using Spring Boot. Can't Autowire @Repository annotated interface in Spring Boot So I can't do @EnableJpaRepositories there's no Spring Boot Runner in my case. I have SpringMVC Controllers inside a Web app.

I'm using Spring Data independently, in a regular old-school SpringMVC application.

I'm getting the error

Caused by: No qualifying bean of type 'com.myapp.dao.QuestionsDAO' available: 
expected at least 1 bean which qualifies as autowire candidate.

DAO Interface for Spring Data, note @Repository:

@Repository
public interface QuestionsDAO extends JpaRepository<Question, Long> {

    public String findById(Long id);

}

A Service should then use this DAO, autowired:

Component

public class SchedulingService {

    @Autowired
    QuestionsDAO questionsDAO;

    public String findLabelById(Long id) {

        return questionsDAO.findById(id);

    }

}

Component Scan is enabled, works for everything else.

<context:component-scan base-package="com.myapp" />

Is Spring Data only allowed with Spring Boot?

Upvotes: 1

Views: 1635

Answers (1)

dunni
dunni

Reputation: 44515

The annotation @EnableJpaRepositories comes from Spring Data, it has nothing to do with Spring Boot. So it would be enough, to have one class annotated with @Configuration and @EnableJpaRepositories.

If you want to do it in XML, you have to add

<jpa:repositories base-package="com.acme.repositories" />

You also don't need the @Repository annotation on your interfaces, that annotation has another purpose.

Upvotes: 1

Related Questions