【问题标题】:How to read all properties values from the source properties file in Spring-cloud-config client如何从 Spring-cloud-config 客户端中的源属性文件中读取所有属性值
【发布时间】:2016-10-28 08:50:42
【问题描述】:

我有这个 spring-cloud-config 客户端类,我可以使用 @Value 注释访问各个属性就好了。但是,我很想知道如何从属性文件中读取所有属性值,而不将每个属性的键绑定到 @Value 注释。基本上这个想法是我想从属性文件中读取所有属性值,甚至不知道文件中定义的属性的任何内容。知道我该怎么做吗?

客户端类

@EnableAutoConfiguration                                                                       
@ComponentScan                                       
@RestController             
@RefreshScope                                           
public class ConfigDemoClientApplication  
{             
    @Value("${special}")            
    String special;

    @RequestMapping("/restaurant")
    public String hello()
    {
        return "Hello " + special;
    }

    public static void main(String[] args) {
        SpringApplication.run(ConfigDemoClientApplication.class, args);
    }
}

示例属性文件

special: bargain!                                                                    
amount: 200                                                                           
city: New York

在本例中,我想读取所有 3 个属性,而不为我的班级中的每个属性定义 @Value 注释。这可能吗?

感谢您的帮助。

【问题讨论】:

    标签: spring spring-mvc spring-boot spring-cloud-config


    【解决方案1】:

    我刚刚解决了创建这个 applicationProps bean 的问题,它是一个包含应用程序所有属性的 java.util.Properties 对象。

    唯一需要的是一个自动装配的环境对象。

    代码如下:

        @Autowired
        Environment env;
    
        //Load all the properties of the server and put them into a java Properties obj
        @Bean(name = "applicationProps")
        public Properties applicationProperties() {
            final Properties properties = new Properties();
            for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
                PropertySource propertySource = (PropertySource) it.next();
                if (propertySource instanceof PropertiesPropertySource) {
                    log.info("Adding all properties contained in " + propertySource.getName());
                    properties.putAll(((MapPropertySource) propertySource).getSource());
                }
                if (propertySource instanceof  CompositePropertySource){
                    properties.putAll(getPropertiesInCompositePropertySource((CompositePropertySource) propertySource));
                }
            }
            return properties;
        }
    
        private Properties getPropertiesInCompositePropertySource(CompositePropertySource compositePropertySource){
            final Properties properties = new Properties();
            compositePropertySource.getPropertySources().forEach(propertySource -> {
                if (propertySource instanceof MapPropertySource) {
                    log.info("Adding all properties contained in " + propertySource.getName());
                    properties.putAll(((MapPropertySource) propertySource).getSource());
                }
                if (propertySource instanceof CompositePropertySource)
                    properties.putAll(getPropertiesInCompositePropertySource((CompositePropertySource) propertySource));
            });
            return properties;
        }
    
        @Autowired
        @Qualifier("applicationProps")
        Properties applicationProperties;
    

    需要 getPropertiesInCompositePropertySource 方法中的递归步骤,因为从配置服务器获取的属性递归嵌套在 CompositePropertySource 中

    希望对你有帮助

    问候

    【讨论】:

    • @jake 请将其标记为答案。完美总结需要什么
    【解决方案2】:

    试试这个:它都是 Spring,你可以将它与 PostConstruct 方法一起使用

    Map<String,String> someMap = new HashMap<>();
    Resource resource = new ClassPathResource("some.properties");
    Properties props = PropertiesLoaderUtils.loadProperties(resource);
    for(Object key : props.keySet()) {
         someMap.put(key.toString(),props.getProperty(key.toString()));  
    }
    

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 2017-01-21
      • 2021-04-13
      • 1970-01-01
      • 2018-07-29
      • 2021-03-22
      • 2015-08-28
      • 2019-08-20
      • 2019-07-09
      相关资源
      最近更新 更多