问题:
我有一个ReactiveCrudRepository
,我想在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.
存储库如下: link to GitHub
interface ProductRepository : ReactiveCrudRepository<ProductEntity, String> {
fun findByProductId(productId: Int): Mono<ProductEntity>
}
其余控制器如下所示: link to GitHub
@RestController
class ProductServiceImpl(
private val repository: ProductRepository,
private val mapper: ProductMapper,
private val serviceUtil: ServiceUtil
) : ProductService { // implementation left out here }
到目前为止我尝试过的事情:
我在debug
中启用了application.yml
标志,以从输出中了解更多信息,但这没有产生有用的见解。
我从ProductRepository
类中删除了ProductServiceImpl
依赖项,以在启动Spring时不出现上述错误。
然后,我给自己写了一个小测试,要求向ApplicationContext
索取ProductRepository
:
@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!")
}
}
这也不起作用!
因此,似乎不想找到该存储库。我仔细检查了一下,并使用非反应性CrudRepository
进行了同样的尝试,发现了这一点。 ?♂️
完整披露:
我是Spring / Spring Boot的新手,很高兴在这里提出任何建议。
答案 0 :(得分:2)
首先,您必须将依赖项从org.springframework.boot:spring-boot-starter-data-mongodb
更新为org.springframework.boot:spring-boot-starter-data-mongodb-reactive
。
第二,启用如下所示的反应性支持,
@SpringBootApplication
@ComponentScan("de.shinythings")
@EnableReactiveMongoRepositories
class RecommendationServiceApplication
经过这两项更改,我可以看到测试de.shinythings.microservices.core.recommendation.BeanLoadingDebugging
成功。