List of Final List of Properties - Spring Cloud Server

Spring Cloud Server accepts multiple profiles and returns properties for all profiles when accessing the application endpoint / env. The response lists the properties specific to each profile. If the same property is present in two different property files, the one that is defined last takes precedence. Is there a way to get the final list of property keys and values ​​that will be used by the application?

+8
spring-boot spring-cloud-netflix spring-cloud-config
source share
4 answers

For Cloud Config Client Application

I tried different ways and found the following (by accident):

GET /env/.* returns a complete list of configuration properties

For Cloud Config Server Application

It turns out that this is already implemented, but poorly documented. All you need to do is request json , yml or properties according to patterns:

 /{application}-{profile}.{ext} /{label}/{application}-{profile}.{ext} 
+4
source share

This is apparently a deliberate limitation of the Spring Framework.

See here

You can crack it and insert the PropertySources interface, and then iterate over all the individual PropertySource objects, but you need to know what properties you are looking for.

+3
source share
 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; } } 

Sorry ... this is my first answer to the question here. I created an account specifically to answer this question because I ran into this while researching the same problem. I found a solution that worked for me and decided to share it.

Here is my explanation of what has been done:

  • I initialize a new Properties object (maybe a HashMap or something else you want)

  • I was looking for a property source for "bootstrapProperties", which is a CompositePropertySource object. This property source contains all loaded application properties.

  • I look through all the property names returned by the getPropertyNames method of the CompositePropertySource object and create a new property record.

  • I am returning a property object.

+3
source share
  1. External setting

Spring Download allows you to externalize your configuration so that you can work with the same application code in different environments. You can use property files, YAML files, environment variables, and command line arguments to externalize the configuration. Property values ​​can be entered directly into your beans using the @Value annotation, accessed via abstraction of Spring s environment, or attached to structured objects via @ConfigurationProperties.

Spring Boot uses a very specific PropertySource order, which is designed to intelligently override values. Properties are considered in the following order:

  • General properties of Devtools settings in your home directory (~ / .spring-boot-devtools.properties when devtools is active).
  • @TestPropertySource annotations for your tests.
  • @ SpringBootTest # attribute attribute attribute for your tests.
  • Command line arguments.
  • Properties from SPRING_APPLICATION_JSON (inline JSON, inline in an environment variable or system property)
  • ServletConfig initialization options.
  • Parameters init ServletContext.
  • JNDI attributes from java: comp / env.
  • Java System Properties (System.getProperties ()).
  • OS environment variables.
  • A RandomValuePropertySource object that only has properties in random order. *.
  • Profile-dependent application properties outside your packaged jar (application- {profile} .properties and YAML variants)
  • Profile-dependent application properties packaged inside your jar (application- {profile} .properties and YAML variants)
  • Application properties outside your packaged jar (application.properties and YAML options).
  • Application properties packaged in your jar (application.properties and YAML options).
  • @PropertySource for your @Configuration classes.
  • Default properties (set using SpringApplication.setDefaultProperties).

The program below prints properties from the spring boot environment.

 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); } } } } } } } } 
+2
source share

All Articles