1

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?

gene b.
  • 10,512
  • 21
  • 115
  • 227

1 Answers1

1

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.

dunni
  • 43,386
  • 10
  • 104
  • 99
  • But I do need some Spring annotation, otherwise the Autowire won't work? Are you suggesting @Component for the Repository interface? – gene b. Nov 15 '17 at 17:42
  • No, the `@EnableJpaRepositories` or `` will take care of it. It will scan the packages which are configured, and check all interfaces which extend one of the Spring Data interfaces. So you don't need any annotation on the interfaces itself. – dunni Nov 15 '17 at 17:59