Spring Boot多数据源配置(二)MongoDB
在Spring Boot多数据源配置(一)durid、mysql、jpa 整合中已经讲过了Spring Boot如何配置mysql多数据源。本篇文章讲一下Spring Boot如何配置mongoDB多数据源。
配置文件
spring:
#mongo配置
data:
mongodb:
statis:
database: kxlist_statis
uri: 192.168.1.115:27017
list:
database: kxlist_list
uri: 192.168.1.115:27017
JAVA文件:
- 总的配置:
@Configuration
public class MultipleMongoProperties {
@Bean(name="statisMongoProperties")
@Primary
@ConfigurationProperties(prefix="spring.data.mongodb.statis")
public MongoProperties statisMongoProperties() {
System.out.println("-------------------- statisMongoProperties init ---------------------");
return new MongoProperties();
}
@Bean(name="listMongoProperties")
@ConfigurationProperties(prefix="spring.data.mongodb.list")
public MongoProperties listMongoProperties() {
System.out.println("-------------------- listMongoProperties init ---------------------");
return new MongoProperties();
}
}
- statis数据源配置
@Configuration
@EnableMongoRepositories(basePackages = "com.kxlist.statistics.domain.statis", mongoTemplateRef = "statisMongo")
public class StatisMongoMongoTemplate {
@Autowired
@Qualifier("statisMongoProperties")
private MongoProperties mongoProperties;
@Primary
@Bean(name = "statisMongo")
public MongoTemplate statisMongoTemplate() throws Exception {
return new MongoTemplate(statisFactory(this.mongoProperties));
}
@Bean
@Primary
public MongoDbFactory statisFactory(MongoProperties mongoProperties) throws Exception {
ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());
return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());
}
}
- list数据源配置
和statis的配置很相似
@Configuration
@EnableMongoRepositories(basePackages = "com.kxlist.statistics.domain.list", mongoTemplateRef = "listMongo")
public class ListMongoTemplate {
@Autowired
@Qualifier("listMongoProperties")
private MongoProperties mongoProperties;
@Bean(name = "listMongo")
public MongoTemplate listTemplate() throws Exception {
return new MongoTemplate(listFactory(this.mongoProperties));
}
@Bean
public MongoDbFactory listFactory(MongoProperties mongoProperties) throws Exception {
ServerAddress serverAdress = new ServerAddress(mongoProperties.getUri());
return new SimpleMongoDbFactory(new MongoClient(serverAdress), mongoProperties.getDatabase());
}
}
在相应的包下建立实体类和Repository就可以了。
首发地址
http://www.devzxd.top/2017/06/06/springboot-datasource-mongo.html
Spring Boot多数据源配置(二)MongoDB的更多相关文章
- spring boot多数据源配置(mysql,redis,mongodb)实战
使用Spring Boot Starter提升效率 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfi ...
- spring boot(12)-数据源配置原理
本篇讲的不仅是数据源配置,这也是spring boot实现自动配置的一部分.要理解数据源的配置原理,首先要理解第十篇tomcat连接池的配置 数据源配置源码 这里截取org.springframewo ...
- 三、Spring Boot 多数据源配置
下面一个Java类是已经写好的根据配置文件动态创建多dataSource的代码,其原理也很简单,就是读取配置文件,根据配置文件中配置的数据源数量,动态创建dataSource并注册到Spring中. ...
- Spring Boot Druid数据源配置
package com.hgvip.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.su ...
- 21. Spring Boot Druid 数据源配置解析
1.数据源配置属性类源码 package org.springframework.boot.autoconfigure.jdbc; @ConfigurationProperties( prefix = ...
- spring boot 多数据源配置与使用
在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文 ...
- Spring Boot多数据源配置与使用
在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文件中配置连接参数即可.但是往往随着业务量发展,我们通常会进行数据库拆分或是引入其他数据库 ...
- Spring Boot (14) 数据源配置原理
数据源配置源码 这里截取org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration的部分源码,主要介绍Tomcat和Hika ...
- Spring Boot☞ 多数据源配置(二):Spring-data-jpa
效果图: 代码区: package com.wls.integrateplugs.datasource; import org.springframework.beans.factory.annota ...
随机推荐
- Vuex进阶
1.插件 下面以一个对state进行持久化存储的插件为例进行介绍: 代码结构: saveInLocal.js export default function (store) { if (localSt ...
- maven简单了解,没有Maven和使用Maven的区别
Maven提供了开发人员构建一个完整的生命周期框架.开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周期.Maven让开发人员的工作更轻松,同时创建报表,检查,构建和 ...
- [USACO07FEB]银牛派对Silver Cow Party---最短路模板题
银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...
- Android笔记(预安装APK)
一般一个安卓的产品在出厂时,会预安装许多APK,关于这些APP,主要分为下面这几类 1.系统级别APK 这一类应用一般是:电话/设置或者厂家自己特定的应用. 2.系统预安装APK 因为商业原因,产品出 ...
- mysql完全卸载大全
如何在Linux下卸载MySQL数据库呢? 下面总结.整理了一下Linux平台下卸载MySQL的方法. MySQL的安装主要有三种方式:二进制包安装(Using Generic Binaries).R ...
- singnal 13 was raised
在app运行过程中按下home键或者其他原因app被挂起,socket连接不会断开,服务器为了节省资源,在一段时间后会主动关闭这个连接.当玩家再次切回到游戏后,前端并不知道这个连接已经断开了,继续通过 ...
- Problem 1002-2017 ACM/ICPC Asia Regional Shenyang Online
网络赛:2017 ACM/ICPC Asia Regional Shenyang Online 题目来源:cable cable cable Problem Description: Connecti ...
- SpringMVC使用ModelAndView的相对路径和绝对路径的问题
例如:abc/a/a.jsp,想要跳转到根目录的b.jsp 使用 ModelAndView 跳转, 若引用:org.springframework.web.portlet.ModelAndView 这 ...
- Logger日志配置级别说明及设置方法、说明
日志记录器(Logger)是日志处理的核心组件.log4j具有5种正常级别(Level).日志记录器(Logger)的可用级别Level (不包括自定义级别 Level), 以下内容就是摘自log4j ...
- oracle时间计算
1.在给定时间上加减天数 SQL> select to_char(to_date('20170531000000','yyyymmdd HH24:MI:SS')+4,'YYYYMMDDHH24M ...