Search in sources :

Example 11 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project spring-data-mongodb by spring-projects.

the class PerformanceTests method setUp.

@BeforeEach
public void setUp() throws Exception {
    this.mongo = MongoClients.create();
    SimpleMongoClientDatabaseFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(this.mongo, DATABASE_NAME);
    MongoMappingContext context = new MongoMappingContext();
    context.setInitialEntitySet(Collections.singleton(Person.class));
    context.afterPropertiesSet();
    this.converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory), context);
    this.operations = new MongoTemplate(new SimpleMongoClientDatabaseFactory(this.mongo, DATABASE_NAME), converter);
    MongoRepositoryFactoryBean<PersonRepository, Person, ObjectId> factory = new MongoRepositoryFactoryBean<PersonRepository, Person, ObjectId>(PersonRepository.class);
    factory.setMongoOperations(operations);
    factory.afterPropertiesSet();
    this.repository = factory.getObject();
}
Also used : ObjectId(org.bson.types.ObjectId) MongoRepositoryFactoryBean(org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean) MongoMappingContext(org.springframework.data.mongodb.core.mapping.MongoMappingContext) DefaultDbRefResolver(org.springframework.data.mongodb.core.convert.DefaultDbRefResolver) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MappingMongoConverter(org.springframework.data.mongodb.core.convert.MappingMongoConverter) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project kiwi by kiwiproject.

the class PagingQueryRealMongoTest method beforeAll.

@BeforeAll
static void beforeAll() {
    var databaseName = "test_db_" + System.nanoTime();
    LOG.info("Using database name: {}", databaseName);
    var mongoUri = stripTrailingSlash(System.getProperty("realMongoDB.url"));
    var connectionString = f("{}/{}?connectTimeoutMS=500&socketTimeoutMS=500&serverSelectionTimeoutMS=500", mongoUri, databaseName);
    LOG.info("Mongo connection string: {}", connectionString);
    var dbFactory = new SimpleMongoClientDatabaseFactory(connectionString);
    mongoTemplate = new MongoTemplate(dbFactory);
}
Also used : SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 13 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project kiwi by kiwiproject.

the class KiwiSpringMongoQueriesTest method beforeAll.

@BeforeAll
static void beforeAll() {
    var connectionString = MONGO_SERVER_EXTENSION.getConnectionString();
    var mongoClientDbFactory = new SimpleMongoClientDatabaseFactory(connectionString);
    mongoTemplate = new MongoTemplate(mongoClientDbFactory);
}
Also used : SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 14 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project charon by harvies.

the class MongoAutoConfiguration method postProcessBeanDefinitionRegistry.

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry beanDefinitionRegistry) throws BeansException {
    if (multipleDataSourcesProperties == null) {
        return;
    }
    Map<String, MongoProperties> dataSources = multipleDataSourcesProperties.getDataSources();
    for (Map.Entry<String, MongoProperties> stringMongoPropertiesEntry : dataSources.entrySet()) {
        String dataSourceName = stringMongoPropertiesEntry.getKey();
        MongoProperties mongoProperties = stringMongoPropertiesEntry.getValue();
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(MongoTemplate.class);
        genericBeanDefinition.setScope(ConfigurableBeanFactory.SCOPE_SINGLETON);
        ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
        genericBeanDefinition.setConstructorArgumentValues(constructorArgumentValues);
        constructorArgumentValues.addGenericArgumentValue(new SimpleMongoClientDatabaseFactory(mongoProperties.getUri()));
        String mongoTemplateBeanName = dataSourceName + "MongoTemplate";
        beanDefinitionRegistry.registerBeanDefinition(mongoTemplateBeanName, genericBeanDefinition);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) Map(java.util.Map) MongoProperties(org.springframework.boot.autoconfigure.mongo.MongoProperties) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues)

Example 15 with SimpleMongoClientDatabaseFactory

use of org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory in project spring-oxygen by isxcode.

the class FlysqlAutoConfiguration method flysql.

