Java 基于env变量的spring引导自动配置

Java 基于env变量的spring引导自动配置,java,mysql,spring-boot,properties,Java,Mysql,Spring Boot,Properties,根据我的理解,在spring引导应用程序中,在类路径和mysql连接器上有spring引导启动器数据jpa时 如果未定义与db相关的属性,spring boot仍将尝试在内存h2中使用(根据默认设置) 但如果您具有如下属性: spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example spring.datasource.usernam

根据我的理解,在spring引导应用程序中,在类路径和mysql连接器上有spring引导启动器数据jpa时 如果未定义与db相关的属性,spring boot仍将尝试在内存h2中使用(根据默认设置)

但如果您具有如下属性:

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
它将使用mysql数据库

我的问题是: 如果这些属性只声明为环境变量,会发生什么? 这种行为仍然有效吗


谢谢

我尝试同时使用以下依赖项:“web”、“mysql”、“jpa”。不进行任何配置的简单运行会显示以下错误:

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

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).

如果您使用环境路由而不是“application.properties”,只要您设置正确,它应该与完全配置的“application.properties”文件相同。

哦,是的,在我的应用程序中,我也有h2,这就是为什么默认情况下会选择它。非常感谢您的尝试!是的,在属性文件中可以使用环境变量占位符。看见