欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【DEBUG】Error starting ApplicationContext. To display the conditions report re-run your application w

程序员文章站 2022-01-30 20:47:04
...

BUG INFO

本项目源自《尚硅谷SpringBoot核心技术篇 —— springboot-data-jdbc-demo》

根据application.yml自动加载配置department.sql和employee.sql时的报错信息

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-03-09 11:30:11.671 ERROR 30260 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target [[email protected] type = java.util.List<java.lang.String>, value = 'provided', annotations = array<Annotation>[[empty]]] failed:

    Property: spring.datasource.schema[0].classpath
    Value: sql/department.sql
    Origin: class path resource [application.yml]:8:20
    Reason: The elements [spring.datasource.schema[0].classpath,spring.datasource.schema[1].classpath] were left unbound.
    Property: spring.datasource.schema[1].classpath
    Value: sql/employee.sql
    Origin: class path resource [application.yml]:9:20
    Reason: The elements [spring.datasource.schema[0].classpath,spring.datasource.schema[1].classpath] were left unbound.

Action:

Update your application's configuration


Process finished with exit code 1

BUG分析

  • application.yml配置

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.137.130:3306/jdbc?serviceTimezone=UTC
        username: root
        password: xxxxxx
    
  • 添加schema之前,运行 SpringbootDataJdbcDemoApplicationTests.java 看到自己的默认数据源是HikariDatasource ,课程中的则是来自Tomcat

    class com.zaxxer.hikari.HikariDataSource
    [email protected] wrapping [email protected]
    

    同样可以切换其他数据源,如配置Druid阿里巴巴数据源

  • 添加指定位置的sql文件配置

    schema:
      - classpath: sql/department.sql
      - classpath: sql/employee.sql
    
  • 结果出现了上述的DUG INFO

    Reason: The elements [spring.datasource.schema[0].classpath,spring.datasource.schema[1].classpath] were left unbound.
    

    因为我们配置的schemaList<String>类型,而且List内存放的是String 类型数据,提示元素信息未绑定,是因为在配置的时候,冒号两侧加了空格,导致识别不了此路径下的sql文件

    schema[0]classpath:sql/department.sql

    schema[1]classpath:sql/employee.sql

    private List<String> schema;
    

DEBUG方案

  • 配置spring.datasource.schema时,schema内的元素是Spring类型,classpath冒号两侧不可添加空格

  • 在以上步骤完成后,解决了报错问题,但是,无报错信息,建表还是未成功啊!

  • 查阅相关文档后,给出以下解决方案:

    • 添加spring.datasource.initialization-mode: always

      spring:
        datasource:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://192.168.137.130:3306/jdbc?serviceTimezone=UTC
          username: root
          password: xxxxxx
          schema:
            - classpath:sql/department.sql
            - classpath:sql/employee.sql
          type: com.alibaba.druid.pool.DruidDataSource
          initialization-mode: always
      
  • 至此,完美解决!