一起使用 spring-data-mongodb 和 spring-data-neo4j

Using spring-data-mongodb and spring-data-neo4j together

如何在同一个 spring-boot 应用程序中使用 spring-data-mongodb 和 spring-data-neo4j?

我可以按照 "getting started" 指南轻松使用一个或另一个,但是一旦我尝试将 Neo4J 添加到 MongoDB 应用程序,我就会遇到运行时错误,例如:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'application': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property findAll found for type MongoBook!

我在 https://github.com/afaulconbridge/myspring-mongo-neo

设置了一个最小示例

您可以 try this project 同时使用 JPA 和 Neo4J。该结构在技术上也应该与 Mongo 一起使用。请注意 Mongo 不支持事务的概念,因此您可能不必为每个 Spring 数据项目定义一个明确的事务管理器。

正如@manish 指出的那样,您需要使 Spring Data MongoDB 和 Spring Data Neo4J 扫描单独的包。即

@EnableMongoRepositories(basePackageClasses=MongoBook.class)
@EnableNeo4jRepositories(basePackageClasses=NeoAuthor.class)

我已经用解决方案更新了 https://github.com/afaulconbridge/myspring-mongo-neo 的示例项目。

即使在同一个包中,您也应该能够分别使用 excludeFiltersincludeFilters 参数(在大多数情况下 includeFilters 就足够了)

@EnableMongoRepositories(basePackageClasses=MongoBook.class,
  includeFilters ={@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE,
  classes = {MongoRepository.class}))

@EnableNeo4jRepositories(basePackageClasses=NeoAuthor.class,
  includeFilters ={@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, 
  classes = {NeoRepository.class}))

来自 includeFilters() 描述

Specifies which types are eligible for component scanning. Further narrows the set of candidate components from everything in {#basePackages()} to everything in the base packages that matches the given filter or filters.