有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在使用纯Java配置(无xml)的Spring编写一个web应用程序。我想要一个解决方案,根据应用程序的运行位置(dev/test/prod),公开各种特定于环境的属性。通过xml使用Spring 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>

在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;
}

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

我似乎不知道如何使用纯Java访问该环境变量。我尝试了以下方法:

  • 使用Spring环境,如上面的代码所示
  • @值(“#{systemEnvironment['env']}”),并将其作为字符串访问
  • @值(“#{systemProperties['env']}”),并将其作为字符串访问
  • @值(${env}”),并将其作为字符串访问

以上所有参数都返回null。如果能在Spring中看到一个如何使用纯Java配置访问环境变量的工作示例,那就太好了

谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    它现在尝试在属性文件中查找“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;
        }
    
    }
    

    }

    差异: 已使用属性资源占位符配置器,系统。使用getenv代替自动连线环境。也许在设置PropertyPlaceHolder bean之前不能使用环境