MongoDB configuration in Spring Boot and Spring Data REST
For starters use the framework Spring Boot will do autoconfiguration for the frameworks it detects. This includes Spring Data JPA and Spring Data Mongo. So you can remove the @Enable
annotation for it.
The same for Spring MVC and Spring Data Rest.
To allow Spring Boot to configure Spring Mongo add the following properties to your application.properties
spring.data.mongodb.host= # the db host
spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
or the
spring.data.mongodb.uri=mongodb://localhost/test # connection URL
More on the Spring Boot Mongo support can be found in this section of the Spring Boot Reference Guide.
When not using an embedded datasource you have to specify which driver to use for this add the following property to your application.properties
. This is also documented in this section of the Spring Boot Reference Guide.
spring.datasource.driverClassName=your.driver.class
I suggest moving your Bootstrap
class to a top level package and remove all not needed annotations and configuration files
@EnableAutoConfiguration
@Configuration
@ComponentScan
public class Bootstrap extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Bootstrap.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Bootstrap.class);
}
}
Should be enough to bootstrap your whole application including jpa, mongo and web support.
For a quite complete list I suggest Appendix A of the Spring Boot Reference Guide.
Sarath
Updated on September 24, 2020Comments
-
Sarath over 3 years
I want to use MongoDB for the mongoDB with spring-boot and JPA.. I'm able to do with embedded H2 database. But I'm not sure what's going wrong using mongo-db. While running the application, I'm getting error that datasource is missing.
@EnableAutoConfiguration @EnableJpaRepositories(basePackages = "com..........repo") @EnableWebMvc @Configuration @ComponentScan @Import({ SpringMongoConfig.class, RepositoryRestMvcConfiguration.class }) public class Bootstrap extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Bootstrap.class, args); } @Override protected SpringApplicationBuilder configure( SpringApplicationBuilder application) { return application.sources(Bootstrap.class); } }
.
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.data.mongodb.config.AbstractMongoConfiguration; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.Mongo; import com.mongodb.MongoClient; @Configuration @EnableMongoRepositories(basePackages = "com.............repo") @PropertySource(value = "classpath:mongo-config.properties") public class SpringMongoConfig extends AbstractMongoConfiguration { @Value("${MONGO_DB_HOST}") private String MONGO_DB_HOST; @Value("${MONGO_DB_PORT}") private int MONGO_DB_PORT; @Value("${DB}") private String DB; @Override protected String getDatabaseName() { return DB; } @Bean @Override public Mongo mongo() throws Exception { return new MongoClient(MONGO_DB_HOST, MONGO_DB_PORT); } }
.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:293) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1186) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) .......................... Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.dataSource; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:509) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290) ... 25 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:597) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) .................................... Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586) ... 39 common frames omitted Caused by: org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.getDriverClassName(DataSourceProperties.java:93) at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource(DataSourceAutoConfiguration.java:105) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-
Sarath over 9 yearsAfter making the changes you specified and adding application.properties to the classpath with the following properties also same exception is thrown.
spring.data.mongodb.uri=mongodb://localhost:27017/test spring.datasource.driverClassName=com.mongodb.Mongo
-
M. Deinum over 9 yearsRead and read again. The datasource needs your JDBC driver NOT MongoDB.
-
Sarath over 9 years
<dependency> <groupId>net.sf.mongojdbcdriver</groupId> <artifactId>mongojdbcdriver</artifactId> <version>0.0.2</version> </dependency>
I added andspring.datasource.driverClassName=net.sf.mongodb_jdbc_driver.MongoDbDriver
Exception is still the same. -
M. Deinum over 9 yearsWhat is it exactly that you want to do? Do you want to use the Mongo client or do you want to use MongoDB as a JDBC client? As I interpreted the question initially is that you want to use the Mongoclient as well as JPA entities. However that doesn't seem to be the case. (Also I doubt that the exception is still the same.)
-
Sarath over 9 yearsOkay, the application goes like this. Its a web application in which I wanted to use MongoDB for all the CRUD operations which are exposed as REST APIs. I was experimenting with the spring-boot, spring-data-rest and JPA. The thing is, its working for the embedded h2 database. But I don't want use the embedded h2 database perform the CRUD operations on mongodb. I followed the instruction you posted. Exception hasn't changed. Don't know what i'm missing.
-
M. Deinum over 9 yearsIf you want to use MongoDB what is JPA doing in there, that is the part I don't understand. So again do you want to use plain mongodb or do you want to access mongodb through JDBC? It is not clear from your post it looks like you want to combine Mongodb and jpa...
-
89n3ur0n about 9 yearsHi guys, any update on this? In my case setting mongo properties in classpath does not work, my application is always connecting to test db , not with the one provided by me
-
M. Deinum about 9 yearsYou are configuring to much remove your own MongoDB configuration, remove
@EnableMOngoRepositories
and@EnableJpaRepositories
. Spring boot will do all that for you. The same goes for@EnableWebMvc
and the import of theRepositoryRestMvcConfiguration
that will all be automatically done. -
yodamad over 8 yearsDo you have this dependency '<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>`
-
Satish Patro about 5 yearsIs there any option to give autoreconnect in spring.data.mongodb.uri ?