Spring Boot and how to configure connection details to MongoDB?

后端 未结 6 2030
死守一世寂寞
死守一世寂寞 2020-12-12 18:45

Being new to Spring Boot I am wondering on how I can configure connection details for MongoDB. I have tried the normal examples but none covers the connection details.

6条回答
  •  囚心锁ツ
    2020-12-12 19:29

    You can define more details by extending AbstractMongoConfiguration.

    @Configuration
    @EnableMongoRepositories("demo.mongo.model")
    public class SpringMongoConfig extends AbstractMongoConfiguration {
        @Value("${spring.profiles.active}")
        private String profileActive;
    
        @Value("${spring.application.name}")
        private String proAppName;
    
        @Value("${spring.data.mongodb.host}")
        private String mongoHost;
    
        @Value("${spring.data.mongodb.port}")
        private String mongoPort;
    
        @Value("${spring.data.mongodb.database}")
        private String mongoDB;
    
        @Override
        public MongoMappingContext mongoMappingContext()
            throws ClassNotFoundException {
            // TODO Auto-generated method stub
            return super.mongoMappingContext();
        }
        @Override
        @Bean
        public Mongo mongo() throws Exception {
            return new MongoClient(mongoHost + ":" + mongoPort);
        }
        @Override
        protected String getDatabaseName() {
            // TODO Auto-generated method stub
            return mongoDB;
        }
    }
    

提交回复
热议问题