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

com.wueasy.data.mongodb.init.MongodbApplicationContextInitializer Maven / Gradle / Ivy

There is a newer version: 3.2.2
Show newest version
package com.wueasy.data.mongodb.init;
/*
 * wueasy - A Java Distributed Rapid Development Platform.
 * Copyright (C) 2017-2019 wueasy.com

 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */

import java.util.Map;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.annotation.Order;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory;
import org.springframework.stereotype.Component;

import com.wueasy.base.util.EnvironmentHelper;
import com.wueasy.data.configure.mongodb.MongodbConfigure;

/**
 * Mongodb客户端初始化
 * @author: fallsea
 * @version 1.0
 */
@Component
@Order(4)
public class MongodbApplicationContextInitializer implements ApplicationContextInitializer {

	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		Map clientConfigMap = EnvironmentHelper.getMap("wueasy.data.mongodb", MongodbConfigure.class,
				applicationContext.getEnvironment());
		if (null != clientConfigMap && !clientConfigMap.isEmpty()) {
			BeanDefinitionRegistry beanFactory = (BeanDefinitionRegistry) applicationContext.getBeanFactory();
			boolean primary = true;
			for (Map.Entry entry : clientConfigMap.entrySet()) {
				MongodbConfigure mongodbConfigure = entry.getValue();
		        MongoDbFactory mongoDbFactory = new SimpleMongoClientDbFactory(mongodbConfigure.getUri());
		        
		        BeanDefinitionBuilder beanDefinitionBuilderMongoDbFactory = BeanDefinitionBuilder.genericBeanDefinition(MongoDbFactory.class,() -> {
	                return mongoDbFactory;
				});
		        if(primary) {
		        	beanDefinitionBuilderMongoDbFactory.setPrimary(true);
		        }
		        beanFactory.registerBeanDefinition(entry.getKey() + "MongoDbFactory", beanDefinitionBuilderMongoDbFactory.getRawBeanDefinition());
				
				BeanDefinitionBuilder beanDefinitionBuilderMongoTemplate = BeanDefinitionBuilder.genericBeanDefinition(MongoTemplate.class,() -> {
	                return new MongoTemplate(mongoDbFactory);
				});
				if(primary) {
					beanDefinitionBuilderMongoTemplate.setPrimary(true);
				}
				// 动态注册bean.
			    beanFactory.registerBeanDefinition(entry.getKey() + "MongoTemplate", beanDefinitionBuilderMongoTemplate.getRawBeanDefinition());
			    primary = false;
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy