Spring Boot 应用程序中特定于环境的 application.properties 文件

编程入门 行业动态 更新时间:2024-10-28 05:23:53
本文介绍了Spring Boot 应用程序中特定于环境的 application.properties 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的 Spring Boot 应用程序中,我想创建特定于环境的属性文件.我的应用程序在 war 中的打包类型,我正在嵌入式 tomcat 中执行它.我使用 sts 并从 sts 本身执行 main.

In my Spring Boot application, i want to create environment specific properties file. The packaging type of my application in war and i am executing it in embedded tomcat. I use sts and execute the main from sts itself.

  • 我可以拥有特定于环境的属性文件,例如 application-${env-value}.properties 吗?
  • 在上述情况下,env-value 的值为 local/devl/test/prod

    In above case, env-value will have values as local/devl/test/prod

  • 在哪里设置 env-value 文件?对于本地,我可以通过 sts 将其设置为 jvm 参数

  • Where to set the env-value file? For local, i can set it as the jvm argument through sts

    谁读取 Spring Boot 应用程序中的 application.properties.

    Who reads the application.properties in Spring Boot application.

    如何加载环境特定的属性文件?例如 - 如果我在环境特定的属性文件中设置数据库 uid、pwd、schema 等,在这种情况下,数据源是否能够理解其中的属性?

    How to load the environment specific properties file? For ex - if i set the database uid,pwd, schema etc in environment specific property file, in that case will the datasource be able to understand the properties in it?

    我可以同时使用 application.properties 和 application-local.properties 文件吗?

    Can i use application.properties and application-local.properties file at same time?

    推荐答案

    Spring Boot 已经 支持 profile 基于属性.

    Spring Boot already has support for profile based properties.

    只需添加一个 application-[profile].properties 文件并使用 spring.profiles.active 属性指定要使用的配置文件.

    Simply add an application-[profile].properties file and specify the profiles to use using the spring.profiles.active property.

    -Dspring.profiles.active=local

    这将加载 application.properties 和 application-local.properties ,后者覆盖第一个属性.

    This will load the application.properties and the application-local.properties with the latter overriding properties from the first.

  • 更多推荐

    Spring Boot 应用程序中特定于环境的 application.properties 文件

    本文发布于:2023-11-14 02:04:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1585929.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:中特   应用程序   环境   文件   Boot

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!