MongoDB configuration class

MongoDB configuration class:

  1. @Profile ( "dev"): point application-dev.properties configuration file, dev environment refers to the configuration identification, generally refers to the test environment.
  2. @Configuration: @Configuration notes show that this class is a class configuration. Components can initiate a scan for the entity with @Bean instantiate bean and the like.
  3. @Configuration understood as a spring when the inside xml tag, which is: disposed spring containers (application context), @Bean understood as a spring when the inside xml tag.
  4. @Bean: Spring annotation is used to tell the @Bean method, resulting in a Bean object, and the object is to Spring Bean management. This method produces Spring Bean object only called once, then this will be the Spring Bean object in its own IOC container.
  5. SpringIOC container manages one or more of the bean, the bean need to be created in the @Configuration comment, use @Bean annotation on a method to show that this approach needs to be managed Spring.

MongoDB class configuration as follows:

package com.terse.develop.activity.config;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Profile;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.util.StringValueResolver;

@Configuration
public class ActivityMongoConfig implements EmbeddedValueResolverAware {

    private StringValueResolver stringValueResolver;

    public @Bean
    MongoClient activityMongo() {
        MongoClientURI mongoClientURI = new MongoClientURI(stringValueResolver.resolveStringValue("${mongodb.uri}"));
        return new MongoClient(mongoClientURI);
    }
    
    @Profile("dev")
    public @Bean("activityMongoFactory")
    MongoDbFactory mongoDbFactoryDev(MongoClient mongoClient) {
        return new SimpleMongoDbFactory(MongoClient, stringValueResolver.resolveStringValue("${mongodb.dev.database}"));
    }

    @Profile("pro")
    public @Bean("activityMongoFactory")
    MongoDbFactory mongoDbFactoryPro(MongoClient mongoClient) {
        return new SimpleMongoDbFactory(mongoClient, stringValueResolver.resolveStringValue("${mongodb.pro.database}"));
    }

     public @Bean
    MongoTemplate mongoTemplate() throws Exception {
       MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactoryPro());
       return mongoTemplate;
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        this.stringValueResolver = resolver;
    }
}

Published 51 original articles · won praise 11 · views 6077

Guess you like

Origin blog.csdn.net/weixin_42140261/article/details/104863700