<?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.1.xsd
  http://www.springframework.org/schema/data/mongo
  http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<!-- 加载mongodb的属性配置文件 -->
	<context:property-placeholder location="classpath:mongodb.properties"/>

	<!-- 我们使用的mongodb的版本是3.X,所以在构造这段话的时候要根据 Mongo 类的构造函数来构造，不同版本可能会造成构造函数的不同 -->
	<!--<mongo:mongo-client id="mongo" host="${mongo.host}" port="${mongo.port}">

		<mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
							  threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
							  connect-timeout="${mongo.connectTimeout}"
							  max-wait-time="${mongo.maxWaitTime}"
							  socket-keep-alive="${mongo.socketKeepAlive}"
							  socket-timeout="${mongo.socketTimeout}" />
	</mongo:mongo-client>-->
	<!-- 1.7版本+不支持mongo,支持mongo-client,options为client-options -->
	<!-- Usage of 'UserCredentials' with 'MongoClient' is no longer supported -->
	<!-- 配置mongodb连接池 ,创建mongoClient实例-->
	<!--credentials="mongodb://${mongo.user}:${mongo.pwd}@${mongo.host}:${mongo.port}/${mongo
    .defaultDbName}"为什么不起作用
    如果需要验证,mongo-client需要加这句: credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDbName}"
    -->
	<mongo:mongo-client id="mongoClient"  host="${mongo.host}" port="${mongo.port}">
		<mongo:client-options
				connections-per-host="${mongo.connectionsPerHost}"
				min-connections-per-host="${mongo.minConnectionsPerHost}"
				threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
				connect-timeout="${mongo.connectTimeout}"
				max-wait-time="${mongo.maxWaitTime}"
				socket-keep-alive="${mongo.socketKeepAlive}"
				socket-timeout="${mongo.socketTimeout}"
				description="${mongo.description}"
				max-connection-idle-time="${mongo.maxConnectionIdleTime}"
				max-connection-life-time="${mongo.maxConnectionLifeTime}"
				heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}"
				heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}"
				min-heartbeat-frequency="${mongo.minHeartbeatFrequency}"
				heartbeat-frequency="${mongo.heartbeatFrequency}"
		/>
	</mongo:mongo-client>

	<!-- 用户验证 -->
	<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
		<constructor-arg name="username" value="${mongo.user}" />
		<constructor-arg name="password" value="${mongo.pwd}" />
	</bean>
	<!-- mongo的工厂，通过它来取得mongo实例,dbname为mongodb的数据库名，没有的话会自动创建 -->
	<bean id="mongoDbFactory"
		  class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
		<constructor-arg ref="mongoClient" />
		<constructor-arg value="${mongo.defaultDbName}" />
	</bean>

	<bean id="mappingContext"
		  class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />

	<bean id="defaultMongoTypeMapper"
		  class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
		<constructor-arg name="typeKey">
			<null />
		</constructor-arg>
	</bean>

	<!-- collection的映射 -->
	<bean id="mappingMongoConverter"
		  class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
		<constructor-arg name="mappingContext" ref="mappingContext" />
		<property name="typeMapper" ref="defaultMongoTypeMapper" />
	</bean>

	<!-- mongodb的主要操作对象，所有对mongodb的增删改查的操作都是通过它完成 -->
	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
		<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
	</bean>
	<!-- MongoDB GridFS Template 支持，操作mongodb存放的文件 -->
	<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"
							 base-package="com.jeeplus.modules.mongo"/>
	<mongo:gridFsTemplate id="gridFsTemplcate" db-factory-ref="mongoDbFactory" converter-ref="converter" />
</beans>