使用 Spring 引导配置文件和命令行参数
Using Spring Boot profiles with command line arguments
我对如何在 Spring 引导应用程序中使用配置文件感到困惑。
假设我在 pom.xml
文件中定义了配置文件,
这是我找到的不同方法。
mvnw -Pdev
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
mvnw spring-boot:run -Dspring.profiles.active=dev
mvnw spring-boot:run --spring.profiles.active=dev
1/它们有什么区别吗?
2/ 最好的使用方法是什么?
您不应该混淆 Maven 配置文件和 Spring 配置文件 - 这是完全不同的事情。
mvnw spring-boot:run -Dspring.profiles.active=dev
- 它告诉 Spring 在运行时使用哪个配置文件。
Spring 配置文件 - 您的 Spring 应用程序将如何在运行时根据活动配置文件进行配置。
您可以阅读更多相关信息 here and here。
mvnw -P dev
- 它告诉 Maven 使用构建 Maven 配置文件
Maven 配置文件 - 就是您的 Java 应用程序 compiled/build/packed 的样子。它与 compiling/packaging 相关,与 Spring 应用程序的运行时执行无关,并且与 Spring 完全无关。
您可以阅读更多相关信息 here and here。
这是对您第一个问题的回答。
关于你的第二个问题,
当您需要根据配置文件设置以不同方式构建应用程序时,将使用 Maven 配置文件。例如,当你执行 mvn -P fat-jar
.
时,打包一个 "fat jar" 和所有依赖项
Spring 配置文件用于配置 Spring 应用程序以在运行时以不同方式工作,例如 - 使用 development
和 production
数据库等.
mvnw -Pdev
这会考虑maven profile
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
这会考虑maven profile
mvnw spring-boot:run -Dspring.profiles.active=dev
这将考虑 spring 引导配置文件
mvnw spring-boot:run --spring.profiles.active=dev
这应该行不通,因为 maven 不理解 --spring
我对如何在 Spring 引导应用程序中使用配置文件感到困惑。
假设我在 pom.xml
文件中定义了配置文件,
这是我找到的不同方法。
mvnw -Pdev
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
mvnw spring-boot:run -Dspring.profiles.active=dev
mvnw spring-boot:run --spring.profiles.active=dev
1/它们有什么区别吗?
2/ 最好的使用方法是什么?
您不应该混淆 Maven 配置文件和 Spring 配置文件 - 这是完全不同的事情。
mvnw spring-boot:run -Dspring.profiles.active=dev
- 它告诉 Spring 在运行时使用哪个配置文件。
Spring 配置文件 - 您的 Spring 应用程序将如何在运行时根据活动配置文件进行配置。 您可以阅读更多相关信息 here and here。
mvnw -P dev
- 它告诉 Maven 使用构建 Maven 配置文件
Maven 配置文件 - 就是您的 Java 应用程序 compiled/build/packed 的样子。它与 compiling/packaging 相关,与 Spring 应用程序的运行时执行无关,并且与 Spring 完全无关。 您可以阅读更多相关信息 here and here。
这是对您第一个问题的回答。
关于你的第二个问题,
当您需要根据配置文件设置以不同方式构建应用程序时,将使用 Maven 配置文件。例如,当你执行 mvn -P fat-jar
.
Spring 配置文件用于配置 Spring 应用程序以在运行时以不同方式工作,例如 - 使用 development
和 production
数据库等.
mvnw -Pdev
这会考虑maven profile
mvnw spring-boot:run -Dspring-boot.run.profiles=dev
这会考虑maven profile
mvnw spring-boot:run -Dspring.profiles.active=dev
这将考虑 spring 引导配置文件
mvnw spring-boot:run --spring.profiles.active=dev
这应该行不通,因为 maven 不理解 --spring