All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sdase.commons.spring.boot.mongodb.SdaMongoDbConfiguration Maven / Gradle / Ivy

Go to download

A library to bootstrap services easily that follow the patterns and specifications promoted by the SDA SE

The newest version!
/*
 * Copyright 2022- SDA SE Open Industry Solutions (https://www.sda.se)
 *
 * Use of this source code is governed by an MIT-style
 * license that can be found in the LICENSE file or at
 * https://opensource.org/licenses/MIT.
 */
package org.sdase.commons.spring.boot.mongodb;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.index.IndexOperations;
import org.springframework.data.mongodb.core.index.IndexResolver;
import org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexResolver;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * Enables features that make a Spring Boot service using MongoDB ready to use within the SDA
 * platform.
 *
 * 

So far this covers: * *

    *
  • {@linkplain org.springframework.core.convert.converter.Converter Converter} for {@linkplain * java.time.ZonedDateTime} *
* *

Not covered: * *

    *
  • Mongo Client Options are only configurable via connection uri *
  • CA certificate can't be configured via environment variable *
*/ @AutoConfiguration @PropertySource("classpath:/org/sdase/commons/spring/boot/mongodb/defaults.properties") public class SdaMongoDbConfiguration { private static final Logger LOG = LoggerFactory.getLogger(SdaMongoDbConfiguration.class); private final MongoTemplate mongoTemplate; public SdaMongoDbConfiguration(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } /** * Since it's not recommended to automatically create indices via {@code * spring.data.mongodb.auto-index-creation=true}. Have a look here: https://github.com/spring-projects/spring-data-mongodb/issues/3049 */ @EventListener(ContextRefreshedEvent.class) public void initIndicesAfterStartup() { var mappingContext = mongoTemplate.getConverter().getMappingContext(); IndexResolver resolver = new MongoPersistentEntityIndexResolver(mappingContext); // consider only entities that are annotated with @Document mappingContext.getPersistentEntities().stream() .filter(it -> it.isAnnotationPresent(Document.class)) .forEach( it -> { IndexOperations indexOps = mongoTemplate.indexOps(it.getType()); resolver .resolveIndexFor(it.getType()) .forEach( indexDefinition -> { indexOps.ensureIndex(indexDefinition); LOG.info("Ensured index '{}'", indexDefinition.getIndexOptions()); }); }); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy