无法加载driverClassName类mongodb.jdbc.MongoDriver

时间:2018-03-13 16:45:43

标签: spring-boot spring-data spring-data-jpa hikaricp

有人可以帮帮我吗。我现在在这里待了2天。我正在进行Spring启动,可以访问两(2)个数据源。使用HikariCp连接到Postgresql和Mongodb的应用程序,但我收到了mongodb错误。如果我将mongodb指向另一个数据库(如Mysql等),它将起作用,但不适用于mongodb。

DataSourceOne

@Configuration
@EnableJpaRepositories(
        basePackages = "com.deanace.oauth2.repository.datasourceOne",
        entityManagerFactoryRef = "entityManagerFactoryBeanOne",
        transactionManagerRef = "transactionManagerOne"
)
public class DataSourceOne {

    @Value("${spring.datasource.one.driverClassName}")
    private String driver;

    @Value("${spring.datasource.one.url}")
    private String url;

    @Value("${spring.datasource.one.username}")
    private String username;

    @Value("${spring.datasource.one.password}")
    private String password;

    @Bean
    public DataSource DataSourceOne(){
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driver);
        config.setJdbcUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        config.setAutoCommit(true);
        return new HikariDataSource(config);
    }

    @Bean
    public PlatformTransactionManager transactionManagerOne(){
        return new JpaTransactionManager(entityManagerFactoryBeanOne().getObject());
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanOne() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactoryBean.setDataSource(DataSourceOne());
        entityManagerFactoryBean.setPackagesToScan("com.deanace.oauth2.entities");
        return entityManagerFactoryBean;
    }
}

DataSourceTwo

@Configuration
@EnableJpaRepositories(
        basePackages = "com.deanace.oauth2.repository.datasourceTwo",
        entityManagerFactoryRef = "entityManagerFactoryBeanTwo",
        transactionManagerRef = "transactionManagerTwo"
)
public class DataSourceTwo {

    @Value("${spring.datasource.mongodb.driverClassName}")
    private String driver;

    @Value("${spring.datasource.mongodb.url}")
    private String url;

    @Value("${spring.data.mongodb.username}")
    private String username;

    @Value("${spring.data.mongodb.password}")
    private String password;

    @Bean
    public DataSource DataSourceTwo(){
        HikariConfig config = new HikariConfig();
        config.setDriverClassName(driver);
        config.setJdbcUrl(url);
        config.setUsername(username);
        config.setPassword(password);
        config.setAutoCommit(true);
        return new HikariDataSource(config);
    }

    @Bean
    public PlatformTransactionManager transactionManagerTwo(){
        return new JpaTransactionManager(entityManagerFactoryBeanTwo().getObject());
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBeanTwo() {
        HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();

        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactoryBean.setDataSource(DataSourceTwo());
        entityManagerFactoryBean.setPackagesToScan("com.deanace.oauth2.entities");
        return entityManagerFactoryBean;
    }
}

application.properties

    spring.application.name=spring-security-oauth2
    spring.datasource.type=com.zaxxer.hikari.HikariDataSource
    # suppress inspection "SpringBootApplicationProperties" for whole file
    server.port=8080
    # ===============================
    # DATASOURCE
    # ===============================
    # DataSource (Postgresql offline).
    spring.datasource.one.driverClassName=org.postgresql.Driver
    spring.datasource.one.url=jdbc:postgresql://localhost:5432/application1
    spring.datasource.one.username=postgres
    spring.datasource.one.password=password

