简体   繁体   English

spring 基于环境变量的引导自动配置

[英]spring boot autoconfiguration based on env variables

In my understanding in a spring-boot application when having spring-boot-starter-data-jpa on your classpath and also mysql-connector spring-boot will still try to use in memory h2 (as per default), if no db related properties are defined.据我了解,在 spring-boot 应用程序中,当您的类路径上有 spring-boot-starter-data-jpa 和mysql-connector spring-boot时,spring-boot 应用程序仍将尝试在 ZCD69B4957F06CD818D7BF23D61980E2 中使用相关属性(如果默认情况下没有定义)。

But if you have properties like the following:但是,如果您具有以下属性:

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

It will use mysql database.它将使用 mysql 数据库。

My question is: what happens if you have those properties only declared as environmental variables?我的问题是:如果您将这些属性仅声明为环境变量会怎样? Is this behaviour still valid?这种行为仍然有效吗?

thanks!谢谢!

I tried using the following dependencies together: "web", "mysql", "jpa".我尝试一起使用以下依赖项:“web”、“mysql”、“jpa”。 A simple run without any configuration shows the following error:没有任何配置的简单运行显示以下错误:

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

And if you use the environment route instead of "application.properties" as long as you set them correctly it should act the same as a fully configured "application.properties" file.如果您使用环境路由而不是“application.properties”,只要您正确设置它们,它应该与完全配置的“application.properties”文件相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM