【问题标题】:Spring Boot configuration with Morphia?带有 Morphia 的 Spring Boot 配置?
【发布时间】:2015-06-04 14:37:16
【问题描述】:

我不想使用 Spring DATA MongoDB 支持。 我想利用名为 Morphia 的 MongoDB 的 ORM。

https://github.com/mongodb/morphia

我想用 Spring Boot 配置 Morphia。我想以遵循 Spring Boot 理念的方式外部化 Morphia 的配置。

我想利用环境变量来配置 Morphia 属性。

实现这一目标的 Spring Boot 方法是什么?

在一个简单的主程序中,将执行以下操作以使 Morhpia ORM 正常工作。

private Morphia morphia; 
private MongoClient mongoClient; 

morphia = new Morphia();
// Person is an entity object with Morphia annotations
morphia.map(Person.class);

// THESE properties MUST be read from environment variables in Spring BOOT.
final String host = "localhost";
final int port = 27017;

mongoClient = new MongoClient(host, port);

//Set database
// this instance would be autowired all data access classes
Datastore ds  = morphia.createDatastore(mongoClient, "dataStoreInstanceId");

// this is how instance would be used in those data accesses classes
Person p = ds.find(Person.class, "username", "john").get();

【问题讨论】:

    标签: java spring mongodb spring-boot morphia


    【解决方案1】:

    我正在为 spring boot 做一个启动器,这里是 repo. 非常好用,过几天会在 maven Central 中更新它。

    【讨论】:

      【解决方案2】:

      类似 Spring Boot 的方法是创建一个具有所需属性的 AutoConfiguration,它将 Datastore 的实例创建为 bean。

      Reference Guide 中,您将了解如何设置属性和连接到 MongoDB。

      Morphia 的自动配置可能如下所示:

      @Configuration
      public class MorphiaAutoConfiguration {
      
          @Autowired
          private MongoClient mongoClient; // created from MongoAutoConfiguration
      
          @Bean
          public Datastore datastore() {
              Morphia morphia = new Morphia();
      
              // map entities, there is maybe a better way to find and map all entities
              ClassPathScanningCandidateComponentProvider entityScanner = new ClassPathScanningCandidateComponentProvider(true);
              entityScanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class));
              for (BeanDefinition candidate : scanner.findCandidateComponents("your.basepackage")) { // from properties?
                  morphia.map(Class.forName(candidate.getBeanClassName()));
              }
      
              return morphia.createDatastore(mongoClient, "dataStoreInstanceId"); // "dataStoreInstanceId" may come from properties?
          }
      }
      

      然后您可以按照通常的方式在其他 Spring bean 中自动装配您的 Datastore:

          @Autowired
          private Datastore datastore;
      

      如果某些点不正确或不清楚,请查看 Spring Boot 中现有的 *AutoConfiguration 类。

      【讨论】:

      • 我主要关心的是不使用实际上为 MongoAutoConfiguration 提供动力的 Spring DATA,我想要一种独立于 spring data dependency 的方法。如果 Spring Data 和 Moprhia 之间没有冲突,那么我可以接受你的方法。怎么说?
      • 好的,那么您将在 AutoConfiguration 中注入 MongoProperties 而不是 MongoClient,并自己创建一个带有 MongoProperties.createMongoClient() 的 MongoClient。您可能需要@EnableConfigurationProperties(MongoProperties.class)。那么你就独立于 Spring Data 了。
      • 通过查看MongoAutoConfiguration,我想说我的原始解决方案在不依赖 Spring Data 的情况下也是可能的。当 MongoDB 在类路径中时,这会自动应用。
      • 谢谢,看来是这样。 !
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 2019-09-28
      • 1970-01-01
      • 2016-04-17
      • 2019-03-18
      • 2018-07-31
      • 2016-03-14
      相关资源
      最近更新 更多