Skip to content
Advertisement

Field “” in “” required a bean of type “” that could not be found

I’m struggling through a Spring Web API tutorial, using STS4.

I got to the state in the tutorial where I need to test it, but I just got a “not found”, so the routing doesn’t seem to be working.

I saw online comments that seem to suggest I need a @ComponentScan("com.example") adding to the main() function, something not mentioned in the tutorial.

So I did this but now I’m getting the following.

JavaScript

My project looks like this:

enter image description here

The main method (in QuickPollApplication.java):

JavaScript

and PollController.java looks like this:

JavaScript

and this is PollRepository.java:

JavaScript

Let me know if it would be helpful to show anything else!

As per a suggestion I tried adding @EnableJpaRepositories(basePackages="com.example") to the QuickPollApplication class, but this just resulted in more errors, starting with:

Error creating bean with name ‘pollController’: Unsatisfied dependency expressed through field ‘pollRepository’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘pollRepository’ defined in com.example.repository.PollRepository defined in @EnableJpaRepositories declared on QuickPollApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.example.domain.Poll

Advertisement

Answer

Can you move application main class to com.example package instead?

If not, You can try adding
@EnableJpaRepositories(basePackages="com.example") to QuickPollApplication class.

Also you require additional config like @EntityScan with base packages configured.

Explaination:

By default, Spring Boot enables JPA repository support and looks in the package (and its subpackages) where @SpringBootApplication is located. If your configuration has JPA repository interface definitions located in a package that is not visible, you can point out alternate packages by using @EnableJpaRepositories and its type-safe basePackageClasses=MyRepository.class parameter.

Ref: https://spring.io/guides/gs/accessing-data-jpa/

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement