如何在Spring中使用application.properties设置配置文件?

如何在Spring中使用application.properties设置配置文件?,第1张

如何在Spring中使用application.properties设置配置文件

有几种更改活动配置文件的方法,这些方法都不直接取自属性文件。

  • 您可以
    <init-param>
    像在问题中一样使用。
  • 您可以在应用程序启动时提供系统参数
    -Dspring.profiles.active="master"
  • 你可以得到
    ConfigurableEnvironment
    从你
    ApplicationContext
    setActiveProfiles(String...)
    编程方式与
    context.getEnvironment().setActiveProfiles("container");

您可以使用

ApplicationListener
侦听上下文初始化。有关如何执行此 *** 作的说明。您可以使用
ContextStartedEvent

ContextStartedEvent event = ...; // from method argumentConfigurableEnvironment env = (ConfigurableEnvironment) event.getApplicationContext().getEnvironment();env.setActiveProfiles("master");

您可以根据需要

"master"
从属性文件中获取值。



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5475716.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-12
下一篇2022-12-12

发表评论

登录后才能评论

评论列表(0条)

    保存