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?

8
  • do you have @Repository annotation on your interface? If yes remove it. Commented Feb 15, 2017 at 15:58
  • no i don't have the @Repository annotation on the interface, the classes look exactly like in this post. Thank you. Commented Feb 15, 2017 at 16:05
  • normally this should be enough. Do you have a non default constructor on Topic Service or any other xml configuration? Commented Feb 15, 2017 at 16:11
  • i don't have xml configuration at all and for the TopicService there is only the default constructor.Thank you. Commented Feb 15, 2017 at 16:14
  • Can you provides us with your jpa configuration? Provided you have a custom jpa configuration. Thank you Commented Feb 15, 2017 at 16:24

6 Answers 6

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Thanks for working this out already with the version combinations. It helped.
Thank you so much...spent 3 hours on this! You rock!
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 :)

Comments

0

Make sure:

1) TopicRepository is annotated with @Repository.

2) You have the scanning packages configured:

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

3 Comments

both is not necessary to do.
Spring data scans for repositories that extend CrudRepository, JPARepository etc. The @Repository annotation for spring data in not needed.
According to the Sprign spec docs.spring.io/spring-data/jpa/docs/current/reference/html, you should add that configuration with JavaConfig - @EnableJpaRepositories or with xml as i specified
0

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

Comments

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); }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.