简体   繁体   English

在没有 spring 的情况下设置活动配置文件

[英]set active profile without spring

Our web app need to run on two different servers.我们的 web 应用程序需要在两个不同的服务器上运行。 There are also two different properties files for those servers, like application.prd1.properties and application.prd1.properties.这些服务器还有两个不同的属性文件,如 application.prd1.properties 和 application.prd1.properties。 I do not use spring boot.我不使用 spring 引导。 So I can not use -Dspring.profiles.active=prd1 script.所以我不能使用-Dspring.profiles.active=prd1脚本。

Now I am getting the properties file like below image现在我得到如下图所示的属性文件在此处输入图像描述

How can I use two different properties files on two different servers without spring boot?如何在没有 spring 引导的情况下在两个不同的服务器上使用两个不同的属性文件?

I suppose you have a jar jar file with your application.我想你的应用程序有一个 jar jar 文件。 Given that, you can create a script to run it based on the server.鉴于此,您可以创建一个脚本来基于服务器运行它。

In the entry point of the application you can parse the arguments:在应用程序的入口点,您可以解析 arguments:

public static void Main(String[] args){
    if (args.length == 1)
        System.exit(1);

    String server = args[1];
    init(server);
}

Then, the init will be a little bit different:然后,init 会有点不同:

static void init(String server) {
ResourceBundle resourceBundle;
switch (server){
    case "server1":
        resourceBundle = ResourceBundle.getBundle("application.server1.properties");
        break;
    }
}

Note: Be aware that the above code does not compile.注意:请注意,上面的代码无法编译。

I have defined local variable for different servers like -DENV=prd1 and -DENV=prd2我已经为不同的服务器定义了局部变量,比如-DENV=prd1-DENV=prd2

public static void init(String server) {
  String env = System.getProperty("ENV")
  ResourceBundle res = ResourceBundle.getBundle("application." + env );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM