LOOK:SpringBoot 配置 MongoDB 多数据源,nginx实战pdf下载

import com.mongodb.MongoCredential;

import com.mongodb.ServerAddress;

import lombok.Getter;

import lombok.Setter;

import org.springframework.data.mongodb.MongoDbFactory;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.util.ArrayList;

import java.util.List;

@Getter

@Setter

public abstract class AbstractMongoConfigure {

private String host;

private int port;

private String username;

private String password;

private String database;

public MongoDbFactory mongoDbFactory() throws Exception {

/*// 无认证的初始化方法

return new SimpleMongoDbFactory(new MongoClient(host, port), database);*/

//有认证的初始化方法

ServerAddress serverAddress = new ServerAddress(host, port);

List mongoCredentialList = new ArrayList<>();

MongoCredential mongoCredential = MongoCredential.createCredential(username, database, password.toCharArray());

mongoCredentialList.add(mongoCredential);

return new SimpleMongoDbFactory(new MongoClient(serverAddress, mongoCredentialList), database);

}

abstract public MongoTemplate getMongoTemplate() throws Exception;

}

数据源加载需要继承 AbstractMongoConfigure 抽象类,有多少个数据源就需要新建多少个数据源加载类

3.1、第一个数据源

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Primary;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration

@EnableMongoRepositories(basePackages = {"com.tcl.dc.autodata.dao.ba

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

se"}, mongoTemplateRef = “mongoTemplate”)

@ConfigurationProperties(prefix = “basic.spring.data.mongodb”)

public class BasicMongoConfig extends AbstractMongoConfigure {

@Primary

@Bean(name = “mongoTemplate”)

@Override

public MongoTemplate getMongoTemplate() throws Exception {

return new MongoTemplate(mongoDbFactory());

}

}

其中 basePackages 的值用于相应的基础包,prefix 为 application.properties 中的配置值

3.2、第二个数据源

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.mongodb.core.MongoTemplate;

import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration

@EnableMongoRepositories(basePackages = {“com.tcl.dc.autodata.dao.auth”}, mongoTemplateRef = “authMongoTemplate”)

@ConfigurationProperties(prefix = “auth.spring.data.mongodb”)

public class BasicMongoConfig extends AbstractMongoConfigure {

@Bean(name = “authMongoTemplate”)

@Override

public MongoTemplate getMongoTemplate() throws Exception {

return new MongoTemplate(mongoDbFactory());

}

}

4、注意

1、多个数据源中有一个 bean 需要设置为 mongoTemplate ,且必须添加 @Primary 注解,否则 WebMvcConfigurationSupport.class 等会报错找不到 mongoTemplate

2、Spring Boot 会自动注入 mongoTemplate ,与我们配置的多个数据源有冲突。为了防止默认注入,需要排除自动注入的类。在 Spring Boot 的启动类 Applocation.java 添加排除类注解

@SpringBootApplication(exclude = {

MongoAutoConfiguration.class,

MongoDataAutoConfiguration.class})

5、使用多个数据源

使用时,直接对应注入即可

@Autowired

@Qualifier(value = “mongoTemplate”)

MongoTemplate mongoTemplate;

@Autowired

@Qualifier(value = “authMongoTemplate”)

MongoTemplate authMongoTemplate;

6、可能遇到的问题

1、‘com.mongodb.MongoClient’ that could not be found

详细报错如下:


APPLICATION FAILED TO START


Description:

Parameter 0 of method mongoDbFactory in org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration required a bean of type ‘com.mongodb.MongoClient’ that could not be found.

  • Bean method ‘mongo’ not loaded because auto-configuration ‘MongoAutoConfiguration’ was excluded

Action:

Consider revisiting the conditions above or defining a bean of type ‘com.mongodb.MongoClient’ in your configuration.

原因:重写了 MongoClient 等之后导致原来的自动注入缺少 bean

解决方式:主要是看哪个自动注入的类在引用默认的 MongoClient ,把它排除出去即可,例如:

@SpringBootApplication(exclude = {

MongoAutoConfiguration.class,

MongoDataAutoConfiguration.class})

2、more than one ‘primary’ bean found among candidates

详细报错如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sampleController’: Unsatisfied dependency expressed through field ‘mongoTemplate’; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘org.springframework.data.mongodb.core.MongoTemplate’ available: more than one ‘primary’ bean found among candidates: [logMongoTemplate, userMongoTemplate, mongoTemplate]

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:588) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]

at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.7.RELEASE.jar:4.3.7.RELEASE]

上一篇:腾讯云服务器CentOS7 安装mongodb


下一篇:systemctl start mongod.service 报错 mongod.service: New main PID 1390 does not belong to service, and