【问题标题】:Java Spring Profiles not working in LinuxJava Spring 配置文件在 Linux 中不起作用
【发布时间】:2015-11-24 18:12:02
【问题描述】:

我有一个在 Windows 上开发的 Web 应用程序,然后将其部署到 Linux(暂存和生产)。

我为每个环境创建了 3 个 .properties 文件:

  • application-dev.properties
  • application-staging.properties
  • application-prod.properties

我决定实施以下解决方案 - 在每台机器上创建具有相关值(dev / staging / prod)的环境变量,并据此加载 corect .properties 文件。

该解决方案在 Windows 上完美运行,但我无法让它在 Linux 上以同样的方式运行。

这是我的代码:

Web.xml

<context-param>

    <param-name>contextInitializerClasses</param-name>

    <param-value>com.app.server.configuration.ConfigurableApplicationContextInitializer</param-value>

</context-param>

ConfigurableApplicationContextInitializer 类:

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;


public class ConfigurableApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext context) {
        String APP_ENV = System.getenv("APP_ENV");
        context.getEnvironment().setActiveProfiles(APP_ENV);
        System.setProperty("spring.profiles.active", APP_ENV);

    }
}

ContextsConfiguration 类:

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;


@Configuration
@PropertySource("classpath:application-${spring.profiles.active}.properties")
public class ContextsConfiguration {

    @Bean
    public static PropertySourcesPlaceholderConfigurer configurer() {
            return new PropertySourcesPlaceholderConfigurer();
    } 


    @Value("${FTPport}")
    public String FTPport;
    @Value("${FTPserver}")

在 Linux 中,我已经在数百万个地方定义了这个变量 (APP_ENV)。在 .environment 文件中,在 .bash 文件中,在 setenv.sh 文件中。同样当我做 printenv - 我在那里看到它。

我尝试创建简单的 java 类 - main 打印 System.getenv("APP_ENV") 的值和正在打印的 "staging" 值。

但在我的应用程序中,我总是看到 - dev 而不是 staging。

我看到暂存的唯一方法是将“硬编码”活动配置文件添加到 web.xml

<context-param>  
    <param-name>spring.profiles.active</param-name>  
    <param-value>dev</param-value>  
</context-param>

但我真的不想这样工作,我希望它能够被自动和动态地识别。

请帮忙:)

【问题讨论】:

    标签: java linux spring environment-variables spring-profiles


    【解决方案1】:

    其中一个原因可能是您的 tomcat 在不同的用户下运行,因此您可能需要为运行 tomcat 的 那个 用户设置环境变量。

    也可以在$CATALINA_BASE/bin/setenv.sh文件中设置tomcat所需的环境变量

    【讨论】:

    • 嘿 Zilvinas - 感谢您的回答。我从我的用户那里运行一切——我也是那个安装了 Tomcat 的人。该值存在于 setenv.sh
    • 这就是我在 setenv.sh 中的内容: export JAVA_OPTS="-Dfile.encoding=UTF-8 -Xms512m -Xmx4096m -XX:PermSize=128m -XX:MaxPermSize=512m" export APP_ENV = “分期”也许这是不正确的方式?
    • @Vika 我认为应该这样做
    【解决方案2】:

    显然 setenv.sh 的正确位置是:usr/share/tomcat7/bin 我有一个带有“dev”值的旧文件:) 所以在修复这个文件之后 - 它完美地工作:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      • 1970-01-01
      • 2023-01-30
      • 2014-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      相关资源
      最近更新 更多