Spring Boot JPA应用程序不是以spring boot 2.0.2开始的,它可以在Spring boot 1.5.13中正常运行

时间:2018-05-16 00:08:57

标签: java spring spring-boot jpa jdbc

如果我创建连接到PostgresSQL的空Spring Boot JPA应用程序,它可以正常使用Spring Boot 1.5.13。它不适用于Spring Boot 2.0.2。

重新创建的步骤: 1.使用SPRING INITIALIZR(https://start.spring.io/)创建新的Spring Boot应用程序。在项目初始化期间选择以下依赖项“DevTools”,“Web”,“JPA”,“JDBC”“PostgreSQL”

  1. 将数据库连接属性添加到application.properties:

    spring.datasource.url=jdbc:postgresql://localhost:5432/my_db_name
    spring.jpa.hibernate.ddl-auto=none
    spring.datasource.driverClassName=org.postgresql.Driver
    spring.datasource.platform=postgres
    spring.datasource.username=my_user_id
    spring.datasource.password=my_password
    spring.jpa.database=POSTGRESQL
    spring.jpa.show-sql=true
    
  2. 启动Spring Boot应用程序

  3. 如果我使用项目创建选择Spring Boot 1.5.13,则上述步骤可以正常工作。我在Spring Boot 2.0.2中不断收到错误消息:

     Description:
    
    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    
    Reason: Failed to determine a suitable driver class
    
    
    Action:
    
    Consider the following:
        If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
        If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
    

2 个答案:

答案 0 :(得分:0)

只需使用Spring Boot 1.5.13,新版本似乎有一个错误:我遇到了同样的问题,经过长时间的搜索,我决定降级到1.5.13版本。

答案 1 :(得分:0)

我试图将我的应用程序从Spring Boot的Spring Boot 1.5.13转换为2.0.2,我终于弄清楚了问题所在。几个月前,我从事另一个项目,并在Windows环境变量中添加了SPRING_CONFIG_LOCATION = C:\...path...\application.yml。 (我测试了外部配置如何与Grails 3.3一起使用)。我忘记删除此环境变量。

它并没有影响我的Spring boot 1.5.13应用程序。它确实影响了我的2.0.2 Spring引导应用程序,如果设置了application.yml,显然它没有在src/main/resources中使用SPRING_CONFIG_LOCATION。因此,这本身不是Spring Boot的问题,而是我的计算机配置问题。 1.5.3和2.0.4的不同行为使其令人困惑。

如果您遇到类似的问题,建议您在启动过程中确保Spring Boot确实读取了application.propertiesapplication.yml文件。您可以通过在启动过程中打开调试日志记录来实现。您可以通过将此语句添加到application.yml(或类似于application.properties)来打开它:

logging:
  level:
    ROOT: DEBUG
debug: true

如果在此之后的启动过程中未出现调试语句,则表示未读取您的application.ymlapplication.properties。在这种情况下,请通过--debug flag打开调试。我使用gradle,因此在我的情况下,start命令是

gradlew bootRun --debug

然后查看启动消息,并查看是否已读取application.properties或application.yml或是否存在其他问题。