Package org.kiwiproject.spring.context
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 DataMongoRepositoryinterfaces.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 MongoRepositoryContext(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
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidattachListeners(org.springframework.context.ApplicationListener<?>... listeners)Attach one or more listeners to the context.<T extends org.springframework.data.mongodb.repository.MongoRepository<?,?>>
TgetRepository(Class<T> repositoryInterfaceClass)Get a MongoRepository having the given class.static org.springframework.data.mongodb.core.MongoTemplateinitializeMongoTemplate(String mongoUri)Convenience method to initialize a newMongoTemplatefrom the given MongoDB connection string.
-
-
-
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 givenMongoTemplate.- 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 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
-
-