在使用spring-data-mongodb中,需要配置spring文件,如下:

  mongodb.xml

<?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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 加载mongodb的属性配置文件 -->
<context:property-placeholder location="classpath:config/mongodb.properties" /> <!-- 定义mongo对象,对应的是mongodb官方jar包中的Mongo,replica-set设置集群副本的ip地址和端口 -->
<mongo:mongo id="mongo" replica-set="${mongo.hostport}">
<!-- 一些连接属性的设置 -->
<mongo:options
connections-per-host="${mongo.connectionsPerHost}"
threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
connect-timeout="${mongo.connectTimeout}"
max-wait-time="${mongo.maxWaitTime}"
auto-connect-retry="${mongo.autoConnectRetry}"
socket-keep-alive="${mongo.socketKeepAlive}"
socket-timeout="${mongo.socketTimeout}"
slave-ok="${mongo.slaveOk}"
write-number="1"
write-timeout="0"
write-fsync="true"/>
</mongo:mongo>
<mongo:db-factory dbname="database" mongo-ref="mongo" />
<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
<constructor-arg name="username" value="${mongo.username}" />
<constructor-arg name="password" value="${mongo.psw}" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongo" />
<constructor-arg name="databaseName" value="test" />
<constructor-arg ref="userCredentials" />
</bean> </beans>

  通过上面的配置,我们通过获取注入Template对mongo数据库操作。  

这其中有些需要认识的类:

  1. Mongo.class

    这个类中需要知道在xml配置文件中使用的构造方法:

    public Mongo( String host , MongoOptions options )
throws UnknownHostException {
this( new ServerAddress( host ) , options );
}

  

  2. MongoOptions.class

    此类中配置一些Mongo对象的一些属性

public class MongoOptions {

    public MongoOptions(){
reset();
} public MongoOptions(final MongoClientOptions options) {
connectionsPerHost = options.getConnectionsPerHost();
threadsAllowedToBlockForConnectionMultiplier = options.getThreadsAllowedToBlockForConnectionMultiplier();
maxWaitTime = options.getMaxWaitTime();
connectTimeout = options.getConnectTimeout();
socketTimeout = options.getSocketTimeout();
socketKeepAlive = options.isSocketKeepAlive();
autoConnectRetry = options.isAutoConnectRetry();
maxAutoConnectRetryTime = options.getMaxAutoConnectRetryTime();
readPreference = options.getReadPreference();
dbDecoderFactory = options.getDbDecoderFactory();
dbEncoderFactory = options.getDbEncoderFactory();
socketFactory = options.getSocketFactory();
description = options.getDescription();
cursorFinalizerEnabled = options.isCursorFinalizerEnabled();
writeConcern = options.getWriteConcern();
slaveOk = false; // default to false, as readPreference field will be responsible
} // other codes ...
}

  3. UserCredentials.class

    此类被用来提供账号密码的验证类  

package org.springframework.data.authentication;

import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; /**
* Class used to provide credentials for username/password authentication
*
* @author Thomas Risberg
* @author Oliver Gierke
*/
public class UserCredentials { public static final UserCredentials NO_CREDENTIALS = new UserCredentials(null, null); private final String username;
private final String password; /**
* Creates a new {@link UserCredentials} instance from the given username and password. Empty {@link String}s provided
* will be treated like no username or password set.
*
* @param username
* @param password
*/
public UserCredentials(String username, String password) {
this.username = StringUtils.hasText(username) ? username : null;
this.password = StringUtils.hasText(password) ? password : null;
}   // other codes ...
}

  4. MongoTemplate.class

    如下是使用它的构造方法: 

    /**
* Constructor used for a template configuration with user credentials in the form of
* {@link org.springframework.data.authentication.UserCredentials}
*
* @param mongo must not be {@literal null}.
* @param databaseName must not be {@literal null} or empty.
* @param userCredentials
*/
public MongoTemplate(Mongo mongo, String databaseName, UserCredentials userCredentials) {
this(new SimpleMongoDbFactory(mongo, databaseName, userCredentials));
}

使用mongo主要利用其中的Template对数据库操作。

mongodb spring 配置文件的更多相关文章

  1. 你不知道的Spring配置文件

    Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...

  2. Spring配置文件详解

      转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...

  3. Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml

    转自:http://www.cnblogs.com/wj-wangjun/archive/2009/10/21/1587624.html Hibernate SQL方言 (hibernate.dial ...

  4. Spring配置文件详解 - applicationContext.xml文件路径

    spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...

  5. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  6. spring配置文件详解--真的蛮详细

    spring配置文件详解--真的蛮详细   转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...

  7. Spring 配置文件详解 (以2.5为例)

    转载自:http://blog.csdn.net/zzjjiandan/article/details/22922847          Spring配置文件是用于指导Spring工厂进行Bean生 ...

  8. Spring配置文件外部化配置及.properties的通用方法

    摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...

  9. Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />

    <context:annotation-config/> 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<contex ...

随机推荐

  1. sking

    #include #include #include typedef struct SKI{ int data; int x; int y; char flag; int len; }STSKI; i ...

  2. Mask RCNN 原理

    转自:https://blog.csdn.net/ghw15221836342/article/details/80084861 https://blog.csdn.net/ghw1522183634 ...

  3. bzoj千题计划191:bzoj2337: [HNOI2011]XOR和路径

    http://www.lydsy.com/JudgeOnline/problem.php?id=2337 概率不能异或 但根据期望的线性,可以计算出每一位为1的概率,再累积他们的期望 枚举每一位i,现 ...

  4. Eclipse的个性化设置

    Eclipse的个性化设置 1. 在Eclipse中查看JDK源码的配置 a. 点 “window”-> "Preferences" -> "Java&quo ...

  5. shell 判断文件出现次数

    判断 file 文件中 第一个变量 出现次数 awk '{print $1}' file |sort |uniq -c|sort -k1r

  6. Python 入门基础12 --函数基础5 匿名函数、内置函数

    今日内容: 1.三元表达式 2.列表.元组生成式 | 字典生成式 3.递归 4.匿名函数 5.内置函数 一.三元表达式 三元运算符:就是 if...else... 语法糖 前提:if 和 else # ...

  7. networkManger介绍

    http://www.linuxidc.com/Linux/2013-08/88809.htm

  8. ajax请求成功但不执行success-function回调函数的问题

    在success:function(data){}下面加个error:function(){},看看是不是出错了走了error.如果是,说明返回值类型不符合要求. 比如:下面代码返回String类型. ...

  9. 分析占用了大量CPU处理时间的是Java进程中哪个线程

    下面是详细步骤: 1. 首先确定进程的 ID ,可以使用 jps -v 或者 top 命令直接查看 2. 查看该进程中哪个线程占用大量 CPU,执行 top -H -p [PID] 结果如下: 可以发 ...

  10. 如何计算 App 的启动时间

    应用启动场景 事实上 Android 中一个 App 的启动时间可以准确计算的.但是要分场景.也就是说要分开游戏和应用. 大家都知道,在Android中,游戏开发和应用开发是两码事.所以我们需要分开来 ...