使用 IntelliJ 在 Spring Boot 中自定义应用程序属性

     2023-02-26     196

关键词:

【中文标题】使用 IntelliJ 在 Spring Boot 中自定义应用程序属性【英文标题】:Custom application properties in spring boot using IntelliJ 【发布时间】:2020-04-30 05:00:37 【问题描述】:

我正在尝试使用 application.properties 以外的应用程序属性,例如资源目录中的 application_local.properties

这样我就可以拥有 2 个属性文件,一个用于本地,另一个用于服务器。

正如许多博客中提到的,我应该使用以下命令:

spring-boot:run -Dspring.config.location=/Users/myuser/work/MyProject/my-app/src/main/resources/application_local.properties

但这不起作用,它仍在从 application.properties 获取值。

我错过了什么,请提出建议?

谢谢

【问题讨论】:

在 spring 文档中有清楚的解释:docs.spring.io/spring-boot/docs/current/reference/html/… 【参考方案1】:

1- 遵循命名约定 application-profile.properties

application-local.properties

2 套配置文件

-Dspring.profiles.active=local

简单来说,你可以使用这两个链接:

    How to load property file based on spring profiles spring-profiles

【讨论】:

【参考方案2】:

-Dspring.profile.location 将目录作为输入。此属性的目的是指定额外的目录位置来保存您的属性文件。

您在命令中使用了属性文件名。

详细参考@Answer at other thread here

您可以按照@mehardad的建议使用

【讨论】:

【参考方案3】:

-D 选项将参数发送到 Java 虚拟机。为了向Spring boot发送参数,必须使用'--'命令选项。

例子:

假设,在 application.properties 文件中定义了一个名为“spring.profiles.active”的选项,如下所示:

spring.profiles.active=dev

可以使用命令行参数覆盖该选项,如下所示:

java -jar application.jar --spring.profiles.active=prod

【讨论】:

【参考方案4】:

使用 Spring 配置文件并在运行时选择,本地会

-Dspring.profiles.active=local

属性文件应该叫application-local.properties

【讨论】:

使用 Intellij 在 Spring Boot 中使用 Gradle 构建 docker 映像

】使用Intellij在SpringBoot中使用Gradle构建docker映像【英文标题】:BuilddockerimageusingGradleinSpringBootusingIntellij【发布时间】:2018-08-2607:16:12【问题描述】:我有以下dockerfile。FROMopenjdk:8-jdk-alpineVOLUME/tmpARGJAR_FILEADD$JAR_FILEapp.jarENTRYPOIN 查看详情

如何在 IDEA Intellij 上使用 Spring-boot 进行自动重新加载

】如何在IDEAIntellij上使用Spring-boot进行自动重新加载【英文标题】:howtomakeautoreloadwithSpring-bootonIDEAIntellij【发布时间】:2016-01-2518:30:33【问题描述】:我写了一个基于Spring-boot,tomcat,freemarker的项目,我运行成功了,但是每当我修... 查看详情

使用 IntelliJ 在 Spring Boot 中自定义应用程序属性

】使用IntelliJ在SpringBoot中自定义应用程序属性【英文标题】:CustomapplicationpropertiesinspringbootusingIntelliJ【发布时间】:2020-04-3005:00:37【问题描述】:我正在尝试使用application.properties以外的应用程序属性,例如资源目录中的applicatio... 查看详情

Spring Boot 开发工具 IntelliJ

】SpringBoot开发工具IntelliJ【英文标题】:SpringBootdevtoolsIntelliJ【发布时间】:2015-12-1412:08:11【问题描述】:我正在使用SpringBoot1.3.0.M5,并且正在尝试利用devtools。这允许您在开发过程中对应用程序进行更改,并且引导将重新加载... 查看详情

在运行时使用 intellij 在 Spring boot application.yml 文件中注入占位符值

】在运行时使用intellij在Springbootapplication.yml文件中注入占位符值【英文标题】:InjectplaceholdervaluesatSpringbootapplication.ymlfileusingintellijatruntime【发布时间】:2020-06-2622:36:49【问题描述】:我正在使用Intellj运行我的SpringBoot应用程序,... 查看详情

使用 Spring Boot 和 JUnit5 在 Intellij 中终止所有测试

】使用SpringBoot和JUnit5在Intellij中终止所有测试【英文标题】:AlltestsgetterminatedinIntellijwithSpringBootandJUnit5【发布时间】:2020-09-1921:45:29【问题描述】:我试图在我的SpringBoot应用程序中使用JUnit5编写一个简单的测试,但我注意到我... 查看详情

使用 Groovy、IntelliJ 和 Gradle 在项目中“重新运行 Spring Boot 配置注释处理器”

】使用Groovy、IntelliJ和Gradle在项目中“重新运行SpringBoot配置注释处理器”【英文标题】:"Re-runSpringBootConfigurationAnnotationProcessor"inprojectwithGroovy,IntelliJandGradle【发布时间】:2017-05-2107:06:54【问题描述】:我正在使用IntelliJ15.... 查看详情

