Skip to content
Advertisement

NoSuchBeanDefinitionException with reactive mongo repository: required a bean of type that could not be found

I have an issue: repository bean couldn’t be found when it’s placed in outer package. It causes nested UnsatisfiedDependencyException which is due to NoSuchBeanDefinitionException (expected at least 1 bean which qualifies as autowire candidate). After I copied the class to my project, it works perfectly. But I would like to use it as a dependency on external module. This is repository class:

@Repository
public interface PersonRepository extends ReactiveMongoRepository<Person, String> {
}

And classes from project that should use the repository:

@Configuration
@ComponentScan("outer.package.repository")
@EnableMongoRepositories(basePackages = {
        "outer.package.repository"
        //"local.package.repository" // temporary solution, should be external
})
public class MyConfig {
}

@Service
@RequiredArgsConstructor
public class PersonService {
    private final PersonRepository personRepository;
// do some stuff
}

As you see, I have all needed annotations on beans (@Repository, @Service, @Configuration), I registered mongo repositories (@EnableMongoRepositories) and even provided the directory to scan (@ComponentScan). Do you have any ideas what I’ve missed?

UPD: I’m using maven and project structure is like this:

src
  main
    java
      com
        example
          configuration
            MyConfig.java
          controller
            PersonController.java
          repository
            PersonRepository.java
          service
            PersonService.java
          MainApplication.java
    resources
  test
pom.xml

Advertisement

Answer

I’ve tried to reproduce the issue and it seems that changing the annotation

@EnableMongoRepositories(basePackages = {
        "outer.package.repository"
        //"local.package.repository" // temporary solution, should be external
})
public class MyConfig {}

to its reactive equivalent:

@EnableReactiveMongoRepositories(basePackages = {
        "outer.package.repository"
        //"local.package.repository" // temporary solution, should be external
})
public class MyConfig {}

solved the issue. More on that in the documentation

MongoDB uses two different drivers for imperative (synchronous/blocking) and reactive (non-blocking) data access. You must create a connection by using the Reactive Streams driver to provide the required infrastructure for Spring Data’s Reactive MongoDB support. Consequently, you must provide a separate configuration for MongoDB’s Reactive Streams driver. Note that your application operates on two different connections if you use reactive and blocking Spring Data MongoDB templates and repositories.

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