    #mongodb mongodb.jdbc.MongoDriver
    spring.datasource.one.driverClassName=org.MongoDriver.Driver
    spring.datasource.one.url=jdbc:mongo://localhost:5432/application2
    spring.data.mongodb.username=mongodb
    spring.data.mongodb.password=password
    spring.data.mongodb.host=localhost
    spring.data.mongodb.port=27017
    spring.data.mongodb.database=application2

错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactoryBeanTwo' defined in class path resource [com/deanace/oauth2/persistence/DataSourceTwo.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactoryBeanTwo' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DataSourceTwo' defined in class path resource [com/deanace/oauth2/persistence/DataSourceTwo.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'DataSourceTwo' threw exception; nested exception is java.lang.RuntimeException: Failed to load class of driverClassName mongodb.jdbc.MongoDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.10.RELEASE.jar:1.5.10.RELEASE]
    at com.deanace.oauth2.SpringSecurityOauth2Application.main(SpringSecurityOauth2Application.java:64) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactoryBeanTwo' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DataSourceTwo' defined in class path resource [com/deanace/oauth2/persistence/DataSourceTwo.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'DataSourceTwo' threw exception; nested exception is java.lang.RuntimeException: Failed to load class of driverClassName mongodb.jdbc.MongoDriver
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 18 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'DataSourceTwo' defined in class path resource [com/deanace/oauth2/persistence/DataSourceTwo.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'DataSourceTwo' threw exception; nested exception is java.lang.RuntimeException: Failed to load class of driverClassName mongodb.jdbc.MongoDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.obtainBeanInstanceFromFactory(ConfigurationClassEnhancer.java:389) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56.DataSourceTwo(<generated>) ~[classes/:na]
    at com.deanace.oauth2.persistence.DataSourceTwo.entityManagerFactoryBeanTwo(DataSourceTwo.java:62) ~[classes/:na]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56.CGLIB$entityManagerFactoryBeanTwo$1(<generated>) ~[classes/:na]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56$$FastClassBySpringCGLIB$$12340b71.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56.entityManagerFactoryBeanTwo(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 19 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'DataSourceTwo' threw exception; nested exception is java.lang.RuntimeException: Failed to load class of driverClassName mongodb.jdbc.MongoDriver
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 41 common frames omitted
Caused by: java.lang.RuntimeException: Failed to load class of driverClassName mongodb.jdbc.MongoDriver
    at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:323) ~[HikariCP-2.5.1.jar:na]
    at com.deanace.oauth2.persistence.DataSourceTwo.DataSourceTwo(DataSourceTwo.java:43) ~[classes/:na]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56.CGLIB$DataSourceTwo$0(<generated>) ~[classes/:na]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56$$FastClassBySpringCGLIB$$12340b71.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    at com.deanace.oauth2.persistence.DataSourceTwo$$EnhancerBySpringCGLIB$$cf32bb56.DataSourceTwo(<generated>) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.14.RELEASE.jar:4.3.14.RELEASE]
    ... 42 common frames omitted
Caused by: java.lang.ClassNotFoundException: mongodb.jdbc.MongoDriver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_152]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_152]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) ~[na:1.8.0_152]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_152]
    at com.zaxxer.hikari.HikariConfig.setDriverClassName(HikariConfig.java:318) ~[HikariCP-2.5.1.jar:na]
    ... 53 common frames omitted

或者,如果有人可以帮助另一种可以提供帮助的方法。提前谢谢

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您正在尝试将mongoDB与JDBC一起使用。我认为这不是使用JDBC和mongoDB的常见做法。但是,如果要使用这种方式,可以使用unityJDBC作为JDBC驱动程序。另请查看this

答案 1 :(得分:0)

如果要将monogoDb与JDBC连接,可以在 application.properties 文件中添加这两行,以将mongodb与JDBC连接并排除自动配置。

#connect mongodb with jdbc
spring.data.mongodb.uri=mongodb://localhost:27017/testDb\ 

#exclude jdbc autoconfiguration
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

还将@EnableAutoConfiguration批注放入应用程序的 main.class

@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})

因为spring boot应用程序的JPA自动配置功能尝试使用JPA数据源建立数据库连接。 Java类DataSourceAutoConfiguration负责加载JPA DataSource。可以通过从Spring Boot上下文中排除DataSourceAutoConfiguration类来禁用自动配置。