Java 将.yml文件中的所有spring引导属性设置为系统属性

Java 将.yml文件中的所有spring引导属性设置为系统属性,java,spring-boot,keystore,jvm-arguments,vmargs,Java,Spring Boot,Keystore,Jvm Arguments,Vmargs,我正在从事一个spring启动项目,在该项目中,为应用程序启动传递了许多VM参数,即证书位置、特定的配置文件类型(不是开发、qa、产品等)。 我正在将所有配置移动到default.yml文件中。 问题陈述default.yml中设置的属性仅可由spring上下文的环境接口访问,即org.springframework.core.env.env.environment,并且属性不会自动/默认设置为系统属性我正在通过列表器在系统中设置属性ServletContextListener方法中的conte

我正在从事一个spring启动项目,在该项目中,为应用程序启动传递了许多VM参数,即证书位置、特定的配置文件类型(不是开发、qa、产品等)。
我正在将所有配置移动到default.yml文件中。
问题陈述
default.yml中设置的属性仅可由spring上下文的环境接口访问,即org.springframework.core.env.env.environment,并且属性不会自动/默认设置为系统属性
我正在通过列表器在系统中设置属性ServletContextListener方法中的contextInitialized
但我不想使用环境.getProperty(key)按名称显式调用所有属性,相反,我希望spring上下文中可用的所有属性都应该循环/不循环设置为系统/环境变量。
预期解决方案
我正在寻找一种方法,在listner方法中,我可以使用该方法将default.yml文件中定义的所有属性设置为系统属性,而无需通过名称访问属性

下面是我目前正在采用的将从spring env/default.yml提取的活动概要文件设置为系统属性的方法。我不想从yml中获取活动配置文件或任何属性,但希望将.yml中可用的所有属性自动设置到系统中

Optional.ofNullable(springEnv.getActiveProfiles())
            .ifPresent(activeProfiles -> Stream.of(activeProfiles).findFirst().ifPresent(activeProfile -> {
                String currentProfile = System.getProperty("spring.profiles.active");
                currentProfile = StringUtils.isBlank(currentProfile) ? activeProfile : currentProfile;
                System.setProperty("spring.profiles.active", currentProfile);
            }));

您可以使用如下内容:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Properties;

@Component
public class EnvTest {

    final StandardEnvironment env;

    @Autowired
    public EnvTest(StandardEnvironment env) {
        this.env = env;
    }

    @PostConstruct
    public void setupProperties() {
        for (PropertySource<?> propertySource : env.getPropertySources()) {
            if (propertySource instanceof MapPropertySource) {
                final String propertySourceName = propertySource.getName();
                if (propertySourceName.startsWith("applicationConfig")) {
                    System.out.println("setting sysprops from " + propertySourceName);
                    final Properties sysProperties = System.getProperties();

                    MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
                    for (String key : mapPropertySource.getPropertyNames()) {
                        Object value = mapPropertySource.getProperty(key);
                        System.out.println(key + " -> " + value);
                        sysProperties.put(key, value);
                    }
                }
            }
        }
    }
}
import org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.core.env.MapPropertySource;
导入org.springframework.core.env.PropertySource;
导入org.springframework.core.env.StandardEnvironment;
导入org.springframework.stereotype.Component;
导入javax.annotation.PostConstruct;
导入java.util.Properties;
@组成部分
公营考试{
最终环境标准;
@自动连线
公共环境测试(标准环境){
this.env=env;
}
@施工后
公共属性(){
for(PropertySource PropertySource:env.getPropertySources()){
if(映射属性源的属性源实例){
最后一个字符串propertySourceName=propertySource.getName();
if(propertySourceName.startsWith(“applicationConfig”)){
System.out.println(“从“+propertySourceName”设置sysprops);
最终属性sysProperties=System.getProperties();
MapPropertySource MapPropertySource=(MapPropertySource)propertySource;
for(字符串键:mapPropertySource.getPropertyNames()){
对象值=mapPropertySource.getProperty(键);
System.out.println(键+“->”+值);
sysProperties.put(键、值);
}
}
}
}
}
}

当然,当stdout消息为您工作时,请删除它

您可能会使用以下内容:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Properties;

@Component
public class EnvTest {

    final StandardEnvironment env;

    @Autowired
    public EnvTest(StandardEnvironment env) {
        this.env = env;
    }

    @PostConstruct
    public void setupProperties() {
        for (PropertySource<?> propertySource : env.getPropertySources()) {
            if (propertySource instanceof MapPropertySource) {
                final String propertySourceName = propertySource.getName();
                if (propertySourceName.startsWith("applicationConfig")) {
                    System.out.println("setting sysprops from " + propertySourceName);
                    final Properties sysProperties = System.getProperties();

                    MapPropertySource mapPropertySource = (MapPropertySource) propertySource;
                    for (String key : mapPropertySource.getPropertyNames()) {
                        Object value = mapPropertySource.getProperty(key);
                        System.out.println(key + " -> " + value);
                        sysProperties.put(key, value);
                    }
                }
            }
        }
    }
}
import org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.core.env.MapPropertySource;
导入org.springframework.core.env.PropertySource;
导入org.springframework.core.env.StandardEnvironment;
导入org.springframework.stereotype.Component;
导入javax.annotation.PostConstruct;
导入java.util.Properties;
@组成部分
公营考试{
最终环境标准;
@自动连线
公共环境测试(标准环境){
this.env=env;
}
@施工后
公共属性(){
for(PropertySource PropertySource:env.getPropertySources()){
if(映射属性源的属性源实例){
最后一个字符串propertySourceName=propertySource.getName();
if(propertySourceName.startsWith(“applicationConfig”)){
System.out.println(“从“+propertySourceName”设置sysprops);
最终属性sysProperties=System.getProperties();
MapPropertySource MapPropertySource=(MapPropertySource)propertySource;
for(字符串键:mapPropertySource.getPropertyNames()){
对象值=mapPropertySource.getProperty(键);
System.out.println(键+“->”+值);
sysProperties.put(键、值);
}
}
}
}
}
}

当然,当stdout消息为您服务时,请删除它

嗨,迈希,谢谢您的回答。我最后也做了类似的事情:)嗨,迈希,谢谢你的回答。我最后做了类似的事情:)