Spring 没有发现 ReactiveCrudRepository 被注入到其他类中

Par*_*aum 3 spring dependency-injection reactive-programming kotlin spring-boot

问题:

我有一个ReactiveCrudRepository我想在 a 中使用的,RestController但 Spring 没有发现它再被注入。在我将存储库重构为响应式CrudRepository之前(它是之前的),存储库是由 Spring 找到并注入的。

现在我收到这个错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in de.shinythings.microservices.core.product.services.ProductServiceImpl required a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' that could not be found.


Action:

Consider defining a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' in your configuration.
Run Code Online (Sandbox Code Playgroud)

存储库如下所示: 链接到 GitHub

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in de.shinythings.microservices.core.product.services.ProductServiceImpl required a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' that could not be found.


Action:

Consider defining a bean of type 'de.shinythings.microservices.core.product.persistence.ProductRepository' in your configuration.
Run Code Online (Sandbox Code Playgroud)

其余控制器如下所示: 链接到 GitHub

interface ProductRepository : ReactiveCrudRepository<ProductEntity, String> {

    fun findByProductId(productId: Int): Mono<ProductEntity>
}
Run Code Online (Sandbox Code Playgroud)

到目前为止我尝试过的:

我启用了我的debug标志以application.yml从输出中了解更多信息,但这并没有产生有用的见解。

ProductRepositoryProductServiceImpl类中删除了依赖项,以便在启动 Spring 时不会出现上述错误。

然后,我写我自己一个小测试来询问ApplicationContextProductRepository具体做法是:

@SpringBootTest
class BeanLoadingDebugging {

    @Autowired
    private lateinit var applicationContext: ApplicationContext

    @Test
    fun test() {
        val bean = applicationContext.getBean(ProductRepository::class.java)

        Assert.notNull(bean, "Bean not found!")
    }
}
Run Code Online (Sandbox Code Playgroud)

这也行不通!

所以看起来这个存储库只是不想被发现。我仔细检查了这个并尝试了同样的非反应性CrudRepository,发现了这一点。???

全面披露:

我是 Spring / Spring Boot 的新手,很高兴在这里提供任何建议。

完整的代码可以在 GitHub 上找到

小智 5

首先,您必须将依赖项从 更新org.springframework.boot:spring-boot-starter-data-mongodborg.springframework.boot:spring-boot-starter-data-mongodb-reactive

其次,启用如下所示的反应式支持,

@SpringBootApplication
@ComponentScan("de.shinythings")
@EnableReactiveMongoRepositories 
class RecommendationServiceApplication
Run Code Online (Sandbox Code Playgroud)

在这两个更改之后,我可以看到测试de.shinythings.microservices.core.recommendation.BeanLoadingDebugging成功。

深入了解Spring 反应式 mongo