Spring + Mongo + Morphia Configuration
Spring Configuration
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Beans xmlns = "http://www.springframework.org/schema/beans"
- Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
- Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <! -- Database instance -->
- <Bean id = "mongo" class = "com. mongodb. MongoClient">
- <Constructor-arg name = "host" value = "localhost"/>
- <Constructor-arg name = "port" value = "27017" type = "codeph" text = "codeph"/>
- </Bean>
- <! -- Morphia instance -->
- <Bean id = "morphia" class = "org. mongodb. morphia. Morphia"/>
- <! -- Database name -->
- <Bean id = "dbName" class = "java. lang. String">
- <Constructor-arg index = "0" value = "testDB"/>
- </Bean>
- <! -- Datastore instance -->
- <Bean id = "dataStore" class = "org. mongodb. morphia. DatastoreImpl">
- <Constructor-arg name = "morphia" ref = "morphia"/>
- <Constructor-arg name = "mongo" ref = "mongo"/>
- <Constructor-arg name = "dbName" ref = "dbName"/>
- </Bean>
- </Beans>
Java DAO
- @ Repository
- Public class UserEntityDAO extends BasicDAO <UserEntity, Serializable> {
- @ Autowired
- Protected UserEntityDAO (Datastore dataStore ){
- Super (dataStore );
- EnsureIndexes (); // automatically creates an index.
- }
- }