Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用纯spring java配置访问环境属性_Java_Spring_Spring Mvc_Environment Variables - Fatal编程技术网

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

使用纯spring java配置访问环境属性,java,spring,spring-mvc,environment-variables,Java,Spring,Spring Mvc,Environment Variables,我正在使用纯Java配置的Spring编写一个web应用程序,没有xml。我想要一个解决方案,根据我的应用程序运行dev/test/prod的位置,公开各种特定于环境的属性。使用Spring xml配置和通过xml的PropertyPlaceHolderConfiger,我可以做如下操作: <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholder

我正在使用纯Java配置的Spring编写一个web应用程序,没有xml。我想要一个解决方案,根据我的应用程序运行dev/test/prod的位置,公开各种特定于环境的属性。使用Spring xml配置和通过xml的PropertyPlaceHolderConfiger,我可以做如下操作:

<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>
我在Tomcat上将-Denv=localhost设置为VM参数。我还将其设置为终端输出localhost中mac ie echo$env的系统属性

我似乎不知道如何使用纯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;
}
使用Spring环境,如上面的代码所示。 @新变量的值{systemEnvironment['env']},并将其作为字符串访问 @新变量的值{systemProperties['env']},并将其作为字符串访问 @新变量的值${env},并将其作为字符串访问 以上所有参数都返回null。如果能在Spring中看到一个如何使用纯Java配置访问环境变量的工作示例,那就太好了


感谢您的帮助。

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

您没有在PropertyPlaceHolderConfigure上使用SystemPropertiesModelName方法,这将使@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代替自动连线环境。在设置PropertyPlaceHolder bean之前,可能无法使用环境?

是“env”属性位于shift-local.properties文件中,还是您只是尝试使用@Value${env}在字符串字段中获取“local”?我正在尝试在字符串字段中获取“local”。然后,该字符串将构成特定于环境的属性文件名的一部分。”env'是传入的变量名。谢谢您的建议。我已经查过了,似乎如果我根本没有设置它,Spring应该在检查我的应用程序属性文件后“回退”到系统属性:默认值是回退:如果无法解析具有指定属性的占位符,将尝试使用系统属性。你是对的:你的springEnv字段是静态的,在使用@AutowiredI时,哪一个不是最好的选择,因为我是在静态PropertyConfigure方法中访问该变量的,我已经读到该方法也应该是静态的。不过,让它们都不是静态的也没有帮助。它仍然是空的。如果您有一个使用您描述的方法的工作示例,那么可能还有其他问题。感谢您提供您的工作示例。今晚或明天早上,我将尝试实现类似的功能,如果我成功的话,我会接受你的答案。我已经验证了你提供的代码确实允许我访问环境变量。再次感谢。