java 创建在类路径资源中定义了名称为“dataSourceScriptDatabaseInitializer”的Bean时出错

okxuctiv  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(926)

大家好,我在本地运行heroku服务器时遇到了问题,我相信不止一个错误,但我不知道主要问题是什么。我已经添加了procfile和数据库配置,如代码中所提供的。

6:43:37 PM web.1 |  ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
6:43:37 PM web.1 |   \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
6:43:37 PM web.1 |    '  |____| .__|_| |_|_| |_\__, | / / / /
6:43:37 PM web.1 |   :: Spring Boot ::                (v2.6.1)
6:43:37 PM web.1 |  2021-12-14 18:43:37.764  INFO 269 --- [           main] c.discount.discount.DiscountApplication  : Starting DiscountApplication using Java 11.0.11 on Sabbagh99 with PID 269 (/mnt/c/Users/ad199/IdeaProjects/Discounts-Web-Api-old/build/libs/discount-0.0.1-SNAPSHOT.jar started by sabbagh99 in /mnt/c/Users/ad199/IdeaProjects/Discounts-Web-Api-old)
6:43:37 PM web.1 |  2021-12-14 18:43:37.777  INFO 269 --- [           main] c.discount.discount.DiscountApplication  : No active profile set, falling back to default profiles: default
6:43:39 PM web.1 |  2021-12-14 18:43:39.864  INFO 269 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
6:43:40 PM web.1 |  2021-12-14 18:43:40.022  INFO 269 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 123 ms. Found 1 JPA repository interfaces.
6:43:41 PM web.1 |  2021-12-14 18:43:41.697  INFO 269 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
6:43:41 PM web.1 |  2021-12-14 18:43:41.727  INFO 269 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
6:43:41 PM web.1 |  2021-12-14 18:43:41.727  INFO 269 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.55]
6:43:41 PM web.1 |  2021-12-14 18:43:41.981  INFO 269 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
6:43:41 PM web.1 |  2021-12-14 18:43:41.981  INFO 269 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3820 ms
6:43:42 PM web.1 |  2021-12-14 18:43:42.202  WARN 269 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSource
6:43:42 PM web.1 |  >  ScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"
6:43:42 PM web.1 |  2021-12-14 18:43:42.210  INFO 269 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
6:43:42 PM web.1 |  2021-12-14 18:43:42.262  INFO 269 --- [           main] ConditionEvaluationReportLoggingListener :
6:43:42 PM web.1 |  Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
6:43:42 PM web.1 |  2021-12-14 18:43:42.346 ERROR 269 --- [           main] o.s.boot.SpringApplication               : Application run failed
6:43:42 PM web.1 |  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConfig': Injection of aut
6:43:42 PM web.1 |  >  owired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"
6:43:42 PM web.1 |      at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) ~[spring-boot-2.6.1.jar!/:2.6.1]
6:43:42 PM web.1 |      at com.discount.discount.DiscountApplication.main(DiscountApplication.java:10) ~[classes!/:na]
6:43:42 PM web.1 |      at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[discount-0.0.1-SNAPSHOT.jar:na]
6:43:42 PM web.1 |      at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[discount-0.0.1-SNAPSHOT.jar:na]
6:43:42 PM web.1 |      at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[discount-0.0.1-SNAPSHOT.jar:na]
6:43:42 PM web.1 |      at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[discount-0.0.1-SNAPSHOT.jar:na]
6:43:42 PM web.1 |  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'databaseConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"
6:43:42 PM web.1 |      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:405) ~[spring-beans-5.3.13.jar!/:5.3.13]
6:43:42 PM web.1 |      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431) ~[spring-beans-5.3.13.jar!/:5.3.13]
6:43:42 PM web.1 |  Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"
6:43:42 PM web.1 |      at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-5.3.13.jar!/:5.3.13]

这是我的档案

等级:

plugins {
   id 'org.springframework.boot' version '2.6.1'
   id 'io.spring.dependency-management' version '1.0.11.RELEASE'
   id 'java'
}

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

repositories {
   mavenCentral()
}

dependencies {
   implementation 'org.springframework.boot:spring-boot-starter-web'
   testImplementation 'org.springframework.boot:spring-boot-starter-test'
   implementation group: 'javax.persistence', name: 'javax.persistence-api', version: '2.2'
   implementation group: 'org.springframework.boot', name: 'spring-boot-starter-jdbc', version: '2.6.0'
   implementation group: 'org.postgresql', name: 'postgresql', version: '42.3.1'

}

test {
   useJUnitPlatform()
}

数据库配置类:

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

    @Bean
    public DataSource dataSource() {
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl(dbUrl);
        return new HikariDataSource(config);
    }
}
dw1jzc5e

dw1jzc5e1#

例外情况是通知您无法创建bean,因为您尚未定义属性spring.datasource.url
如果你使用的是yml配置,(例如在src/main/resources中你有application.yml文件),打开它并在“spring”下添加datasource属性:

spring:
  datasource:
    url: # db connection url here

如果使用的是属性文件,则格式更改为

spring.datasource.url= # db connection url here
kwvwclae

kwvwclae2#

我遇到了同样的问题。尽管我的配置是正确的:

spring.datasource.url=jdbc:postgresql://localhost:5432/<your_data_base_name>

我错过了maven依赖:

<dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

相关问题