1.去掉SpringDataMongodb中的_class 2.通过环境获取变量

package com.xinyue.game.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.convert.MongoTypeMapper;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

@Configuration
public class MongodbConfig extends AbstractMongoClientConfiguration {

    @Autowired
    Environment environment;

    public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory databaseFactory,
                                                       MongoCustomConversions customConversions,
                                                       MongoMappingContext mappingContext) {
        MappingMongoConverter mmc = super.mappingMongoConverter(databaseFactory, customConversions, mappingContext);
        mmc.setTypeMapper(defaultMongoTypeMapper());
        return mmc;
    }

    @Bean
    public MongoTypeMapper defaultMongoTypeMapper() {
        return new DefaultMongoTypeMapper(null);
    }

    @Override
    protected String getDatabaseName() {
        return environment.getProperty("spring.data.mongodb.database");
    }
}

其中数据库的名字是database字段,我们可以通过环境获取

spring:
  data:
    mongodb:
      host: 127.0.0.1
      port: 27017
#      username: dev001
#      password: "123456"  #  密码必须添加引号
#      authentication-database: admin
      database: my-center-game
  redis:
    host: localhost
    port: 6379
#    password: 123456


xinyue:
  center:
    config:
#      token-aes-key: 123456AAbbcc#@x2

你可能感兴趣的:(#,#,springboot,java)