0

Hy everybody

I have this simplified version of my trouble. I´m trying to run my application but it fails as Repository is not beign recognized to be injected. I already tried to annotate Repository as Service and to add the repository package to be scanned, but none works. Can someone help me?

I get an exception

Description:

Field topicRepository in br.com.alura.controller.TopicController required a bean of type 'br.com.alura.repository.TopicRepository' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'br.com.alura.repository.TopicRepository' in your configuration.

Controller/Service

@Autowired
private TopicRepository topicRepository;

@GetMapping(value = "/api/topics", produces = MediaType.APPLICATION_JSON_VALUE)
public Page<TopicBriefOutputDto> listTopics(TopicSearchInputDto topicSearch, @PageableDefault(sort="creationInstant", direction=Sort.Direction.DESC) Pageable pageRequest) {
    Specification<Topic> topicSearchSpecification = topicSearch.build();
    Page<Topic> topics = this.topicRepository.findAll(topicSearchSpecification, pageRequest);
    return TopicBriefOutputDto.listFromTopics(topics);
}

Start

@SpringBootApplication
@Configuration
@ComponentScan(basePackages ={"br.com.alura.controller"})
@EntityScan
@EnableAutoConfiguration
@EnableJpaRepositories
@EnableSpringDataWebSupport
public class ForumApplication {

public static void main(String[] args) {
    SpringApplication.run(ForumApplication.class, args);
}

}

Repository

public interface TopicRepository extends Repository<Topic, Long>, JpaSpecificationExecutor<Topic> {

}
  • You don't have to explicitly add ComponentScan annotation. SpringBootApplication annotation does the job for you. Just add basePackages = {"br.com.alura"} – Pavan Aug 07 '20 at 12:33

1 Answers1

0

Probably You set incorrect base package. The package scope is too narrow.

Try this:

@EnableJpaRepositories(basePackages = {"br.com.alura.repository"})

or change br.com.alura.controller to br.com.alura

@ComponentScan(basePackages ={"br.com.alura"})
  • I got a new exception org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'topicController': Unsatisfied dependency expressed through field 'topicRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'topicRepository' defined in br.com.alura.repository.TopicRepository defined in @EnableJpaRepositories declared on ForumApplication: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class br.com.alura.model.Topic – Rafael Perracini Aug 07 '20 at 12:56
  • Topic is annotated with @Entity – Rafael Perracini Aug 07 '20 at 12:57
  • What did You do exactly? another option which You can try is `@SpringBootApplication(scanBasePackages = "br.com.alura")` – Łukasz Olszewski Aug 07 '20 at 13:01
  • I tried both alone and also togheter. but if you see there, the exception now was with the entity, that is in another package – Rafael Perracini Aug 07 '20 at 13:43
  • add `@EntityScan(basePackages = "br.com.alura.model")` – Łukasz Olszewski Aug 07 '20 at 13:47