Java 如何从系统变量设置 Spring 配置文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38520638/
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-08-11 20:23:06  来源:igfitidea点击:

How to set Spring profile from system variable?

javaspringspring-profiles

提问by user2656851

I have a Spring project which uses another project. Each project has its own spring profile initialize from java code using applicationContext.xmland *.propertiesfor each profile. I inject the profile from args[]. The problem is that second project uses the default configuration for the env from theapplicationContext.xmlI can not inject the env from args[]to the second project and I tried looking for an article which will explain how Spring profile works.

我有一个使用另一个项目的 Spring 项目。每个项目都使用Java代码有自己的春天轮廓初始化applicationContext.xml*.properties每个配置文件。我从args[]. 问题是第二个项目使用了 env 的默认配置,applicationContext.xml我无法将 env 注入args[]到第二个项目中,我尝试寻找一篇文章来解释 Spring 配置文件的工作原理。

  1. Is there a hierarchy on which it will look the profile when default is not configured at applicationContext.xml?
  2. Is System var stronger than applicationContext.xmlconfiguration?
  3. What you think is the best solution to my challenge?
  1. 当默认值未配置为 时,是否有一个层次结构可以查看配置文件applicationContext.xml
  2. 系统变量比applicationContext.xml配置强吗?
  3. 您认为应对我的挑战的最佳解决方案是什么?

Articles on that subject or even examples would be most appreciated!! Thanks in advance.

关于该主题的文章甚至示例将不胜感激!!提前致谢。

回答by JanTheGun

If you provide your JVM the Spring profile there should be no problems:

如果您提供 JVM 的 Spring 配置文件,则应该没有问题:

java -Dspring.profiles.active=development -jar yourApplication.jar 

Also see Spring-Documentation:

另请参阅 Spring 文档:

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

69.5 Set the active Spring profiles

The Spring Environment has an API for this, but normally you would set a System property (spring.profiles.active) or an OS environment variable (SPRING_PROFILES_ACTIVE). E.g. launch your application with a -D argument (remember to put it before the main class or jar archive):

$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

In Spring Boot you can also set the active profile in application.properties, e.g.

spring.profiles.active=production

A value set this way is replaced by the System property or environment variable setting, but not by the SpringApplicationBuilder.profiles() method. Thus the latter Java API can be used to augment the profiles without changing the defaults.

See Chapter 25, Profiles in the ‘Spring Boot features' section for more information.

69.5 设置激活的 Spring 配置文件

Spring Environment 有一个 API,但通常您会设置系统属性 (spring.profiles.active) 或操作系统环境变量 (SPRING_PROFILES_ACTIVE)。例如,使用 -D 参数启动您的应用程序(记住将其放在主类或 jar 存档之前):

$ java -jar -Dspring.profiles.active=production demo-0.0.1-SNAPSHOT.jar

在 Spring Boot 中,您还可以在 application.properties 中设置活动配置文件,例如

spring.profiles.active=生产

以这种方式设置的值会被 System 属性或环境变量设置替换,但不会被 SpringApplicationBuilder.profiles() 方法替换。因此,后一种 Java API 可用于扩充配置文件而无需更改默认值。

有关更多信息,请参阅第 25 章“Spring Boot 功能”部分中的配置文件。

回答by Achaius

My solution is to set the environment variable as spring.profiles.active=development. So that all applications running in that machine will refer the variable and start the application. The order in which spring loads a properties as follows

我的解决方案是将环境变量设置为spring.profiles.active=development. 这样在该机器上运行的所有应用程序都将引用该变量并启动应用程序。spring加载属性的顺序如下

application.properties
system properties
environment variable

回答by Achille_vanhoutte

If i run the command line : java -Dspring.profiles.active=development -jar yourApplication.jarfrom my webapplication directory it states that the path is incorrect. So i just defined the profile in manualy in the application.properties file like this :

如果我运行命令行:java -Dspring.profiles.active=development -jar yourApplication.jar从我的 webapplication 目录中,它指出路径不正确。所以我只是在 application.properties 文件中手动定义了配置文件,如下所示:

spring.profiles.active=mysql 

or

或者

spring.profiles.active=postgres

