简体   繁体   中英

Can not Connect to MLab Mongo Database from mongodb java driver

This is my MLab setup for my blooddb database在此处输入图片说明

I am trying to connect it from a spring application by mongodb java driver. This is my springDataDb Utils file:

public class SpringDataDBUtils {

    private static MongoOperations  mongoOperation;
    private final static Properties properties = new Properties();
    private final static Logger     logger     = LoggerFactory.getLogger(SpringDataDBUtils.class);

    public static MongoOperations getMongoOperations() throws Exception {

        if( mongoOperation==null){

            logger.info("Connecting to db ... ");

            MongoClientURI uri = new MongoClientURI(getDatabaseURI()+getDatabaseName());
            MongoClient client = new MongoClient(uri);

            mongoOperation = new MongoTemplate(client, getDatabaseName());
            logger.info("Connected to db : "+  getDatabaseName());

        }
        return mongoOperation;

        /*AppConfig appConfig = new AppConfig();

        return appConfig.getMongoOperations();*/
     }

     protected static String getDatabaseName() {

            try {
                InputStream inputStream = SpringDataDBUtils.class.getClassLoader()
                        .getResourceAsStream(AppConstant.PROPERTIES_FILE);
                properties.load(inputStream);

            } catch (IOException e) {

                logger.error("Error:"+e.getMessage());
            }

            return properties.getProperty(AppConstant.PROPERTIES_DB_NAME);
      }



     protected static  String getDatabaseURI() {

         try {
             InputStream inputStream = SpringDataDBUtils.class.getClassLoader().getResourceAsStream(AppConstant.PROPERTIES_FILE);
             properties.load(inputStream);

        } catch (IOException e) {
            logger.error("Error:"+e.getMessage());
        }

         String dbURI = "mongodb://"+  properties.getProperty(AppConstant.PROPERTIES_DB_USER) + 
                        ":" + properties.getProperty(AppConstant.PROPERTIES_DB_PASSWORD)   +
                         "@" + properties.getProperty(AppConstant.PROPERTIES_DB_IP)      +
                         ":" + properties.getProperty(AppConstant.PROPERTIES_DB_PORT)      + "/";

        logger.info(dbURI);

         return dbURI;
    }    

     public static Properties ssProperties(){
         try {
             InputStream inputStream = SpringDataDBUtils.class.getClassLoader()
                     .getResourceAsStream(AppConstant.PROPERTIES_FILE);
             properties.load(inputStream);

         } catch (IOException e) {

             logger.error("Error:"+e.getMessage());
         }
          return properties;
      }
}

and my properties file is:

db.name=blooddb
db.password=****
db.user=****
db.ip= mongodb://<dbuser>:<dbpassword>@ds037587.mlab.com:37587/blooddb
db.port=27017

But while running the app i am getting exception.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoTemplate' defined in class path resource [com/istiak/blooddb/AppConfig.class]: Bean instantiation via factory method failed

this is probably the db.ip i have provided here in the properties file. so what can i do while putting ip from mLab?

Seems like there is problem in your dbURI string. You are adding "mongoldb://" two times in dbURI, one the constant string and other as computed from db.ip in properties file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM