英文:
Reading .env and properties.yml files based on profile in Spring Boot?
问题
-
根据我所知,这些文件会根据IntelliJ或Maven的运行/调试配置文件自动读取。如果活动配置文件是"dev",则只会读取
.env-dev
和properties-dev.yml
文件;如果配置文件是"prod",则只会读取.env-prod
和properties-prod.yml
文件。如果配置文件是"dev,prod",则两种文件都会被读取。这样是否正确? -
如果项目中只有
.env
和properties.yml
文件,那么当选择配置文件时,这些文件是否会被始终读取? -
当通过Maven运行/调试应用程序时,是否可以通过以下命令来读取
.env
文件中的环境变量?
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug"
-Dspring-boot.run.profiles=dev -Dspring-boot.run.arguments="DB_NAME=employee_db
DB_USERNAME=postgres DB_PASSWORD=******"
英文:
I am using .env
and properties.yml
files in my Spring Boot apps and need to be clarified for using them properly. After that, I will also use the other profiles of these files e.g. .env-dev
and properties-dev.yml
.
Could you please explain these issues?
1. As far as I know, these files are automatically read based on the Run/Debug profile of Intellij or maven. If the active profile is dev, only .env-dev
and properties-dev.yml
files are read, if the profile is prod, only .env-prod
and properties-prod.yml
files are read. If the profile "dev,prod", then both of these files are read. Is that true?
2. What if there are only .env
and properties.yml
files in the project. Then, are these files always read when profile is selected or not?
3. Can I read environment variables from .env
file when running/debugging app via Maven by giving file name to the following command?
mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xdebug"
-Dspring-boot.run.profiles=dev -Dspring-boot.run.arguments="DB_NAME=employee_db
DB_USERNAME=postgres DB_PASSWORD=******"
答案1
得分: 0
不要惊慌!它/必须正常运行...
开始简单:
-
只是一个演示(没有/空的
application.properties
,根据foo.bar, foo.baz
,我们可以引用任何常见/自定义“属性” ;):package com.example.demo; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoConfigApplication { public static void main(String[] args) { SpringApplication.run(DemoConfigApplication.class, args); } @Bean InitializingBean test( @Value("${foo.bar:undefined}") String fooBar, @Value("${foo.baz:undefined}") String fooBaz ) { return () -> { System.err.format("foo.bar: %s%n", fooBar); System.err.format("foo.baz: %s%n", fooBaz); }; } }
-
构建
./mvnw clean test
输出:
... foo.bar: undefined foo.baz: undefined ...
要通过(Spring Boot)Maven插件设置这些,我们可以:
((Git)Ba)sh
-
通过“命令行参数”
(带空格,使用我的win+gitbash:GNU bash, version 5.2.12(1)-release (x86_64-pc-msys)
)
语法:
./mvnw spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"
(输出):
... 2023-03-15T13:45:52.849+01:00 INFO 4196 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1106 ms foo.bar: Foo Bar foo.baz: Foo Baz ...
-
通过“系统属性”(相同的git bash):
语法:
./mvnw spring-boot:run -Dspring-boot.run.jvmArguments="-Dfoo.bar='Foo Bar' -Dfoo.baz='Foo Baz'"
要使(例如)1. 在
cmd.exe
(版本10.0.19045.2673),与gitbash相同的引号:
.\mvnw.cmd spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"
PowerShell
(v7.3.3)
更复杂,因为/与参考有关:
.\mvnw.cmd spring-boot:run '-Dspring-boot.run.arguments="--foo.bar=""Foo Bar"" --foo.baz=""Foo Baz"""'
...
1: https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html
2: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.3
英文:
Don't panic! It is/must be working...
Start simple:
-
Just a demo (no/empty
application.properties
, according tofoo.bar, foo.baz
we can refer to any common/custom "property" ;):package com.example.demo; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class DemoConfigApplication { public static void main(String[] args) { SpringApplication.run(DemoConfigApplication.class, args); } @Bean InitializingBean test( @Value("${foo.bar:undefined}") String fooBar, @Value("${foo.baz:undefined}") String fooBaz ) { return () -> { System.err.format("foo.bar: %s%n", fooBar); System.err.format("foo.baz: %s%n", fooBaz); }; } }
-
Build
./mvnw clean test
prints:
... foo.bar: undefined foo.baz: undefined ...
To set these via (springboot-)maven-plugin, we can:
((Git)Ba)sh
-
Via "Command Line Arguments"
(with blanks, with my win+gitbash:GNU bash, version 5.2.12(1)-release (x86_64-pc-msys)
)
Syntax:
./mvnw spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"
(prints):
... 2023-03-15T13:45:52.849+01:00 INFO 4196 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1106 ms foo.bar: Foo Bar foo.baz: Foo Baz ...
-
Via "System Properties" (same git bash):
Syntax:
./mvnw spring-boot:run -Dspring-boot.run.jvmArguments="-Dfoo.bar='Foo Bar' -Dfoo.baz='Foo Baz'"
To make (e.g.) 1. work in
cmd.exe
(Version 10.0.19045.2673), same quoting as gitbash:
.\mvnw.cmd spring-boot:run -Dspring-boot.run.arguments="--foo.bar='Foo Bar' --foo.baz='Foo Baz'"
PowerShell
(v7.3.3)
More complicated, due to/with ref:
.\mvnw.cmd spring-boot:run '-Dspring-boot.run.arguments="--foo.bar=""Foo Bar"" --foo.baz=""Foo Baz"""'
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论