将application.properties文件加载到Spring Boot中的java.util.Properties

时间:2016-09-19 05:28:58

标签: spring-boot

我阅读了externalized configuration的Spring Boot文档,我发现它会自动加载src / main / resources / application.properties文件,然后可以使用注释将其连接到bean属性。

但是我希望有一个通用的PropertyHelper类,可用于使用application.properties中的属性构建java.util.Properties。可以这样做吗?

我们目前正在手动实现此目标,如下所示:

public class PropertyHelper {

    private static Properties loadProperties() {
        try {

             String propsName = "application.properties";
             InputStream propsStream = PropertyHelper.class
                    .getClassLoader().getResourceAsStream(propsName);
            if (propsStream == null) {
                throw new IOException("Could not read config properties");
            }

            Properties props = new Properties();
            props.load(propsStream);

3 个答案:

答案 0 :(得分:1)

你可以围绕环境创建一个Wrapper,它会返回一个现成的PropertySource

您可以这样使用它:

@PropertySource(name="myName", value="classpath:/myName.properties")
public class YourService {

    @Autowired
    private CustomMapProperties customMapProperties;
    ...
    MapPropertySource mapPropertySource = customMapProperties.getMapProperties("myName");
    for(String key: mapPropertySource.getSource().keySet()){
        System.out.println(mapPropertySource.getProperty(key));
    }

CustomMapProperties注入Environment并返回请求&加载属性文件基于其名称:

@Component
public class CustomMapProperties {

    @Autowired
    private Environment env;

    public MapPropertySource getMapProperties(String name) {
        for (Iterator<?> it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext();) {
            Object propertySource = it.next();
            if (propertySource instanceof MapPropertySource
                    && ((MapPropertySource) propertySource).getName().equals(name)) {
                return (MapPropertySource) propertySource;
            }
        }
        return null;
    }
}

答案 1 :(得分:0)

以下是我如何从Spring的环境中派生一个Properties对象。我查找java.util.Properties类型的属性源,在我的例子中,它将为我提供系统属性和应用程序属性。

@Resource
private Environment environment;


@Bean
public Properties properties() {
    Properties properties = new Properties();

    for (PropertySource<?> source : ((ConfigurableEnvironment) environment).getPropertySources()) {
        if (source.getSource() instanceof Properties) {
            log.info("Loading properties from property source " + source.getName());
            Properties props = (Properties) source.getSource();
            properties.putAll(props);
        }
    }

    return properties;
}

但请注意,订单可能很重要;您可能希望在其他属性之后加载系统属性,因此它们可以覆盖应用程序属性。在这种情况下,使用source.getName()添加更多控制代码以选择“systemProperties”:

@Bean
public Properties properties() {
    Properties properties = new Properties();

    Properties systemProperties = null;

    for (PropertySource<?> source : ((ConfigurableEnvironment) environment).getPropertySources()) {
        if (source.getSource() instanceof Properties) {
            if ("systemProperties".equalsIgnoreCase(source.getName())) {
                log.info("Found system properties from property source " + source.getName());
                systemProperties = (Properties) source.getSource();
            } else {
                log.info("Loading properties from property source " + source.getName());
                Properties props = (Properties) source.getSource();
                properties.putAll(props);
            }
        }
    }

    // Load this at the end so they can override application properties.
    if (systemProperties != null) {
        log.info("Loading system properties from property source.");
        properties.putAll(systemProperties);
    }

    return properties;
}

答案 2 :(得分:0)

在构造函数中注入应用程序上下文参数,并将其中继到java.util.properties中:

import java.util.Properties;
import org.springframework.boot.ApplicationArguments;

public MyComponentClass(ApplicationArguments arguments) {

  Properties properties = getProperties(arguments);
}

private static Properties getProperties(ApplicationArguments arguments) {

  Properties properties = new Properties();

  for (String argementName : arguments.getOptionNames()) {

    List<String> argumentValues = arguments.getOptionValues(argementName);

    if (argumentValues.size() > 0) {
      properties.put(argementName, argumentValues.get(0));
    }
  }

  return properties;
}