有人能帮我吗?我已经在这里呆了两天了。我在spring boot上工作,可以访问两个数据源。应用程序使用HikariCp连接到Postgresql和mongodb,但我得到了Mongodb错误。如果我将mongodb指向另一个数据库(比如Mysql之类的),它可以工作,但不能用于mongodb。
@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;
}
}
@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;
}
}
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
或者,如果有人可以用另一种可能有用的方法来帮助。提前感谢
如果您想将mongodb与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
同时在应用程序的main.class中加入@EnableAutoConfiguration注释
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class, XADataSourceAutoConfiguration.class})
因为Spring引导应用程序的 JPA 自动配置功能尝试使用 JPA 数据源建立数据库连接。Java 类数据源自动配置负责加载 JPA 数据源。可以通过从Spring引导上下文中排除数据源自动配置类来禁用自动配置。
如果我没理解错的话,你是想在JDBC上使用mongoDB。我认为这不是在mongoDB中使用JDBC常见做法。然而,如果你想使用这种方式,有一个统一的JDBC,你可以使用JDBC驱动程序。也看看这个。
问题内容: 异常堆栈跟踪 我的redirect.jsp文件内容 问题答案: 在servlet容器的掩护下,JSP在执行之前先编译为Java类。 例外 表示您的webcontent文件夹根目录中的文件无法编译,这通常意味着该文件在 scriptlet中 包含一些原始Java代码,这些代码包含语法错误。您需要修复这些语法错误,以便servlet容器可以编译那些JSP文件。但是,普遍的共识是, scri
问题内容: 当我尝试运行Hibernate时,我在控制台中收到以下消息。 但是hibernate状态很好。我的项目有什么问题吗?我使用Eclipse(Helios),Hibernate 3.6.8。我使用这些jar文件:antlr-2.7.6,commons- collections-3.1,dom4j-1.6.1,hibernate3,hibernate- jpa-2.0-api-1.0.1.F
我试图从groovy脚本调用Oracle存储过程,结果出现了标题中的错误。 以下是我的剧本: 当我删除(它们是OUT parameters)参数时,脚本将被编译,所以我认为它们有问题······ 我找到了一个让我的程序工作的解决方案: 但我想有一个更好的方法来传递这些SQL参数... PS:我正在使用Groovy-2.4.13,并尝试了Oracle JDK9和1.8.0_151
我正试图用PDFBox和Groovy来搞乱一下。我试图使用PDType1Font类的getStringWidth()方法计算字符串的宽度。 我的问题是,每次编译和运行代码时,都会出现这样一个错误: 代码如下: 为了清晰起见,我删除了代码中所有不相关的部分。 问题出在哪里? 更新:我在IntelliJ中搜索了一下如何将外部jar添加到我的类路径,发现我需要转到项目结构- 我试着这样做: 但我仍然收到
我的系统中安装了JMeter 5.2.1。当我尝试执行下面的代码时,我遇到了这个错误。 响应消息:javax。剧本ScriptException:组织。科德豪斯。非常棒。控制MultipleComplationErrorsException:启动失败:类生成期间出现常规错误:java。lang.NoClassDefFoundError:无法加载类com。蔚蓝色的消息传递。事件中心。由于缺少依赖关系
这是密码。没有警告,文件正常。
问题内容: 我正在使用 eclipse juno 使用 maven (m2eclipse插件)运行其他人的Java代码。但是我收到以下消息: 在官方网站上,我找到了针对此问题的以下修复程序: 所以我将slf4j-simple.jar复制到 “ C:\ Program Files \ Java \ jdk1.7.0_07 \ lib”中 。但是我仍然遇到错误。 请指导我什么是正确的解决方案。我是Ja
由于我升级到android studio 3.0.1,我无法继续使用我的应用程序。Gradle引发以下错误:Gradle同步问题 错误:无法加载类“groovy.lang.GroovyObject”。此意外错误的可能原因包括: 有什么建议吗?非常感谢你。