当我访问应用程序的/ env端点时,Spring Cloud Config Server接受多个配置文件并返回所有配置文件的属性。响应列出特定于每个配置文件的属性。如果在两个不同的属性文件中存在相同的属性,则最后定义的属性优先。有没有办法获得应用程序将使用的属性键和值的最终列表?列出属性的最终列表 - Spring Cloud Config Server
回答
对于云配置客户端应用程序
我尝试不同的方法,发现如下(不小心):配置属性
对于云配置服务器应用程序的
GET /env/.*
回报完整列表
事实证明,这已经实施,但没有文件记录湖所有你需要的是根据图案要求json
,yml
或properties
:
/{application}-{profile}.{ext}
/{label}/{application}-{profile}.{ext}
import java.util.properties;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
public class MyClass {
@Autowired
private Environment env;
Properties getProperties() {
Properties props = new Properties();
CompositePropertySource bootstrapProperties = (CompositePropertySource) ((AbstractEnvironment) env).getPropertySources().get("bootstrapProperties");
for (String propertyName : bootstrapProperties.getPropertyNames()) {
props.put(propertyName, bootstrapProperties.getProperty(propertyName));
}
return props;
}
}
对不起...这是我第一次来回答问题。我创建了一个专门针对 回答这个问题的帐号,因为我在研究相同问题时遇到了这个问题。我发现了一个为我工作并决定分享它的 解决方案。
这里不用我的做了什么解释:
我初始化一个新的“属性”对象(可能是一个HashMap或其他任何你想要的)
我查找的属性源这是一个CompositePropertySource对象的“bootstrapProperties”。 此属性源包含加载的所有应用程序属性。
我循环遍历CompositePropertySource对象 上的“getPropertyNames”方法返回的所有属性名称,并创建一个新的属性条目。
我返回属性对象。
请注意:代码只有答案是不鼓励的。它总是更好地添加一些层次的解释。 – GhostCat
我没有看到所有的属性。例如'bootstrap.yml'中的'logging.config'。但是,我看到它使用执行器。 –
谢谢@Todd Jones! –
- Externalized Configuration
- 在你的home目录Devtools全局设置属性(〜/ .spring启动-devtools.properties时devtools有效)。
- @TestPropertySource在您的测试上的注释。
- @ SpringBootTest#properties注释属性在您的测试。
- 命令行参数。
- 来自SPRING_APPLICATION_JSON的属性(内嵌在环境变量或系统属性中的JSON)
- ServletConfig init参数。
- ServletContext初始参数。
- 来自java:comp/env的JNDI属性。
- Java系统属性(System.getProperties())。
- OS环境变量。
- 仅具有随机属性的RandomValuePropertySource。*。
- 打包的罐子以外特定资料的应用程序的属性(应用 - {轮廓}的.properties和YAML变体)包装您的罐内
- 特定资料的应用程序的属性(应用 - {轮廓}的.properties和YAML变体)
- 打包jar(application.properties和YAML变体)之外的应用程序属性。
- 打包在jar中的应用程序属性(application.properties和YAML变体)。
- @PropertySource @Configuration类的注释。
- 默认属性(使用SpringApplication.setDefaultProperties指定)。
春季启动,您可以外部化配置,所以你可以在不同环境中的同一个应用程序代码的工作。您可以使用属性文件,YAML文件,环境变量和命令行参数来外部化配置。属性值可以使用@Value注释直接注入到bean中,可以通过Spring的Environment抽象来访问,也可以通过@ConfigurationProperties绑定到结构化对象。
Spring Boot使用非常特殊的PropertySource命令,该命令旨在允许明智地重写值。 属性按以下顺序考虑:
下面的程序从spring引导环境打印属性。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.StandardServletEnvironment;
@Component
public class EnvironmentLogger extends ApplicationObjectSupport {
@Override
protected void initApplicationContext(ApplicationContext context) throws BeansException {
Environment environment = context.getEnvironment();
String[] profiles = environment.getActiveProfiles();
if(profiles != null && profiles.length > 0) {
for (String profile : profiles) {
System.out.print(profile);
}
} else {
System.out.println("Setting default profile");
}
//Print the profile properties
if(environment != null && environment instanceof StandardServletEnvironment) {
StandardServletEnvironment env = (StandardServletEnvironment)environment;
MutablePropertySources mutablePropertySources = env.getPropertySources();
if(mutablePropertySources != null) {
for (PropertySource<?> propertySource : mutablePropertySources) {
if(propertySource instanceof MapPropertySource) {
MapPropertySource mapPropertySource = (MapPropertySource)propertySource;
if(mapPropertySource.getPropertyNames() != null) {
System.out.println(propertySource.getName());
String[] propertyNames = mapPropertySource.getPropertyNames();
for (String propertyName : propertyNames) {
Object val = mapPropertySource.getProperty(propertyName);
System.out.print(propertyName);
System.out.print(" = " + val);
}
}
}
}
}
}
}
}
谢谢Sudhakar。我会试试这个。 –
- 1. 我可以从Spring Cloud Config Server获得单个属性值吗?
- 2. Library Library可以从Spring Cloud Config Server读取属性吗?
- 3. 如何使用Spring Boot(1.5.3.RELEASE)和Spring Cloud Config(1.3.0.RELEASE)编写Spring Cloud Config Server?
- 4. spring cloud config searchPaths
- 5. 问题spring-cloud-config和spring-cloud-bus
- 6. Spring Cloud Config中的加密
- 7. Spring Cloud Config服务器问题
- 8. 外部化Spring Cloud数据流配置 - Spring Cloud Config服务器
- 9. Spring Cloud Config如何理解外部属性?
- 10. Spring Cloud Config - 加密密码
- 11. spring cloud config如何使用本地属性覆盖远程属性
- 12. 使用Spring-Cloud-Connectors配置Spring Boot以使用PWS Config-Server
- 13. Spring Cloud Config定制环境存储库
- 14. 反序列化最终名单属性
- 15. Hoverfly simulationMode与单元测试中的Spring Cloud Config Server相冲突
- 16. Spring Cloud Config(Vault后端)过早提早
- 17. Spring Cloud Config Server无法在启动时找到PropertySource
- 18. spring cloud config ssh连接失败
- 19. 最终属性中的Spring属性注入@Value - Java
- 20. Spring Cloud - Config Client减慢度量/健康
- 21. 最终属性?
- 22. 如何使用Spring对bean的列表属性做出贡献?
- 23. Spring Cloud Config Client - Angel.SR4 vs Boot 1.3.1?
- 24. Spring Cloud config feign fallback(CircuitBreaker)规则
- 25. Spring Cloud Config服务器 - Logback错误
- 26. 使用spring-cloud-dataflow时spring-cloud-stream应用中的属性不起作用
- 27. Maven属性列表
- 28. 从Spring MVC列表中填充HTML排序列表属性
- 29. 如何使用spring-cloud-config-client配置自定义RestTemplate?
- 30. 过滤列表的列表python,如何创建最终列表?
我非常怀疑有这样的事情。没有遇到任何。但是,获得这个开箱即用,并得到这个问题的答案会很棒。 –
感谢您的更新,@GrinishNepal! –