2014-09-23 137 views
2

我想用mongoDB和spring-boot和JPA一起使用MongoDB ..我可以使用嵌入式H2數據庫。但我不確定使用mongo-db會出現什麼問題。在運行應用程序時,我得到數據源丟失的錯誤。Spring Boot和Spring Data中的MongoDB配置REST

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

回答

6

對於初學者,使用框架Spring Boot將爲它檢測到的框架進行自動配置。這包括Spring Data JPA和Spring Data Mongo。因此,您可以爲其刪除@Enable註釋。

對於Spring MVC和Spring Data Rest同樣如此。

爲了讓春節引導配置Spring蒙戈添加以下屬性到您的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引導蒙戈支持可以在this section找到Spring Boot參考指南。

當不使用嵌入式數據源時,您必須指定爲此使用的驅動程序將以下屬性添加到您的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 Reference Guide的Appendix A

+0

在進行指定的更改並將** application.properties **添加到具有以下屬性的類路徑中後,也會拋出相同的異常。 'spring.data.mongodb.uri = mongodb:// localhost:27017/test spring.datasource.driverClassName = com.mongodb.Mongo' – Sarath 2014-09-23 10:28:15

+0

閱讀並重新閱讀。數據源需要您的JDBC驅動程序而非MongoDB。 – 2014-09-23 10:39:37

+0

' \t net.sf.mongojdbcdriver \t mongojdbcdriver \t 0.0.2 ' 我添加和'spring.datasource.driverClassName = net.sf.mongodb_jdbc_driver.MongoDbDriver' 異常仍然一樣。 – Sarath 2014-09-23 11:25:03

相關問題