日期:2014-05-16  浏览次数:20580 次

mongodb(3)Mongodb Configuration and Class
mongodb(3)Mongodb Configuration and Class

1. Spring XML:
Some concept in Spring data configuration:
<repositories base-package="com.acme.repositories">
  <context:exclude-filter type="regex" expression=".*SomeRepository" />
</repositories>

<repositories base-package="com.acme.repository">
  <repository id="userRepository" repository-impl-ref="customRepositoryImplementation" />
</repositories>

<bean id="customRepositoryImplementation" class="…">
  <!-- further configuration -->
</bean>

interface UserRepositoryCustom {
  public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
  public void someCustomMethod(User user) {
    // Your custom implementation
  }
}

There are a lot of customized codes and configuration below. But from the blog, the simple example is as follow:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
   http://www.springframework.org/schema/data/mongo
   http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">

<!-- Activate Spring Data MongoDB repository support -->
  <mongo:repositories base-package="com.sillycat.easynosql.dao.mongodb.repository" />
 
  <!-- MongoDB host -->
<mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/>

<!-- Template for performing MongoDB operations -->
  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
  c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/>

<!-- Service for initializing MongoDB with sample data using MongoTemplate -->
<bean id="initMongoService" class="com.sillycat.easynosql.dao.mongodb.init.InitMongoService" init-method="init"/>
</beans>

2. Annotation in POJO
package com.sillycat.easynosql.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Role {

@Id
private String id;

private Integer role;
...snip getter and setter...
}

package com.sillycat.easynosql.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.da