mongodb spring 配置文件
在使用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 配置文件的更多相关文章
- 你不知道的Spring配置文件
Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的"图纸".Java EE程序员必须学会并灵活应用这份"图纸&quo ...
- Spring配置文件详解
转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常有用 spring配置文件是用于指导Sp ...
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
转自:http://www.cnblogs.com/wj-wangjun/archive/2009/10/21/1587624.html Hibernate SQL方言 (hibernate.dial ...
- Spring配置文件详解 - applicationContext.xml文件路径
spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...
- Spring配置文件详解 – applicationContext.xml文件路径
Spring配置文件详解 – applicationContext.xml文件路径 Java编程 spring的配置文件applicationContext.xml的默 ...
- spring配置文件详解--真的蛮详细
spring配置文件详解--真的蛮详细 转自: http://book.51cto.com/art/201004/193743.htm 此处详细的为我们讲解了spring2.5的实现原理,感觉非常 ...
- Spring 配置文件详解 (以2.5为例)
转载自:http://blog.csdn.net/zzjjiandan/article/details/22922847 Spring配置文件是用于指导Spring工厂进行Bean生 ...
- Spring配置文件外部化配置及.properties的通用方法
摘要:本文深入探讨了配置化文件(即.properties)的普遍应用方式.包括了Spring.一般的.远程的三种使用方案. 关键词:.properties, Spring, Disconf, Java ...
- Spring配置文件详解:<context:annotation-config/>和<context:component-scan base-package=""/>和<mvc:annotation-driven />
<context:annotation-config/> 在基于主机方式配置Spring时,Spring配置文件applicationContext.xml,你可能会见<contex ...
随机推荐
- SQL记录-PLSQL游标
PL/SQL游标 Oracle会创建一个存储区域,被称为上下文区域,用于处理SQL语句,其中包含需要处理的语句,例如所有的信息,行数处理,等等. 游标是指向这一上下文的区域. PL/SQL通过控制光标 ...
- 【DS】排序算法之冒泡排序(Bubble Sort)
一.算法思想 冒泡排序是排序算法中比较有意思的一种排序方法,也很简单.其算法思想如下: 1)比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2)对每一对相邻元素作同样的工作,从开始第一对到结尾 ...
- Extending Markov to Hidden Markov
Extending Markov to Hidden Markov a tutorial on hidden markov models, Hidden Markov Models, hidden m ...
- WebSlides - 轻松制作漂亮的 HTML 幻灯片(演讲稿)
WebSlides 是一个开源的 HTML 幻灯片项目,能够帮助熟悉前端语言的开发者快速制作出效果精美的幻灯片.页面中的每个 <section> 都是一个独立的幻灯片,只需要很少的 CSS ...
- gcc初步窥探
由于没有上过Linux编程这门课,所以Linux学得很水啊!!用来用去都是ls -al ; cd .. ;这些渣命令,尤其gcc都不知道什么东西来的,所以先学一下吧. 一.程序的编译过程 对于GUN编 ...
- 【配置】Spring Struts配置信息
- android ViewPager之PagerAdapter中View的重用
在写PagerAdapter的时候,需要重写instantiateItem(ViewGroup container ,int position) 此方法中,将需要加载的View,添加到conta ...
- Paint House
There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...
- Redis常见业务场景应用
一定时间范围内不可重复发短信问题 Redis实现消息队列 Redis实现Session共享 ...
- [WC2008]游览计划 「斯坦那树模板」
斯坦那树 百度释义 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在给定的点集和边中寻求最短网络使所有点连通.而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开 ...