Intellij idea thymeleaf 在 spring boot 项目中完成

】Intellijideathymeleaf在springboot项目中完成【英文标题】:Intellijideathymeleafcompletioninspringbootproject【发布时间】:2016-11-3001:55:09【问题描述】:我在Intellijidea中的SpringBoot/Thymeleaf项目中遇到代码辅助问题。我已经使用gradle建立了一个... 查看详情

使用 Spring Boot 和多模块的 Maven 配置 - 在 Intellij 中运行应用程序

】使用SpringBoot和多模块的Maven配置-在Intellij中运行应用程序【英文标题】:MavenconfigurationwithSpringBoot&multimodules-runapplicationinIntellij【发布时间】:2015-05-2905:26:34【问题描述】:我目前正在使用SpringBoot开发RESTAPI。我是Maven新手,... 查看详情

无法在 IntelliJ Idea 中启动 spring-boot 应用程序

】无法在IntelliJIdea中启动spring-boot应用程序【英文标题】:Unabletostartspring-bootapplicationinIntelliJIdea【发布时间】:2016-12-2802:52:27【问题描述】:概述我可以使用spring-boot的maven插件运行应用程序,但不能使用IDE运行它。以下几行详... 查看详情

如何配置 IntelliJ HQL 控制台以使用 Spring Boot

】如何配置IntelliJHQL控制台以使用SpringBoot【英文标题】:HowdoIconfiguretheIntelliJHQLConsoletoworkwithSpringBoot【发布时间】:2016-01-1901:03:17【问题描述】:我正在使用SpringBoot和IntelliJ。因为SpringBoot会自动配置数据库,所以没有hibernate.cfg.x... 查看详情

有没有办法在 IntelliJ IDEA 中使用单个运行配置运行多个 Spring Boot 应用程序?

】有没有办法在IntelliJIDEA中使用单个运行配置运行多个SpringBoot应用程序?【英文标题】:IsthereawaytorunmultipleSpringBootapplicationswithasingleRunningConfigurationinIntelliJIDEA?【发布时间】:2016-07-0312:12:53【问题描述】:我在一个IntelliJ项目中... 查看详情

如何在 IntelliJ IDEA 中为 Spring Boot 项目构建 jar 工件

】如何在IntelliJIDEA中为SpringBoot项目构建jar工件【英文标题】:HowtobuildthejarartifactforaSpringBootprojectinIntelliJIDEA【发布时间】:2017-06-0314:28:01【问题描述】:我正在使用IntelliJIDEA2016.2.5,并且我已经使用SpringInitializr来设置SpringBoot项... 查看详情

尝试使用 intellij Datagrip 连接到在 Spring Boot 包中创建的数据库

】尝试使用intellijDatagrip连接到在SpringBoot包中创建的数据库【英文标题】:TryingtoconnecttoadatabasecreatedinaspringbootpackagewithintelijDatagrip【发布时间】:2017-11-1319:04:54【问题描述】:我在Eclipse中创建了一个带有H2嵌入式数据库和JPA的Spri... 查看详情

让 Spring Boot 颜色控制台日志记录在 Intellij 中工作?

】让SpringBoot颜色控制台日志记录在Intellij中工作?【英文标题】:GettingSpringBootcolorconsoleloggingworkingwithinIntellij?【发布时间】:2015-05-0106:32:31【问题描述】:有没有人想出如何在IntellijIdea中为SpringBoot应用程序获得颜色输出?【问... 查看详情

如何使用 IntelliJ IDEA 社区版调试 spring-boot 应用程序?

】如何使用IntelliJIDEA社区版调试spring-boot应用程序?【英文标题】:Howtodebugspring-bootapplicationwithIntelliJIDEAcommunityEdition?【发布时间】:2017-10-2103:46:59【问题描述】:我在IntelliJIDEA社区版上调试Javaspring-boot应用程序时遇到了困难。... 查看详情

IntelliJ IDEA 中的 Spring Boot 项目在重启后停止工作

】IntelliJIDEA中的SpringBoot项目在重启后停止工作【英文标题】:SpringBootprojectinIntelliJIDEAstopsworkingafterreboot【发布时间】:2018-05-2108:09:25【问题描述】:这可能是一个新手问题,但我无法解决问题。我使用IntelliJIDEA中的SpringInitializr... 查看详情

Spring Boot 注释和导入无法在 Intellij IDE 上解析

】SpringBoot注释和导入无法在IntellijIDE上解析【英文标题】:SpringbootannotationsandimportsnotresolvingonIntellijIDE【发布时间】:2020-04-1417:49:41【问题描述】:这个项目运行良好。我只是在pom.xml中使用Springboot版本,导入了一些新的maven项目... 查看详情