【问题标题】:MongoDB configuration in Spring Boot and Spring Data RESTSpring Boot 和 Spring Data REST 中的 MongoDB 配置
【发布时间】:2014-11-17 10:35:22
【问题描述】:

我想将 MongoDB 用于带有 spring-boot 和 JPA 的 mongoDB。我可以使用嵌入式 H2 数据库。但我不确定使用 mongo-db 出了什么问题。运行应用程序时,我收到数据源丢失的错误消息。

@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)

【问题讨论】:

    标签: spring mongodb spring-data spring-boot spring-data-rest


    【解决方案1】:

    对于初学者来说,使用 Spring Boot 框架会自动配置它检测到的框架。这包括 Spring Data JPA 和 Spring Data Mongo。因此,您可以删除它的 @Enable 注释。

    Spring MVC 和 Spring Data Rest 也是如此。

    要允许 Spring Boot 配置 Spring Mongo,请将以下属性添加到您的 application.properties

    spring.data.mongodb.host= # the db host
    spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
    

    spring.data.mongodb.uri=mongodb://localhost/test # connection URL
    

    有关 Spring Boot Mongo 支持的更多信息,请参阅 Spring Boot 参考指南的 this section

    当不使用嵌入式数据源时,您必须指定要使用的驱动程序,将以下属性添加到您的application.properties。这也记录在 Spring Boot 参考指南的this section 中。

    spring.datasource.driverClassName=your.driver.class

    我建议将您的 Bootstrap 类移动到顶级包并删除所有不需要的注释和配置文件

    @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);
       }
    }
    

    应该足以引导您的整个应用程序,包括 jpa、mongo 和 web 支持。

    对于一个非常完整的列表,我建议 Spring Boot 参考指南的Appendix A

    【讨论】:

    • 进行您指定的更改并将 application.properties 添加到具有以下属性的类路径后,也会引发相同的异常。 spring.data.mongodb.uri=mongodb://localhost:27017/test spring.datasource.driverClassName=com.mongodb.Mongo
    • 反复阅读。数据源需要您的 JDBC 驱动程序而不是 MongoDB。
    • <dependency> <groupId>net.sf.mongojdbcdriver</groupId> <artifactId>mongojdbcdriver</artifactId> <version>0.0.2</version> </dependency>我加了spring.datasource.driverClassName=net.sf.mongodb_jdbc_driver.MongoDbDriver异常还是一样。
    • 您到底想做什么?您想使用 Mongo 客户端还是使用 MongoDB 作为 JDBC 客户端?正如我最初解释的那样,问题是您想使用 Mongoclient 以及 JPA 实体。然而,情况似乎并非如此。 (我也怀疑异常是否仍然相同。)
    • 好的,应用程序是这样的。它是一个 Web 应用程序,我想在其中将 MongoDB 用于所有作为 REST API 公开的 CRUD 操作。我正在试验 spring-boot、spring-data-rest 和 JPA。问题是,它适用于嵌入式 h2 数据库。但我不想使用嵌入式 h2 数据库在 mongodb 上执行 CRUD 操作。我按照您发布的说明进行操作。异常没有改变。不知道我错过了什么。
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 2014-12-27
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2015-04-29
    相关资源
    最近更新 更多