spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Add Property origin information to environment

Open ddcruver opened this issue 4 years ago • 2 comments

I already posted an feature request to Spring Framework but it might involve more trouble than it is worth to them. https://github.com/spring-projects/spring-framework/issues/25157

Regardless short of that becoming available could Spring Boot implement an Environment interface that exposes the Origin and OriginLookup functionality.

For example instead of this:

private String getPropertySourceName(String property, Environment environment) {
    String propertySourceName = "";
     if (environment instanceof AbstractEnvironment) {
          MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
          for (PropertySource<?> propertySource : propertySources) {
               Object propertyValue = propertySource.getProperty(property);
               if (propertyValue != null) {
                    if (propertySource instanceof OriginLookup) {
                         Origin origin = ((OriginLookup) propertySource).getOrigin(property);
                         propertySourceName = origin.toString();
                    } 
                    else {
                         propertySourceName = propertySource.getName();
                    }
                    break;
               }
          }
     }
     return propertySourceName;
}

I would like to do this:

private String getPropertySourceName(String property, Environment environment) {
     if (environment instanceof OriginTrackedEnvironment) {
          OriginTrackedEnvironment originTrackedEnvironment = ((OriginTrackedEnvironment) environment);
          return originTrackedEnvironment.getOrigin(property).toString();
     } 
     else {
          return "Unknown Origin";
     }
}

ddcruver avatar May 28 '20 16:05 ddcruver