用java和intellij连接到Postgresql的问题

时间:2019-05-26 17:45:19

标签: java postgresql gradle heroku

我的项目有问题。我正在尝试使用postgresql启动我的应用程序,但出现错误。我无法调用build gradle,始终完成BUILD FAILED。我给你看代码。 我不明白为什么会出现此错误,因为我花了一个星期就没有问题,并且gradle构建也成功了。我只是将应用程序属性文件改回了MySQL。当我再次想要使用postgresql时,这些错误会弹出。我正在寻求帮助,感谢您的所有答复

我的应用程序属性

spring.jpa.hibernate.ddl-auto=update
spring.jpa.database=postgresql
spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.username=${JDBC_DATABASE_USERNAME}
spring.datasource.password=${JDBC_DATABASE_PASSWORD}

我的build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.3.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

//应用插件:“战争”

group = 'com.crud'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    compileOnly 'org.projectlombok:lombok:1.18.8'
    annotationProcessor 'org.projectlombok:lombok:1.18.8'
    implementation 'org.springframework.boot:spring-boot-starter-data- 
   jpa'
    //runtimeOnly 'mysql:mysql-connector-java'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'io.springfox:springfox-swagger2:2.7.0'
    implementation 'io.springfox:springfox-swagger-ui:2.7.0'
    testImplementation 'org.springframework.boot:spring-boot-starter- 
   test'
    runtimeOnly 'org.postgresql:postgresql'
    implementation 'org.springframework.boot:spring-boot-starter- 
   mail:2.1.5.RELEASE'
    //providedRuntime 'org.springframework.boot:spring-boot-starter- 
    tomcat'
}

当我运行应用程序时...

2019-05-26 19:19:36.139  INFO 14288 --- [           main] 
o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-05-26 19:19:36.152  INFO 14288 --- [           main] 
ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re- 
run your application with 'debug' enabled.
2019-05-26 19:19:36.162 ERROR 14288 --- [           main] 
o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error 
creating bean with name 
'org.springframework.boot.autoconfigure.
orm.jpa.HibernateJpaConfiguration': 
Unsatisfied dependency expressed through constructor parameter 0; nested 
exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'dataSource' defined in class path resource 



 [org/springframework/boot/autoconfigure
/jdbc/DataSourceConfiguration$Hikari.c 
lass]: Bean instantiation via factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 
'dataSource' threw exception; nested exception is 
 java.lang.IllegalArgumentException: URL must start with 'jdbc'
at 



org.springframework.beans.factory.
support.ConstructorResolver.createArgumentA 
rray(ConstructorResolver.java:769) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 



org.springframework.beans.factory.
support.ConstructorResolver.autowireConstru 
ctor(ConstructorResolver.java:218) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 




 org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory. 
autowireConstructor(AbstractAutowireCapableBeanFactory.java:1325) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 





org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory. 
createBeanInstance(AbstractAutowireCapableBeanFactory.java:1171) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 




 org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory. 
doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 




 org.springframework.beans.factory.
 support.AbstractAutowireCapableBeanFactory. 
 createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans- 
 5.1.5.RELEASE.jar:5.1.5.RELEASE]
 at 




org.springframework.beans.factory.
support.AbstractBeanFactory.lambda$doGetBea 
n$0(AbstractBeanFactory.java:320) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 


org.springframework.beans.factory.
support.DefaultSingletonBeanRegistry.getSin 
gleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 



org.springframework.beans.factory.
support.AbstractBeanFactory.doGetBean(Abstr 
actBeanFactory.java:318) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 


org.springframework.beans.factory.
support.AbstractBeanFactory.getBean(Abstrac 
tBeanFactory.java:199) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.ConstructorResolver.instantiateUsin 
gFactoryMethod(ConstructorResolver.java:392) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

 org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory. 

instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.
java:1305) 
 ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.
createBeanInstance(AbstractAutowireCapableBeanFactory.java:1144) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.support.
AbstractAutowireCapableBeanFactory.
doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory.
createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.AbstractBeanFactory.
lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 


