本章将介绍基于 Spring 传统 XML 的方式去配置 com.mongodb.client.MongoClient 的实例。
Spring 支持使用传统的 <beans/> XML 命名空间方式来向容器注册 com.mongodb.client.MongoClient 的实例。但由于它是通用的,所以 XML配置可能相当冗长。XML(xmlns:mongo="http://www.springframework.org/schema/data/mongo")命名空间是配置常用对象(如 Mongo 实例)的一个更好的选择。mongo 命名空间可以让你创建 Mongo 实例的服务器位置、副本集和选项。例如:
(1)创建 resouces/META-INF/applicationContent.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:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <!-- bean 默认名称为 'mongo' --> <mongo:mongo-client host="localhost" port="27017"/> </beans>
上面配置文件中,引入了 xmlns:mongo 命名空间,XML Schema 地址为 https://www.springframework.org/schema/data/mongo/spring-mongo.xsd。
(2)修改 @SpringBootApplication 启动类,导入我们的 XML 配置文件,如下:
package com.hxstrive.springdata.mongodb; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ImportResource; /** * 启动类 * @author hxstrive.com */ @SpringBootApplication @ImportResource(locations = {"classpath*:META-INF/applicationContext.xml"}) public class SpringdataMongodbDemo1Application { public static void main(String[] args) { SpringApplication.run(SpringdataMongodbDemo1Application.class, args); } }
上面使用了 @ImportResource 注解从类路径中导入 XML 配置文件。
(3)客户端,通过创建一个简单的 @SpringBootTest 来测试我们配置的 MongoClient 实例是否可用。如下:
package com.hxstrive.springdata.mongodb; import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.MongoIterable; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest class BaseXmlConfigTest { // 这是因为 XML 默认的实例名称为 mongo @Autowired @Qualifier("mongo") private MongoClient mongoClient; @Test void contextLoads() { System.out.println("mongoClient=" + mongoClient); MongoDatabase mongoDatabase = mongoClient.getDatabase("test"); // 获取 test 数据库中所有的集合名称 MongoIterable<String> mongoIterable = mongoDatabase.listCollectionNames(); for(String name : mongoIterable) { System.out.println(name); } } }
运行示例,输出如下:
mongoClient=com.mongodb.client.internal.MongoClientImpl@24a2e565 inventory phrase test orders ex_entity_test ex_entity_test1 map_reduce_example users
下面的例子显示了怎样使用 MongoClientSettings(<mongo:client-settings>)对 MongoClient 进行更高级的配置。例如:
<?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:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo.xsd"> <mongo:mongo-client host="localhost" port="27017"> <mongo:client-settings connection-pool-max-connection-life-time="10" connection-pool-min-size="10" connection-pool-max-size="20" connection-pool-maintenance-frequency="10" connection-pool-maintenance-initial-delay="11" connection-pool-max-connection-idle-time="30" connection-pool-max-wait-time="15" /> </mongo:mongo-client> </beans>
下面将在基于 XML 配置中,使用 <mongo:client-settings> 对 MongoClient 进行复制集配置,例如:
<mongo:mongo-client id="replicaSetMongo" replica-set="rs0"> <mongo:client-settings cluster-hosts="127.0.0.1:27017,localhost:27018" /> </mongo:mongo-client>