简体   繁体   English

从 PropertySource Springboot 迭代属性

[英]Iterate over properties from a PropertySource Springboot

I'm trying to expose all properties from some propertysources through an endpoint in my application.我正在尝试通过我的应用程序中的端点公开某些属性源中的所有属性。 I can use MutablePropertySources to get all the propertySources available in the Environment, but my question is how to iterate over a propertySource?我可以使用 MutablePropertySources 来获取环境中可用的所有 propertySources,但我的问题是如何迭代 propertySource?

I think I have found the solution, you basically need to use EnumrablePropertySource instead of a propertySource and use that to get an Array of names(keys) then use that to get the value by calling env.getProperty(name)我想我已经找到了解决方案,您基本上需要使用 EnumrablePropertySource 而不是 propertySource 并使用它来获取名称(键)数组,然后使用它通过调用 env.getProperty(name) 来获取值

The code would be somethin like this: EnumerablePropertySource propertySource=(EnumerablePropertySource) configurableEnvironment.getPropertySources().get(SOME_PROPERTYSOURCE_NAME);代码将是这样的: EnumerablePropertySource propertySource=(EnumerablePropertySource)izableEnvironment.getPropertySources().get(SOME_PROPERTYSOURCE_NAME); Arrays.stream(propertySource.getPropertyNames()).forEach(s->System.out.println(s+"="+configurableEnvironment.getProperty(s))); Arrays.stream(propertySource.getPropertyNames()).forEach(s->System.out.println(s+"="+configurableEnvironment.getProperty(s)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM