Package org.kiwiproject.spring.context
Class MongoRepositoryContext
java.lang.Object
org.kiwiproject.spring.context.MongoRepositoryContext
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
ConstructorsConstructorDescriptionMongoRepositoryContext(String mongoUri) 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 givenMongoTemplate. -
Method Summary
Modifier and TypeMethodDescriptionvoidattachListeners(org.springframework.context.ApplicationListener<?>... listeners) Attach one or more listeners to the context.org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryDirect access to theMongoRepositoryFactoryin case it is needed for whatever reason.org.springframework.data.mongodb.core.MongoTemplateTheMongoTemplateinitialized by this context.final <T extends org.springframework.data.mongodb.repository.MongoRepository<?,?>>
TgetRepository(Class<T> repositoryInterfaceClass) Get a MongoRepository having the given class.org.springframework.context.support.GenericApplicationContextThe Spring application context used by this instance, in case it is needed for whatever reason.static org.springframework.data.mongodb.core.MongoTemplateinitializeMongoTemplate(String mongoUri) Convenience method to initialize a newMongoTemplatefrom the given MongoDB connection string.
-
Constructor Details
-
MongoRepositoryContext
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 givenMongoTemplate.- 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 newMongoTemplatefrom the given MongoDB connection string.The returned instance is configured with write concern set to
WriteConcern.ACKNOWLEDGED.This method also registers
BsonUndefinedToNullStringConverteras 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()TheMongoTemplateinitialized by this context. -
getFactory
public org.springframework.data.mongodb.repository.support.MongoRepositoryFactory getFactory()Direct access to theMongoRepositoryFactoryin 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.
-