• 请使用“ MongoMappingContext#setAutoIndexCreation(boolean)”或覆盖“MongoConfigurationSupport#autoIndexCreation()”以明确


    http://codingdict.com/questions/35983

    在application.properties文件中禁用自动索引创建

    spring.data.mongodb.auto-index-creation=false
    

    或application.yml文件

    spring:
      data:
        mongodb:
          auto-index-creation: false
    

    创建类MongoConfiguration whit @Configuration批注

    Injetc这种依赖

    private final MongoTemplate mongoTemplate;
    private final MongoConverter mongoConverter;
    

    并添加此方法

    @EventListener(ApplicationReadyEvent.class)
    public void initIndicesAfterStartup() {
    
        log.info("Mongo InitIndicesAfterStartup init");
        var init = System.currentTimeMillis();
    
        var mappingContext = this.mongoConverter.getMappingContext();
    
        if (mappingContext instanceof MongoMappingContext) {
            MongoMappingContext mongoMappingContext = (MongoMappingContext) mappingContext;
            for (BasicMongoPersistentEntity<?> persistentEntity : mongoMappingContext.getPersistentEntities()) {
                var clazz = persistentEntity.getType();
                if (clazz.isAnnotationPresent(Document.class)) {
                    var resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
    
                    var indexOps = mongoTemplate.indexOps(clazz);
                    resolver.resolveIndexFor(clazz).forEach(indexOps::ensureIndex);
                }
            }
        }
    
        log.info("Mongo InitIndicesAfterStartup take: {}", (System.currentTimeMillis() - init));
    }
    

    记住var仅适用于Java 11+

    最后一课,使用龙目岛

    import lombok.RequiredArgsConstructor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.context.event.EventListener;
    import org.springframework.data.mongodb.core.MongoTemplate;
    import org.springframework.data.mongodb.core.convert.MongoConverter;
    import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver;
    import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
    import org.springframework.data.mongodb.core.mapping.Document;
    import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
    
    
    @Slf4j
    @RequiredArgsConstructor
    @Configuration
    public class MongoConfiguration {
    
    private final MongoTemplate mongoTemplate;
    
    private final MongoConverter mongoConverter;
    
    @EventListener(ApplicationReadyEvent.class)
    public void initIndicesAfterStartup() {
    
            log.info("Mongo InitIndicesAfterStartup init");
            var init = System.currentTimeMillis();
    
            var mappingContext = this.mongoConverter.getMappingContext();
    
            if (mappingContext instanceof MongoMappingContext) {
                MongoMappingContext mongoMappingContext = (MongoMappingContext) mappingContext;
                for (BasicMongoPersistentEntity<?> persistentEntity : mongoMappingContext.getPersistentEntities()) {
                    var clazz = persistentEntity.getType();
                    if (clazz.isAnnotationPresent(Document.class)) {
                        var resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
    
                        var indexOps = mongoTemplate.indexOps(clazz);
                        resolver.resolveIndexFor(clazz).forEach(indexOps::ensureIndex);
                    }
                }
            }
    
            log.info("Mongo InitIndicesAfterStartup take: {}", (System.currentTimeMillis() - init));
        }
    
    }
    
     
  • 相关阅读:
    Centos下安装FastDFS
    elasticsearch5.6.8安装和配置
    maven利用插件发布项目到tomcat
    vue-cli安装以及mongodb原生操作
    J2EE基础
    Debian Gun/linux基本用法
    SpringCloud 中如何使微服务只能被指定的程序调用
    CentOS 使用Docker 部署多台Springboot程序,并用Nginx做负载均衡
    CentOS上没有ifcofig命令
    CentOS yum安装时报错 yum install cannot find a vaild baseur
  • 原文地址:https://www.cnblogs.com/kelelipeng/p/13448080.html
Copyright © 2020-2023  润新知