Class MongoRepositoryContext

java.lang.Object
org.kiwiproject.spring.context.MongoRepositoryContext

public class MongoRepositoryContext extends Object
This class generates the context and factory necessary to programmatically initialize Spring Data MongoRepository interfaces.

This also provides a default map of repositories. For simple basic usages, simply call getRepository(java.lang.Class<T>) with the appropriate type, and it will return (or create-and-return) the repository instance.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new instance using the given MongoDB URI.
    MongoRepositoryContext(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate)
    Create a new instance that will use the given MongoTemplate.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    attachListeners(org.springframework.context.ApplicationListener<?>... listeners)
    Attach one or more listeners to the context.
    org.springframework.data.mongodb.repository.support.MongoRepositoryFactory
    Direct access to the MongoRepositoryFactory in case it is needed for whatever reason.
    org.springframework.data.mongodb.core.MongoTemplate
    The MongoTemplate initialized by this context.
    final <T extends org.springframework.data.mongodb.repository.MongoRepository<?, ?>>
    T
    getRepository(Class<T> repositoryInterfaceClass)
    Get a MongoRepository having the given class.
    org.springframework.context.support.GenericApplicationContext
    The Spring application context used by this instance, in case it is needed for whatever reason.
    static org.springframework.data.mongodb.core.MongoTemplate
    Convenience method to initialize a new MongoTemplate from the given MongoDB connection string.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MongoRepositoryContext

      public MongoRepositoryContext(String mongoUri)
      Create a new instance using the given MongoDB URI.
      Parameters:
      mongoUri - the MongoDB connection string, e.g. mongodb://my-mongo-server.test:27017
    • MongoRepositoryContext

      public MongoRepositoryContext(org.springframework.data.mongodb.core.MongoTemplate mongoTemplate)
      Create a new instance that will use the given MongoTemplate.
      Parameters:
      mongoTemplate - the MongoTemplate to use
  • Method Details

    • getRepository

      public final <T extends org.springframework.data.mongodb.repository.MongoRepository<?, ?>> T getRepository(Class<T> repositoryInterfaceClass)
      Get a MongoRepository having the given class.
      Type Parameters:
      T - the repository class
      Parameters:
      repositoryInterfaceClass - the repository class
      Returns:
      the singleton repository instance
    • attachListeners

      public void attachListeners(org.springframework.context.ApplicationListener<?>... listeners)
      Attach one or more listeners to the context.
      Parameters:
      listeners - the listeners to attach
      See Also:
      • AbstractApplicationContext.addApplicationListener(ApplicationListener)
    • initializeMongoTemplate

      public static org.springframework.data.mongodb.core.MongoTemplate initializeMongoTemplate(String mongoUri)
      Convenience method to initialize a new MongoTemplate from the given MongoDB connection string.

      The returned instance is configured with write concern set to WriteConcern.ACKNOWLEDGED.

      This method also registers BsonUndefinedToNullStringConverter as a custom converter.

      Parameters:
      mongoUri - the MongoDB connection string, e.g. mongodb://my-mongo-server.test:27017
      Returns:
      a new MongoTemplate instance
    • getMongoTemplate

      public org.springframework.data.mongodb.core.MongoTemplate getMongoTemplate()
      The MongoTemplate initialized by this context.
    • getFactory

      public org.springframework.data.mongodb.repository.support.MongoRepositoryFactory getFactory()
      Direct access to the MongoRepositoryFactory in case it is needed for whatever reason.
    • getSpringContext

      public org.springframework.context.support.GenericApplicationContext getSpringContext()
      The Spring application context used by this instance, in case it is needed for whatever reason.