org.springframework.beans.factory.
support.DefaultSingletonBeanRegistry.
getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractBeanFactory.
doGetBean(AbstractBeanFactory.java:318) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) 
~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.context.support
.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105) 
~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.context.support
.AbstractApplicationContext.
finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~ 
[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
 org.springframework.context.support
.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) 
~[spring-context-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at

org.springframework.boot.web.servlet.
context.ServletWebServerApplicationContext.
refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 
org.springframework.boot.SpringApplication
.refresh(SpringApplication.java:775) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 
 org.springframework.boot.SpringApplication
.refreshContext(SpringApplication.java:397) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 
org.springframework.boot.SpringApplication
.run(SpringApplication.java:316) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 

org.springframework.boot.SpringApplication
.run(SpringApplication.java:1260) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 
 org.springframework.boot.SpringApplication
.run(SpringApplication.java:1248) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 
com.crud.tasks.TasksApplication.
main(TasksApplication.java:11) ~[classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error 
creating bean with name 'dataSource' defined in class path resource 

[org/springframework/boot/autoconfigure
/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via 
factory method failed; nested exception is 
org.springframework.beans.BeanInstantiationException: Failed to 
instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 
'dataSource' threw exception; nested exception is 
java.lang.IllegalArgumentException: URL must start with 'jdbc'
at 
org.springframework.beans.factory.
support.ConstructorResolver.instantiate(ConstructorResolver.java:627) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.ConstructorResolver.
instantiateUsingFactoryMethod(ConstructorResolver.java:607) ~[spring- 
beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory.
instantiateUsingFactoryMethod
(AbstractAutowireCapableBeanFactory.java:1305) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory.
createBeanInstance(AbstractAutowireCapableBeanFactory.java:1144) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory.
doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractAutowireCapableBeanFactory.
createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 
org.springframework.beans.factory.
support.AbstractBeanFactory.
lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.DefaultSingletonBeanRegistry.
getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.AbstractBeanFactory.
doGetBean(AbstractBeanFactory.java:318) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.AbstractBeanFactory.
getBean(AbstractBeanFactory.java:199) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.config
.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:277) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.DefaultListableBeanFactory.
doResolveDependency(DefaultListableBeanFactory.java:1247) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

 org.springframework.beans.factory.
 support.DefaultListableBeanFactory.
 resolveDependency(DefaultListableBeanFactory.java:1167) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.support.
ConstructorResolver.
resolveAutowiredArgument(ConstructorResolver.java:857) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.ConstructorResolver.
createArgumentArray(ConstructorResolver.java:760) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 28 common frames omitted
Caused by: org.springframework.beans.BeanInstantiationException: Failed 
to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 
'dataSource' threw exception; nested exception is 
java.lang.IllegalArgumentException: URL must start with 'jdbc'
at 

org.springframework.beans.factory.
support.SimpleInstantiationStrategy.
instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.beans.factory.
support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~ 
[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 42 common frames omitted
Caused by: java.lang.IllegalArgumentException: URL must start with 'jdbc'
at org.springframework.util.Assert.isTrue(Assert.java:118) ~[spring-core- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
at 

org.springframework.boot.jdbc.DatabaseDriver.
fromJdbcUrl(DatabaseDriver.java:280) ~[spring-boot- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 

org.springframework.boot.autoconfigure.
jdbc.DataSourceProperties.
determineDriverClassName(DataSourceProperties.java:230) ~[spring-boot- 
autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 
org.springframework.boot.autoconfigure
.jdbc.DataSourceProperties.
initializeDataSourceBuilder(DataSourceProperties.java:176) ~[spring-boot- 
autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 

org.springframework.boot.autoconfigure.
jdbc.DataSourceConfiguration.
createDataSource(DataSourceConfiguration.java:43) ~[spring-boot- 
autoconfigure-2.1.3.RELEASE.jar:2.1.3.RELEASE]
at 


org.springframework.boot.autoconfigure.
jdbc.DataSourceConfiguration$Hikari.
dataSource(DataSourceConfiguration.java:83) ~[spring-boot-autoconfigure- 
2.1.3.RELEASE.jar:2.1.3.RELEASE]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 
Method) ~[na:na]
at 
java.base/jdk.internal.reflect.
NativeMethodAccessorImpl.
invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at 
java.base/jdk.internal.reflect.
DelegatingMethodAccessorImpl.
invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at 
org.springframework.beans.
factory.support.SimpleInstantiationStrategy.
instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans- 
5.1.5.RELEASE.jar:5.1.5.RELEASE]
... 43 common frames omitted

当我运行gradle build ...

Task :test

com.crud.tasks.TasksApplicationTests > contextLoads FAILED
java.lang.IllegalStateException
    Caused by: 
org.springframework.beans.factory.UnsatisfiedDependencyException
        Caused by: 
org.springframework.beans.factory.BeanCreationException
            Caused by: 
org.springframework.beans.BeanInstantiationException
                Caused by: java.lang.IllegalArgumentException

7 tests completed, 1 failed

2 个答案:

答案 0 :(得分:0)

我认为日志说明了问题所在。

  

'dataSource'抛出异常;嵌套异常为   java.lang.IllegalArgumentException:URL必须以'jdbc'开头   

目前尚不清楚此变量${JDBC_DATABASE_URL}的值

但是应该是这样的:

jdbc:postgresql://localhost:5432/dbname?currentSchema=dbschema

答案 1 :(得分:0)

我在本地不需要它,问题是当我将诸如application.properties之类的内容发送到heroku存储库时,我的应用程序无法正常工作。今天,我使用了朋友的下层提示并单独设置了JDBC_DATABASE_URL,然后终端向我显示了此错误

Field repository in com.crud.tasks.com.crud.tasks.service.DbService required a bean 
of type 'com.crud.tasks.com.crud.tasks.repository.TaskRepository' that could not be 
found.

The injection point has the following annotations:
- @ org.springframework.beans.factory.annotation.Autowired (required = true)


Action:

Consider defining a bean of type 
'com.crud.tasks.com.crud.tasks.repository.TaskRepository' in your configuration.

但是我没有更改代码中的任何内容,最后的一切都很好 成功的部署计划