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

io.github.ganchix.morphia.configuration.MorphiaAutoConfiguration Maven / Gradle / Ivy

package io.github.ganchix.morphia.configuration;

import com.mongodb.MongoClient;
import io.github.ganchix.morphia.utils.MorphiaUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.annotations.Entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * AutoConfiguration class.
 * 

* Created by Rafa on 05/04/17. */ @Configuration @AutoConfigureAfter(MongoAutoConfiguration.class) public class MorphiaAutoConfiguration { @Autowired private MongoClient mongoClient; @Autowired private MongoTemplate mongoTemplate; @Autowired private ApplicationContext applicationContext; @Bean Morphia morphia() { return new Morphia(); } @Bean public Datastore datastore(Morphia morphia) throws ClassNotFoundException, IOException { List packageNamesFromApplication = MorphiaUtils.getApplicationPackageName(applicationContext); Set> classes = packageNamesFromApplication .parallelStream() .flatMap(packageName -> MorphiaUtils.getClasses(packageName).parallelStream()) .collect(Collectors.toSet()); classes.parallelStream() .filter(clazz -> Objects.nonNull(clazz.getAnnotation(Entity.class))) .forEach( clazz ->morphia.map(clazz)); Datastore dataStore = morphia.createDatastore(mongoClient, mongoTemplate.getDb().getName()); dataStore.ensureIndexes(); return dataStore; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy