设置 Spring Profile 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15814497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:50:45  来源:igfitidea点击:

Setting Spring Profile variable

springspring-bootspring-profiles

提问by Maheshwaran K

I have two Spring profiles: devand test. I want to set the active profile in the server environment, I don't want to set it in my code so that wherever I deploy my application the profile gets loaded based on the profile in the server. How can I do that?

我有两个 Spring 配置文件:devtest. 我想在服务器环境中设置活动配置文件,我不想在我的代码中设置它,这样无论我在哪里部署我的应用程序,配置文件都会根据服务器中的配置文件加载。我怎样才能做到这一点?

回答by hyness

You can simply set a system property on the server as follows...

您可以简单地在服务器上设置系统属性,如下所示...

-Dspring.profiles.active=test

Edit:To add this to tomcat in eclipse, select Run -> Run Configurationsand choose your Tomcat run configuration. Click the Argumentstab and add -Dspring.profiles.active=test at the end of VM arguments. Another way would be to add the property to your catalina.properties in your Serversproject, but if you add it there omit the -D

编辑:要将其添加到 eclipse 中的 tomcat,请选择Run -> Run Configurations并选择您的 Tomcat 运行配置。单击Arguments选项卡并在 末尾添加 -Dspring.profiles.active=test VM arguments。另一种方法是将属性添加到Servers项目中的 catalina.properties 中,但如果添加它,则省略 -D

Edit:For use with Spring Boot, you have an additional choice. You can pass the property as a program argument if you prepend the property with two dashes.

编辑:要与Spring Boot一起使用,您还有一个额外的选择。如果在属性前面加上两个破折号,则可以将该属性作为程序参数传递。

Here are two examples using a Spring Boot executable jar file...

以下是使用 Spring Boot 可执行 jar 文件的两个示例...

System Property

系统属性

[user@host ~]$ java -jar -Dspring.profiles.active=test myproject.jar

Program Argument

程序参数

[user@host ~]$ java -jar myproject.jar --spring.profiles.active=test

回答by Nilesh

There are at least two ways to do that:

至少有两种方法可以做到这一点:

  1. defining context param in web.xml – that breaks "one package for all environments" statement. I don't recommend that

  2. defining system property -Dspring.profiles.active=your-active-profile

  1. 在 web.xml 中定义上下文参数——这打破了“一个包适用于所有环境”的说法。我不建议这样做

  2. 定义系统属性 -Dspring.profiles.active=your-active-profile

I believe that defining system property is a much better approach. So how to define system property for Tomcat? On the internet I could find a lot of advice like "modify catalina.sh" because you will not find any configuration file for doing stuff like that. Modifying catalina.sh is a dirty unmaintainable solution. There is a better way to do that.

我相信定义系统属性是一种更好的方法。那么如何为Tomcat定义系统属性呢?在互联网上,我可以找到很多建议,例如“修改 catalina.sh”,因为您不会找到任何配置文件来执行此类操作。修改 catalina.sh 是一个肮脏的无法维护的解决方案。有一种更好的方法可以做到这一点。

Just create file setenv.sh in Tomcat's bin directory with content:

只需在 Tomcat 的 bin 目录中创建文件 setenv.sh ,内容如下:

JAVA_OPTS="$JAVA_OPTS -Dspring.profiles.active=dev"

and it will be loaded automatically during running catalina.sh start or run.

它会在运行 catalina.sh start 或 run 时自动加载。

Here is a blogdescribing the above solution. The comments section is interesting as it talks about more details.

这是描述上述解决方案的博客。评论部分很有趣,因为它讨论了更多细节。

回答by Abdullah Khan

For Eclipse, setting -Dspring.profiles.activevariable in the VM arguments would do the trick.

对于 Eclipse,-Dspring.profiles.active在 VM 参数中设置变量可以解决问题。

Go to

Right Click Project --> Run as --> Run Configurations --> Arguments

右键单击项目 --> 运行方式 --> 运行配置 --> 参数

And add your -Dspring.profiles.active=devin the VM arguments

-Dspring.profiles.active=devVM 参数中添加您的

回答by R.S

as System environment Variable:

作为系统环境变量:

Windows:Start -> type "envi" select environment variables and add a new: Name: spring_profiles_active Value: dev(or whatever yours is)

Windows:开始 -> 输入“envi”选择环境变量并添加一个新的:( Name: spring_profiles_active Value: dev或任何你的)

Linux:add following line to /etc/environment under PATH:

Linux:在 PATH 下的 /etc/environment 添加以下行:

spring_profiles_active=prod(or whatever profile is)

spring_profiles_active=prod(或任何个人资料)

then also export spring_profiles_active=prodso you have it in the runtime now.

那么export spring_profiles_active=prod你现在也可以在运行时中使用它。

回答by Ram Pratap

For Tomcat 8:

对于 Tomcat 8:

Linux :

Linux:

Create setenv.sh and update it with following:

创建 setenv.sh 并使用以下内容更新它:

export SPRING_PROFILES_ACTIVE=dev

出口 SPRING_PROFILES_ACTIVE=dev

Windows:

视窗:

Create setenv.bat and update it with following:

创建 setenv.bat 并使用以下内容更新它:

set SPRING_PROFILES_ACTIVE=dev

设置 SPRING_PROFILES_ACTIVE=dev