9

I'm working with Spring Boot + Spring Data JPA and facing this problem when trying to inject a class that extends CrudRepository:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topicRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)

Repository Class:

public interface TopicRepository extends CrudRepository<Topic, Integer> {}

Service Class:

@Service
public class TopicService {

      @Autowired
      private TopicRepository topicRepository;
}

Any suggestions?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
A.Chakroun
  • 261
  • 3
  • 17
  • do you have `@Repository` annotation on your interface? If yes remove it. – Patrick Feb 15 '17 at 15:58
  • no i don't have the @Repository annotation on the interface, the classes look exactly like in this post. Thank you. – A.Chakroun Feb 15 '17 at 16:05
  • normally this should be enough. Do you have a non default constructor on Topic Service or any other xml configuration? – Patrick Feb 15 '17 at 16:11
  • i don't have xml configuration at all and for the TopicService there is only the default constructor.Thank you. – A.Chakroun Feb 15 '17 at 16:14
  • Can you provides us with your jpa configuration? Provided you have a custom jpa configuration. Thank you – gkatzioura Feb 15 '17 at 16:24
  • I have no custom jpa configuration. Thank you – A.Chakroun Feb 15 '17 at 16:43
  • I see. My next question has to do with the packages that your classes reside. In which package resides your Application class (then one with the @SpringBootApplication annotation) and in which package resides your repository. Thank you. – gkatzioura Feb 15 '17 at 16:53
  • Can you show your pom xml. Maybe you insert not the right dependency – Patrick Feb 16 '17 at 07:09

6 Answers6

10

I was having the same issue, and I fixed it by switching Spring Boot versions. Changing the Spring Data JPA versions did nothing (this is where I assumed the bug would be), so I think there is a bug in Spring Boot version 1.5.1. I switched back to version 1.4.3 and the error was gone. I didn't try subsequent/different versions, so you may just have to experiment with your dependencies and their versions.

For the record, you can have your service class annotated with @Repository, it shouldn't make any difference. I've been setting these apps up the same way using the service/dao pattern, and it has never been too picky with the annotations. Hopefully this may help others whose Spring Boot development flow suddenly throws an error!

Trevor Bye
  • 686
  • 1
  • 6
  • 26
7

Which versions of spring-data-commons and spring-data-jpa are you using. I just ran into this using spring-data-commons 1.13.x with spring-data-jpa 1.10.x. Upgrading spring-data-jpa to 1.11.x fixed the issue for me.

Andy Sampson
  • 271
  • 4
  • 7
1

I too had the same issue after updating Spring Boot to 1.5.4.

I am also using spring-data-envers, which was at version 1.0.4. Upgrading to 1.4.1 solved the problem.

I hope it helps someone :)

r4phG
  • 470
  • 8
  • 16
0

Make sure:

1) TopicRepository is annotated with @Repository.

2) You have the scanning packages configured:

<jpa:repositories base-package="mypkg.repositories"></jpa:repositories>
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
  • 2
    both is not necessary to do. – Patrick Feb 15 '17 at 16:01
  • Spring data scans for repositories that extend CrudRepository, JPARepository etc. The @Repository annotation for spring data in not needed. – gkatzioura Feb 15 '17 at 16:07
  • According to the Sprign spec https://docs.spring.io/spring-data/jpa/docs/current/reference/html/, you should add that configuration with JavaConfig - @EnableJpaRepositories or with xml as i specified – Maciej Kowalski Feb 15 '17 at 16:25
0

Had the same issue on 1.5.2. Upgrading to 1.5.5 solved the problem.

dzirtbry
  • 328
  • 2
  • 14
0

You can use Applicationcontext to inject repository to this reference topicRepository.. You just declare applicationcontext in @rest controller class Same like topicRepository by using annotation. Then you pass this to the service class which should take parms through constructor. Ex- public TopicService(Applicationcontext ctx) {this.topicRepository =context.getBean(TopicRepository.class); }

Mohan kumar
  • 123
  • 13