Class 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 Detail

      • 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 Detail

      • 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