【问题标题】:Access environment property using pure spring java configuration使用纯spring java配置访问环境属性
【发布时间】:2014-12-01 21:39:36
【问题描述】:

我正在使用带有纯 Java 配置(无 xml)的 Spring 编写一个 Web 应用程序。我想要一个解决方案来根据我的应用程序运行的位置(dev/test/prod)公开各种特定于环境的属性。通过 xml 使用 Spring xml 配置和 PropertyPlaceholderConfigurer,我会做这样的事情:

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:shift.properties</value>
            <value>classpath:shift-${env}.properties</value>
        </list>
    </property>
</bean>

在 Java 配置中,我尝试做的基础如下:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.values.shift" })
public class WebConfig extends WebMvcConfigurerAdapter {

@Autowired
private static Environment springEnv;

@Bean
public static PropertyPlaceholderConfigurer propertyConfigurer() throws IOException {
    PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer();
    System.out.println("Environment:" + env.toString());
    props.setLocations(
            new Resource[] {
                    new ClassPathResource("shift.properties"), 
                    new ClassPathResource("shift-" + springEnv.getProperty("env") + ".properties")}
            );
    props.setIgnoreResourceNotFound(true);
    return props;
}

我将 -Denv=localhost 设置为 Tomcat 上的 VM 参数。我还将它设置为我的 Mac 上的系统属性(即终端输出 localhost 中的 echo $env)

我似乎无法弄清楚如何使用纯 Java 访问该环境变量。我尝试了以下方法:

  • 如上代码所示使用 Spring 环境。
  • @Value("#{ systemEnvironment['env'] }") 获取新变量并将其作为字符串访问
  • @Value("#{ systemProperties['env'] }") 用于新变量并将其作为字符串访问
  • @Value("${env}") 用于新变量并将其作为字符串访问

以上所有都返回null。很高兴看到如何在 Spring 中使用纯 Java 配置访问环境变量的工作示例。

感谢您的帮助。

【问题讨论】:

  • 是位于 shift-local.properties 文件中的 'env' 属性,或者您只是尝试使用 @Value("${env}") 在字符串字段中获取 'local'?
  • 我正在尝试在字符串字段中获取“本地”。然后该字符串将构成我的环境特定属性文件名的一部分。 'env' 是传入的变量名。

标签: java spring spring-mvc environment-variables


【解决方案1】:

它现在尝试在属性文件中查找“env”属性。

您错过了在 PropertyPlaceholderConfigurer 上使用 systemPropertiesModeName 方法,这将使您的 @Value("#{ systemProperties['env'] }") 有效。

//编辑:

不要在静态字段上使用@Autowired。

//编辑2:

这是我正在使用的:

@Configuration
public class PropertiesConfig {

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return PropertyLoadHelper.getPropertySourcesPlaceholderConfigurer();
}

public static class PropertyLoadHelper {
    public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
        properties.setLocations(new ClassPathResource[]{
                new ClassPathResource("config/app." + System.getenv("ENV") + "properties"),
                new ClassPathResource("config/local.app." + System.getenv("ENV") + "properties"),
                new ClassPathResource("config/application.properties")
        });
        properties.setBeanName("app");
        properties.setLocalOverride(true);
        properties.setIgnoreResourceNotFound(true);
        return properties;
    }


    public static Properties loadProperties(String propertiesPath) {
        PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
        propertiesFactoryBean.setLocation(new ClassPathResource(propertiesPath));
        Properties properties = null;
        try {
            propertiesFactoryBean.afterPropertiesSet();
            properties = propertiesFactoryBean.getObject();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

}

}

区别: 使用 PropertySourcesPlaceholderConfigurer,使用 System.getenv 代替 Autowired Environment。设置PropertyPlaceHolder bean之前可能无法使用Environment?

【讨论】:

  • 感谢您的建议。我已经查过了,如果我根本没有设置它,在检查我的应用程序属性文件后,Spring 应该“回退”到系统属性:“默认为“回退”:如果无法解析占位符使用指定的属性,系统属性将被尝试。"
  • 你是对的 :) 你的 springEnv 字段是静态的,这不是使用 @Autowired 时的最佳选择
  • 我将该变量设置为静态,因为我在静态 propertyConfigurer 方法中访问它(我已经阅读过它也应该是静态的)。但是,使它们都不是静态的并没有帮助。它仍然为空。如果您有一个使用您描述的方法的工作示例,则可能还有其他事情发生。
  • 感谢您提供工作示例。如果我成功了,我会尝试在今晚或明天早上实施类似的事情并接受您的回答。
  • 我已经验证您提供的代码确实让我可以访问环境变量。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 2017-05-06
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多