简体   繁体   English

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

[英]Custom application properties in spring boot using IntelliJ

I'm trying to use application properties, other than application.properties, say application_local.properties within resources directory.我正在尝试使用应用程序属性,而不是 application.properties,比如资源目录中的application_local.properties

So that I can have 2 properties files, one for local and other the server.这样我就可以有 2 个属性文件,一个用于本地,另一个用于服务器。

As mentioned in many blogs, I should use below command:正如许多博客中提到的,我应该使用以下命令:

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

But this is not working, it is still fetching values from application.properties.但这不起作用,它仍在从 application.properties 获取值。

What am I missing, please suggest?我缺少什么,请提出建议?

Thanks谢谢

1- Follow the naming convention application-{profile}.properties 1- 遵循命名约定 application-{profile}.properties

  • application-local.properties应用程序-local.properties

2-set profile 2 套配置文件

  • -Dspring.profiles.active=local -Dspring.profiles.active=本地

Briefly, you can use these two links:简而言之,您可以使用以下两个链接:

  1. How to load property file based on spring profiles 如何根据弹簧轮廓加载属性文件
  2. spring-profiles弹簧型材

-Dspring.profile.location takes directory as input. -Dspring.profile.location将目录作为输入。 The purpose of this property is to specify additional directory location to keep your property files.此属性的目的是指定额外的目录位置来保存您的属性文件。

You are using property file name in your command.您在命令中使用了属性文件名。

Refer to detailed @ Answer at other thread here请参阅此处其他线程的详细@Answer

Instead you can use as suggested by @mehardad相反,您可以按照@mehardad 的建议使用

The -D option will send parameters to Java virtual machine. -D 选项会将参数发送到 Java 虚拟机。 In order to send parameters to Spring boot, the command option of '--' must be used.为了向 Spring boot 发送参数,必须使用命令选项“--”。

Example:例子:

Suppose, there is an option named 'spring.profiles.active' defined in the application.properties file as follows:假设在 application.properties 文件中定义了一个名为“spring.profiles.active”的选项,如下所示:

spring.profiles.active=dev spring.profiles.active=dev

This option can be overwritten using command line parameter as follows:可以使用命令行参数覆盖此选项,如下所示:

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

Use Spring profiles and choose at runtime, locally would使用 Spring 配置文件并在运行时选择,本地将

-Dspring.profiles.active=local

The property file should be called application-local.properties属性文件应称为application-local.properties

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

相关问题 如何排除默认的application.properties在Spring启动项目的Maven中使用配置文件添加自定义属性文件? - How to exclude default application.properties add custom properties file using profiles in maven for spring boot project? 调试在 Intellij 的自定义任务中启动的 Spring 引导应用程序 - Debug a Spring Boot application started in custom task in Intellij Spring 引导应用程序属性 - Spring boot application Properties Spring 启动自定义属性 - Spring boot Custom Properties Spring 引导应用程序无法在 IntelliJ 上运行 - Spring Boot Application not Working on IntelliJ 如何将带有自定义.properties文件的Spring Boot应用程序部署到AWS ElasticBeanstalk? - How to deploy a Spring Boot application with custom .properties files to AWS ElasticBeanstalk? 在 Spring-boot 的 application.properties 添加自定义变量 - Add custom variables in application.properties of Spring-boot Java Spring 引导自定义属性在 application.yml 中不起作用 - Java Spring Boot custom properties dont work in application.yml Spring 引导应用程序不使用 Application.Properties 文件 - Spring Boot Application not using Application.Properties file 在Spring Boot中加密应用程序属性 - Encrypting application properties in Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM