2016-09-19 67 views
0

我通过Spring Boot文档了解了externalized configuration,我发现它会自动加载src/main/resources/application.properties文件,然后可以将它连接到使用注释的bean属性。将application.properties文件加载到Spring引导中的java.util.Properties中

但是我想要一个通用的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); 
+0

之前'添加斜线application.properties' – Jens

+3

或者你可以自动装配环境,这是一个包含所有值的属性型豆从文件 – rorschach

+1

使用'Environment'您可以获取属性,但它没有所有属性的列表。你只能使用'env.getProperty(“propertyName”)'获得属性 –

回答

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; 
    } 
} 
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; 
} 
相关问题