/**
 * 初始化flysql factory
 *
 * @param flysqlProperties configs
 * @param jdbcTemplate               jdbcTemplate
 * @param mongoTemplate              mongoTemplate
 * @since 0.0.1
 */
@Bean("flysql")
@ConditionalOnClass(FlysqlAutoConfiguration.class)
private Flysql flysql(FlysqlProperties flysqlProperties, @Nullable JdbcTemplate jdbcTemplate, @Nullable MongoTemplate mongoTemplate) {
    Map<String, JdbcTemplate> jdbcTemplateMap;
    Map<String, MongoTemplate> mongoTemplateMap;
    // 集成oracle/mysql/h2数据库
    Map<String, DataSourceProperties> dataSourcePropertiesMap = flysqlProperties.getDatasource();
    if (dataSourcePropertiesMap == null) {
        jdbcTemplateMap = new HashMap<>(1);
    } else {
        jdbcTemplateMap = new HashMap<>(dataSourcePropertiesMap.size() + 1);
        dataSourcePropertiesMap.forEach((k, v) -> jdbcTemplateMap.put(k, new JdbcTemplate(v.initializeDataSourceBuilder().build())));
    }
    if (jdbcTemplate != null) {
        jdbcTemplateMap.put(FlysqlConstants.PRIMARY_DATASOURCE_NAME, jdbcTemplate);
    }
    // 集成mongodb数据库
    Map<String, MongoProperties> mongodbPropertiesMap = flysqlProperties.getMongodb();
    if (mongodbPropertiesMap == null) {
        mongoTemplateMap = new HashMap<>(1);
    } else {
        mongoTemplateMap = new HashMap<>(mongodbPropertiesMap.size() + 1);
        mongodbPropertiesMap.forEach((k, v) -> {
            String connectSetting;
            if (v.getUri() == null) {
                connectSetting = "mongo://" + v.getUsername() + ":" + String.valueOf(v.getPassword()) + "@" + v.getHost() + ":" + v.getPort() + "/" + v.getDatabase();
            } else {
                connectSetting = v.getUri();
            }
            mongoTemplateMap.put(k, new MongoTemplate(new SimpleMongoClientDatabaseFactory(connectSetting)));
        });
    }
    if (mongoTemplate != null) {
        mongoTemplateMap.put(FlysqlConstants.PRIMARY_DATASOURCE_NAME, mongoTemplate);
    }
    // 储存关系型数据库和非关系型数据库
    return new Flysql(jdbcTemplateMap, mongoTemplateMap, flysqlProperties);
}
Also used : Flysql(com.isxcode.oxygen.flysql.core.Flysql) SimpleMongoClientDatabaseFactory(org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) MongoTemplate(org.springframework.data.mongodb.core.MongoTemplate) DataSourceProperties(org.springframework.boot.autoconfigure.jdbc.DataSourceProperties) MongoProperties(org.springframework.boot.autoconfigure.mongo.MongoProperties) ConditionalOnClass(org.springframework.boot.autoconfigure.condition.ConditionalOnClass) Bean(org.springframework.context.annotation.Bean)

Aggregations

SimpleMongoClientDatabaseFactory (org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory)16 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)13 Test (org.junit.jupiter.api.Test)4 BeforeAll (org.junit.jupiter.api.BeforeAll)3 MappingMongoConverter (org.springframework.data.mongodb.core.convert.MappingMongoConverter)3 Date (java.util.Date)2 lombok.val (lombok.val)2 MongoProperties (org.springframework.boot.autoconfigure.mongo.MongoProperties)2 MongoDatabaseFactory (org.springframework.data.mongodb.MongoDatabaseFactory)2 Flysql (com.isxcode.oxygen.flysql.core.Flysql)1 MongoException (com.mongodb.MongoException)1 WriteConcern (com.mongodb.WriteConcern)1 MongoClient (com.mongodb.client.MongoClient)1 MongoDatabase (com.mongodb.client.MongoDatabase)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Document (org.bson.Document)1 ObjectId (org.bson.types.ObjectId)1