Search code examples
javamongodbspring-boot

Spring Boot app not connecting to the database specified in application.property for Mongodb


I have developer Application in Spring boot and exported the war file and placed it in tomcat 9 server. When I try to test the API in Rest client the app is connecting to test DB instead of the DB which I declared in application.property for mongodb, below is my application.property

Database name.
spring.data.mongodb.database=IndianFarms

Mongo server host.
spring.data.mongodb.host=localhost

Mongo server port.
spring.data.mongodb.port=27017

logging
logging.level.org.springframework.data=debug
logging.level.=error

spring.jackson.default-property-inclusion=NON_NULL

Can any one help me in this.


Solution

  • After lot of search and research in came to know that spring boot auto configure lot of things internally. Declaration in application.properties file is not working. We have to programmatically do this. Below is how I did it. Now Spring boot is connection to correct database which is in my case IndianFarms. @Smile thanks your suggestion helped me to find out exact cause.

    @Configuration
    public class MongoConfig {
    
    
        @Bean
        public MongoDbFactory mongoDbFactory() {
    
            MongoClient mongoClient = new MongoClient("127.0.0.1:27017");
    
            return new SimpleMongoDbFactory(mongoClient, "IndianFarms" );
        }
    
        @Bean
        public MongoTemplate mongoTemplate() {
            return new MongoTemplate(mongoDbFactory());
        }
    }