Spring 引导失败 运行 schema.sql 依赖于 spring-cloud-starter-config
Spring Boot fails to run schema.sql with dependency on spring-cloud-starter-config
我的 spring 引导 2.0 应用程序识别并 运行s schema.sql 初始化我的嵌入式 h2 数据库。但是,当我添加 spring-cloud-starter-config 依赖项时,应用程序不再 运行 为 schema.sql。为了说明,使用 spring initializr 生成一个 Spring Boot 2 (v2.0.1) 应用程序,其依赖于:
- 网络
- 其余存储库
- jpa
- h2
- 配置客户端
添加实体:
package com.example.demo;
import javax.persistence.*;
@Entity
public class Room {
@Id
@Column(name = "ROOM_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
//...getters and setters
}
实体的存储库:
package com.example.demo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {
}
Main class 与 initializr 生成的内容没有变化:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
将schema.sql 和data.sql 文件添加到资源下的基础文件夹中。 Spring Boot 使用这些来为实体创建和填充底层 table:
schema.sql:
CREATE TABLE ROOM(
ROOM_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(16) NOT NULL,
);
data.sql:
INSERT INTO ROOM (NAME) VALUES ('Piccadilly');
INSERT INTO ROOM (NAME) VALUES ('Cambridge');
INSERT INTO ROOM (NAME) VALUES ('Oxford');
INSERT INTO ROOM (NAME) VALUES ('Manchester');
将空 application.properties 替换为 application.yml:
spring:
jpa:
hibernate.ddl-auto: none
现在,运行 应用程序并转到 http://localhost:8080/rooms。应用程序将失败并显示 JdbcSQLException,显示 table 不存在:
org.h2.jdbc.JdbcSQLException: Table "ROOM" not found; SQL statement:
select room0_.room_id as room_id1_0_, room0_.name as name2_0_ from room room0_
现在进入 pom.xml 并注释掉对 spring-cloud-starter-config 依赖项的引用:
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
-->
重新启动应用程序,现在一切正常!在浏览器中打开 http://localhost:8080/rooms,您会发现返回了预期的 JSON 结果(4 行)。这告诉我是 spring-cloud-starter-config 依赖项阻止了 Spring 执行 schema.sql 和 data.sql 来初始化数据库。
当我使用 Spring 云依赖时,如何让 Spring 启动以执行 schema.sql 和 data.sql 文件?
我遇到了类似的问题,但在使用 H2 数据库模式创建执行测试期间。我刚刚尝试了较新的版本 - spring-boot-starter-parent:2.0.2.RELEASE
和 Finchley.RC2
用于 Spring Cloud,它适用于我的情况。
用你的例子花了几分钟 - 可以用 Spring Boot 2.0.1 和 Spring Cloud Finchley.RC1 重现,但在 2.0.2 和 Finchley.RC2 上工作正常。
我没能找到 github 问题,但看起来问题已解决。
我的 spring 引导 2.0 应用程序识别并 运行s schema.sql 初始化我的嵌入式 h2 数据库。但是,当我添加 spring-cloud-starter-config 依赖项时,应用程序不再 运行 为 schema.sql。为了说明,使用 spring initializr 生成一个 Spring Boot 2 (v2.0.1) 应用程序,其依赖于:
- 网络
- 其余存储库
- jpa
- h2
- 配置客户端
添加实体:
package com.example.demo;
import javax.persistence.*;
@Entity
public class Room {
@Id
@Column(name = "ROOM_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
//...getters and setters
}
实体的存储库:
package com.example.demo;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RoomRepository extends CrudRepository<Room, Long> {
}
Main class 与 initializr 生成的内容没有变化:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
将schema.sql 和data.sql 文件添加到资源下的基础文件夹中。 Spring Boot 使用这些来为实体创建和填充底层 table:
schema.sql:
CREATE TABLE ROOM(
ROOM_ID BIGINT AUTO_INCREMENT PRIMARY KEY,
NAME VARCHAR(16) NOT NULL,
);
data.sql:
INSERT INTO ROOM (NAME) VALUES ('Piccadilly');
INSERT INTO ROOM (NAME) VALUES ('Cambridge');
INSERT INTO ROOM (NAME) VALUES ('Oxford');
INSERT INTO ROOM (NAME) VALUES ('Manchester');
将空 application.properties 替换为 application.yml:
spring:
jpa:
hibernate.ddl-auto: none
现在,运行 应用程序并转到 http://localhost:8080/rooms。应用程序将失败并显示 JdbcSQLException,显示 table 不存在:
org.h2.jdbc.JdbcSQLException: Table "ROOM" not found; SQL statement: select room0_.room_id as room_id1_0_, room0_.name as name2_0_ from room room0_
现在进入 pom.xml 并注释掉对 spring-cloud-starter-config 依赖项的引用:
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
-->
重新启动应用程序,现在一切正常!在浏览器中打开 http://localhost:8080/rooms,您会发现返回了预期的 JSON 结果(4 行)。这告诉我是 spring-cloud-starter-config 依赖项阻止了 Spring 执行 schema.sql 和 data.sql 来初始化数据库。
当我使用 Spring 云依赖时,如何让 Spring 启动以执行 schema.sql 和 data.sql 文件?
我遇到了类似的问题,但在使用 H2 数据库模式创建执行测试期间。我刚刚尝试了较新的版本 - spring-boot-starter-parent:2.0.2.RELEASE
和 Finchley.RC2
用于 Spring Cloud,它适用于我的情况。
用你的例子花了几分钟 - 可以用 Spring Boot 2.0.1 和 Spring Cloud Finchley.RC1 重现,但在 2.0.2 和 Finchley.RC2 上工作正常。 我没能找到 github 问题,但看起来问题已解决。