【发布时间】:2018-07-31 06:27:48
【问题描述】:
我有一个 Spring Boot 应用程序,它与 DB 交互以使用 Spring data Rest 提供资源。我想从环境变量中获取配置。下面是我的属性文件。
spring.datasource.url=${mysql.url}
spring.datasource.username=${mysql.user}
spring.datasource.password=${mysql.password}
我的环境变量在图片https://ibb.co/cyxsNc
我什至也尝试过以下配置
spring.datasource.url=${MySQL_Url}
spring.datasource.username=${MySQL_User}
spring.datasource.password=${MySQL_Password}
但我无法连接到数据库并收到以下错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor 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$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
应用程序文件夹结构
Project
|-- src/main/java
|-- com.example.app
|-- DemoApplication.java
|-- src/main/resources
|-- application.properties
注意:如果我设置如下值,配置工作正常
spring.datasource.url=jdbc:mysql://localhost:3306/ex_man
spring.datasource.username=root
spring.datasource.password=root
我错过了什么?
【问题讨论】:
-
不要使用屏幕截图,因为主机可能会宕机。最好是代码摘录。
-
@Sp Sesha,请发布您的主要配置类(如果有)和项目结构。很可能您的应用程序上下文没有选择您的 application.properties 文件(应该在您的类路径中)。
-
那么你的环境变量没有被你的应用程序加载。您如何将它们加载到项目中?
-
在哪里可以解决您的问题?
标签: spring spring-boot