java - 使用纯spring java配置访问环境属性

标签 java spring spring-mvc environment-variables

我正在使用 Spring 和纯 Java 配置(无 xml)编写一个 Web 应用程序。我想要一个解决方案来公开各种环境特定属性,具体取决于我的应用程序运行的位置(开发/测试/产品)。通过 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 配置访问环境变量的工作示例。

感谢您的帮助。

最佳答案

它现在尝试在属性文件中查找“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 环境。也许在设置 PropertyPlaceHolder bean 之前无法使用环境?

关于java - 使用纯spring java配置访问环境属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26236386/

相关文章:

java - Spring JPA 对存储库的数据扫描不起作用

java - Spring MVC 应用程序中的多个 ScriptTemplateViewResolvers

java - NameNotFoundException 在生产 JBoss 中对远程 EJB 执行 JNDI 查找(在本地工作)

java - @After each test, 删除所有用户 - JUnit

java - 如何从java中的wsdl文件生成soap请求xml

java - 如何动态选择RandomAccessFile?

hibernate - Hibernate 和 Spring 的配置问题

java - spring - 如何在 Spring XML 中设置 java.util.Locale 列表?

java - 如何控制同一个包上两个切入点的执行顺序

java - 使用 Spring 在后台运行进程的推荐方法是什么?