or

或者

spring.profiles.active=mongodb

回答by Lalit Jha

SPRING_PROFILES_ACTIVE is the environment variable to override/pick Spring profile

SPRING_PROFILES_ACTIVE 是覆盖/选择 Spring 配置文件的环境变量

回答by db80

If you are using docker to deploy the spring boot app, you can set the profileusing the flag e:

如果您使用 docker 部署 spring boot 应用程序,则可以使用标志e设置配置文件

docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t r.test.co/myapp:latest

docker run -e "SPRING_PROFILES_ACTIVE=prod" -p 8080:8080 -t r.test.co/myapp:latest

回答by Ajmal V Aliyar

I normally configure the applicationContext using Annotation based configurationrather than XML based configuration. Anyway, I believe both of them have the same priority.

我通常使用基于注释的配置而不是基于 XML 的配置来配置 applicationContext 。无论如何,我相信他们都有相同的优先级

*Answering your question, system variable has higher priority *

*回答你的问题,系统变量有更高的优先级*



Getting profile based beans from applicationContext

从 applicationContext 获取基于配置文件的 bean

  • Use @Profile on a Bean

  • 在 Bean 上使用 @Profile

@Component
@Profile("dev")
public class DatasourceConfigForDev

Now, the profile is dev

现在,个人资料是 dev

Note : if the Profile is given as @Profile("!dev")then the profile will exclude dev and be for all others.

注意:如果配置文件为 as @Profile("!dev")那么配置文件将排除 dev 并适用于所有其他人。

  • Use profiles attribute in XML

  • 在 XML 中使用配置文件属性

<beans profile="dev">
    <bean id="DatasourceConfigForDev" class="org.skoolguy.profiles.DatasourceConfigForDev"/>
</beans>


Set the value for profile:

设置配置文件的值:

  • Programmatically via WebApplicationInitializer interface

    In web applications, WebApplicationInitializer can be used to configure the ServletContext programmatically
  • 通过 WebApplicationInitializer 接口以编程方式

    在 Web 应用程序中,可以使用 WebApplicationInitializer 以编程方式配置 ServletContext
@Configuration
public class MyWebApplicationInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
            servletContext.setInitParameter("spring.profiles.active", "dev");
    }
}
  • Programmatically via ConfigurableEnvironment

    You can also set profiles directly on the environment:
  • 通过 ConfigurableEnvironment 以编程方式

    您还可以直接在环境上设置配置文件:
    @Autowired
    private ConfigurableEnvironment env;

    // ...

    env.setActiveProfiles("dev");
  • Context Parameter in web.xml

    profiles can be activated in the web.xml of the web application as well, using a context parameter:
  • web.xml 中的上下文参数

    配置文件也可以在 web 应用程序的 web.xml 中激活,使用上下文参数:
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/app-config.xml</param-value>
    </context-param>
    <context-param>
        <param-name>spring.profiles.active</param-name>
        <param-value>dev</param-value>
    </context-param>
  • JVM System Parameter

    The profile names passed as the parameter will be activated during application start-up:

    -Dspring.profiles.active=dev
    

    In IDEs, you can set the environment variables and values to use when an application runs. The following is the Run Configuration in Eclipse:

  • JVM系统参数

    作为参数传递的配置文件名称将在应用程序启动期间激活:

    -Dspring.profiles.active=dev
    

    在 IDE 中,您可以设置应用程序运行时要使用的环境变量和值。以下是Eclipse中的运行配置:

Eclipse Run Configuration - screenshot is unavailable

Eclipse 运行配置 - 屏幕截图不可用

  • Environment Variable

    to set via command line : export spring_profiles_active=dev

  • 环境变量

    通过命令行设置: export spring_profiles_active=dev

Any bean that does not specify a profile belongs to “default” profile.

任何未指定配置文件的 bean 都属于“默认”配置文件。



The priority order is :

优先顺序是:

  1. Context parameter in web.xml
  2. WebApplicationInitializer
  3. JVM System parameter
  4. Environment variable
  1. web.xml 中的上下文参数
  2. 网络应用初始化器
  3. JVM 系统参数
  4. 环境变量