[← Profiles](02-profiles.md) · [Index](../README.md) · [Why your profile file lost →](04-why-your-profile-file-lost.md) # 3. Seeing precedence instead of reasoning about it Endpoint: [`PrecedenceEndpoint`](../src/main/java/com/ankurm/profiles/web/PrecedenceEndpoint.java). Transcript: [`01-precedence.txt`](output/01-precedence.txt). Set `demo.greeting` from five places at once and ask which won: ``` $ DEMO_GREETING=from-environment-variable \ java -Ddemo.greeting=from-system-property \ -jar profiles-and-config-1.0.0.jar --spring.profiles.active=prod \ --demo.greeting=from-command-line-argument ``` ``` "effectiveValue": "from-command-line-argument", "activeProfiles": ["prod", "prod-db", "prod-metrics"], "holders": [ { "rank": 1, "value": "from-command-line-argument", "source": "commandLineArgs" }, { "rank": 2, "value": "from-system-property", "source": "systemProperties" }, { "rank": 3, "value": "from-environment-variable", "source": "systemEnvironment" }, { "rank": 4, "value": "from-application-prod-yaml", "origin": "application-prod.yaml - 4:13" }, { "rank": 5, "value": "from-application-yaml", "origin": "application.yaml - 21:13" } ], "shadowedCount": 4 ``` Five sources hold the property. Four of them lose. Each one that came from a file names its line. ## The whole implementation ```java for (ConfigurationPropertySource source : ConfigurationPropertySources.get(environment)) { ConfigurationProperty property = source.getConfigurationProperty(ConfigurationPropertyName.of(name)); if (property != null) { // rank = position, property.getValue(), property.getOrigin() } } ``` `ConfigurationPropertySources.get(...)` returns the sources in precedence order. Iterate, collect every hit, and the first is the winner. That is the entire diagnostic. ## Why this beats reading the list The documented order is correct but abstract. It does not tell you that a `DEMO_GREETING` left over from a shell three weeks ago is sitting at rank 3, and that is the actual question. ## Alternatives if you would rather not add an endpoint - Actuator's `/actuator/env` gives the same information with sanitisation, and `/actuator/env/{name}` narrows to one property. Prefer it in anything real. - `logging.level.org.springframework.boot.context.config=TRACE` logs which config data resources were loaded and in what order. - `--debug` does *not* show this. It prints the auto-configuration report.