Spring之xml配置方式快速整合MongoDB

Spring之xml配置方式快速整合MongoDB

文章目录

  • Spring之xml配置方式快速整合MongoDB
  • [1. MongoDB](#1. MongoDB)
    • [1. 引依赖](#1. 引依赖)
    • [2. application.properties](#2. application.properties)
    • [3. application-context-mongodb.xml](#3. application-context-mongodb.xml)

1. MongoDB

MongoDB 版本: 4.2.21

1. 引依赖

xml 复制代码
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>3.4.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.7.2</version>
</dependency>

2. application.properties

properties 复制代码
mongo.username=test
mongo.password=test
mongo.host=127.0.0.1
mongo.port=27017
mongo.dbname=test_doc
mongo.authenticationDbname=admin
mongo.authMechanism=SCRAM-SHA-1

3. application-context-mongodb.xml

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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 有认证的配置 -->
	<mongo:db-factory id="mongoDbFactory" client-uri="mongodb://${mongo.username}:${mongo.password}@${mongo.host}:${mongo.port}/${mongo.dbname}?authSource=${mongo.authenticationDbname}&amp;authMechanism=${mongo.authMechanism}"/>

	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
	</bean>
	
	<!-- 自动扫描 -->
	<mongo:mapping-converter base-package="com.yuan.mongo.page.*" />
	
	<context:annotation-config />
	
	<!-- 自定义注解类 -->
	<context:component-scan base-package="com.yuan.mongo,caom.yuan.utils" />
	
	<bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener" />
	
</beans>
相关推荐
杉之4 小时前
常见前端GET请求以及对应的Spring后端接收接口写法
java·前端·后端·spring·vue
canonical_entropy6 小时前
Nop入门-如何通过配置扩展服务函数的返回对象
spring·mvc·graphql
小李同学_LHY6 小时前
三.微服务架构中的精妙设计:服务注册/服务发现-Eureka
java·spring boot·spring·springcloud
非ban必选7 小时前
spring-ai-alibaba第四章阿里dashscope集成百度翻译tool
java·人工智能·spring
非ban必选7 小时前
spring-ai-alibaba第五章阿里dashscope集成mcp远程天气查询tools
java·后端·spring
爱喝醋的雷达8 小时前
Spring SpringBoot 细节总结
java·spring boot·spring
阁阁下11 小时前
springcloud configClient获取configServer信息失败导致启动configClient注入失败报错解决
后端·spring·spring cloud
chushiyunen11 小时前
dom操作笔记、xml和document等
xml·java·笔记
whisperrr.11 小时前
【spring01】Spring 管理 Bean-IOC,基于 XML 配置 bean
xml·java·spring
天上掉下来个程小白11 小时前
HttpClient-03.入门案例-发送POST方式请求
java·spring·httpclient·苍穹外卖