温馨提示:本文翻译自stackoverflow.com,查看原文请点击:spring - Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSuppor

spring - 请使用“ MongoMappingContext#setAutoIndexCreation(boolean)”或覆盖“ MongoConfigurationSuppor

发布于 2020-04-03 23:33:06

我浏览了https://docs.spring.io/spring-data/mongodb/docs/2.2.0.RC1/reference/html/#mapping-usage和网络上的其他来源,但该解决方案不适用于我。

我正在使用Spring Boot 2.2.2.RELEASESpring Data Mongo在此示例中,在Model / Pojo字段级别,我们使用

@Indexed(name = AppConstants.FIRSTNAME_INDEX, direction = IndexDirection.ASCENDING)
private String firstName;

错误:

Please use 'MongoMappingContext#setAutoIndexCreation(boolean)' or override 'MongoConfigurationSupport#autoIndexCreation()' to be explicit.
However, we recommend setting up indices manually in an application ready block. You may use index derivation there as well.

> -----------------------------------------------------------------------------------------
> @EventListener(ApplicationReadyEvent.class)
> public void initIndicesAfterStartup() {
>
>     IndexOperations indexOps = mongoTemplate.indexOps(DomainType.class);
>
>     IndexResolver resolver = new MongoPersistentEntityIndexResolver(mongoMappingContext);
>     resolver.resolveIndexFor(DomainType.class).forEach(indexOps::ensureIndex);
> }
> -----------------------------------------------------------------------------------------

正如日志中建议的那样,我实现了,但没有看到方法setAutoIndexCreation。

public class MongoConfig extends AbstractMongoClientConfiguration {

    @Override
    public MongoClient mongoClient() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected String getDatabaseName() {
        // TODO Auto-generated method stub
        return null;
    }
}

查看更多

提问者
PAA
被浏览
456
5 2020-02-09 05:49

在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));
    }

}

发布
问题

分享
好友

手机
浏览

扫码手机浏览