在spring.datasource.schema中指定的路径前加上” classpath: “
Spring Boot启动时发生错误
「无效的配置属性值异常:属性spring.datasource.schema的值’ServletContext 资源[/schema.sql]’是无效的:指定的资源不存在。」这是一种说法。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sampleController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sampleService': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcTemplate' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcTemplate' 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.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.schema with value 'ServletContext resource [/schema.sql]' is invalid: The specified resource does not exist.
(中略)
Caused by: org.springframework.boot.context.properties.source.InvalidConfigurationPropertyValueException: Property spring.datasource.schema with value 'ServletContext resource [/schema.sql]' is invalid: The specified resource does not exist.
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getResources(DataSourceInitializer.java:163) ~[spring-boot-autoconfigure-2.2.0.M5.jar!/:2.2.0.M5]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getScripts(DataSourceInitializer.java:146) ~[spring-boot-autoconfigure-2.2.0.M5.jar!/:2.2.0.M5]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.createSchema(DataSourceInitializer.java:93) ~[spring-boot-autoconfigure-2.2.0.M5.jar!/:2.2.0.M5]
at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:62) ~[spring-boot-autoconfigure-2.2.0.M5.jar!/:2.2.0.M5]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.2.0.RC1.jar!/:5.2.0.RC1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.2.0.RC1.jar!/:5.2.0.RC1]
... 77 common frames omitted
在文件名前面加上「classpath:」进行解析
在指定 application.properties 文件的 spring.datasource.schema 和 spring.datasource.data 前面需要加上 “classpath:”。
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=java
spring.datasource.password=cafebabe
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.initialization-mode=always
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
spring.datasource.sql-script-encoding=utf-8
请参考这些信息
-
- Property spring.datasource.schema with value ‘class path resource [schema-mysql.sql]’ is invalid: The specified resource does not exist – Stack Overflow
- Spring Boot Reference Documentation – Appendices – Appendix A: Common application properties