Three new article modules: configuration binding, profiles and config data, Spring AOP
configuration-properties/ @ConfigurationProperties vs @Value on Spring Boot 4.1.1. The relaxed-binding matrix is generated by binding each spelling rather than transcribed, and re-checked against real processes -- the in-process probe was wrong twice before it was right. Records the three findings that came out of it: @Value does get relaxed resolution inside Spring Boot (Boot attaches ConfigurationPropertySources), the configuration processor silently stops generating metadata on JDK 23+ when declared as a plain dependency, and @Valid is not what makes nested constraints run. profiles-and-config/ Precedence, profiles, spring.config.import and config trees. /precedence reports every source holding a property in rank order with file and line, which turns "my profile file had no effect" into a two-line answer. Also pins the counterintuitive one: an imported file outranks the file that imported it. spring-aop/ Designators, proxy types, and aspects that do not fire. One advice per supported designator so the reference table is generated from real matches; all fourteen unsupported designators fed to the parser. Two corrections to the reference documentation: unsupported designators throw UnsupportedPointcutPrimitiveException (extends RuntimeException, not IllegalArgumentException), and spring-boot-starter-aop was renamed to spring-boot-starter-aspectj in Boot 4. 19 contract tests across the three modules, 15 captured transcripts, all regenerated by scripts/run-all.sh. Verified on Spring Boot 4.1.1, Spring Framework 7.0.9, JDK 25.0.4.1. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Gip4srpzMwjgoba6uEfbr5
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.ankurm.profiles;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* Companion application for the ankurm.com article
|
||||
* "Spring Boot Profiles Done Right: Config Import, Config Trees and Kubernetes ConfigMaps".
|
||||
*
|
||||
* <p>Every scenario in {@code scripts/} starts this same application with a different
|
||||
* combination of profiles, imported locations and environment variables, and asks it one
|
||||
* question: which source won, and which sources were present and lost.
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class ProfilesApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(ProfilesApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.ankurm.profiles.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
|
||||
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.EnumerablePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* The endpoint the whole article is built on: for one property, every source that holds a
|
||||
* value for it, in precedence order, with the winner first.
|
||||
*
|
||||
* <p>"I set it in {@code application-prod.yaml} and it had no effect" is not a mystery once
|
||||
* you can see that four sources hold the property and yours is third. Spring Boot knows the
|
||||
* answer; it just never volunteers it.
|
||||
*
|
||||
* <p>Documented in {@code docs/03-seeing-precedence.md}. Delete it before shipping: it will
|
||||
* print whatever a mounted secret contains.
|
||||
*/
|
||||
@RestController
|
||||
public class PrecedenceEndpoint {
|
||||
|
||||
private final ConfigurableEnvironment environment;
|
||||
|
||||
public PrecedenceEndpoint(ConfigurableEnvironment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/** Every source holding {@code name}, highest precedence first. */
|
||||
@GetMapping("/precedence")
|
||||
public Map<String, Object> precedence(
|
||||
@RequestParam(defaultValue = "demo.greeting") String name) {
|
||||
|
||||
ConfigurationPropertyName propertyName = ConfigurationPropertyName.of(name);
|
||||
List<Map<String, Object>> holders = new ArrayList<>();
|
||||
|
||||
for (ConfigurationPropertySource source : ConfigurationPropertySources.get(environment)) {
|
||||
var property = source.getConfigurationProperty(propertyName);
|
||||
if (property == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("rank", holders.size() + 1);
|
||||
row.put("source", shortName(source.getUnderlyingSource()));
|
||||
row.put("value", String.valueOf(property.getValue()));
|
||||
row.put("origin", String.valueOf(property.getOrigin()));
|
||||
holders.add(row);
|
||||
}
|
||||
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("property", name);
|
||||
result.put("effectiveValue", environment.getProperty(name));
|
||||
result.put("activeProfiles", List.of(environment.getActiveProfiles()));
|
||||
result.put("holders", holders);
|
||||
result.put("shadowedCount", Math.max(0, holders.size() - 1));
|
||||
return result;
|
||||
}
|
||||
|
||||
/** The environment's property sources in order, so the article can show the real stack. */
|
||||
@GetMapping("/sources")
|
||||
public List<Map<String, Object>> sources() {
|
||||
List<Map<String, Object>> rows = new ArrayList<>();
|
||||
int rank = 1;
|
||||
for (PropertySource<?> source : environment.getPropertySources()) {
|
||||
Map<String, Object> row = new LinkedHashMap<>();
|
||||
row.put("rank", rank++);
|
||||
row.put("name", source.getName());
|
||||
row.put("type", source.getClass().getSimpleName());
|
||||
if (source instanceof EnumerablePropertySource<?> enumerable) {
|
||||
row.put("propertyCount", enumerable.getPropertyNames().length);
|
||||
}
|
||||
rows.add(row);
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private String shortName(Object underlying) {
|
||||
String text = String.valueOf(underlying);
|
||||
return text.length() > 150 ? text.substring(0, 150) + "..." : text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
# spring.profiles.active cannot be set from a document that is itself profile-specific.
|
||||
# Spring Boot refuses this rather than silently half-applying it. The exact exception is
|
||||
# captured in docs/output/05-invalid-activation.txt.
|
||||
demo:
|
||||
greeting: from-badactivation
|
||||
---
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: staging
|
||||
profiles:
|
||||
active: sneaky
|
||||
@@ -0,0 +1,11 @@
|
||||
# Demonstrates spring.config.import ordering.
|
||||
#
|
||||
# The imported document is processed as though it appeared immediately AFTER this one, which
|
||||
# means the importing file wins on any key both of them set. That is the opposite of the
|
||||
# intuition most people bring from #include, and it is the subject of docs/05-config-import.md.
|
||||
spring:
|
||||
config:
|
||||
import: "optional:classpath:/imported.yaml"
|
||||
|
||||
demo:
|
||||
greeting: from-application-import-yaml
|
||||
@@ -0,0 +1,20 @@
|
||||
# One file, three documents, activated by condition rather than by filename.
|
||||
#
|
||||
# spring.config.activate.on-profile is the mechanism behind profile-specific behaviour when
|
||||
# you would rather keep everything in one file. Later documents win over earlier ones.
|
||||
demo:
|
||||
greeting: from-multidoc-default-document
|
||||
---
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: staging
|
||||
demo:
|
||||
greeting: from-multidoc-staging-document
|
||||
---
|
||||
spring:
|
||||
config:
|
||||
activate:
|
||||
on-profile: prod
|
||||
demo:
|
||||
greeting: from-multidoc-prod-document
|
||||
@@ -0,0 +1,3 @@
|
||||
# Activated as part of the "prod" profile group declared in application.yaml.
|
||||
demo:
|
||||
pool-size: 40
|
||||
@@ -0,0 +1,3 @@
|
||||
# The second member of the "prod" group.
|
||||
demo:
|
||||
metrics-enabled: true
|
||||
@@ -0,0 +1,5 @@
|
||||
# Profile-specific configuration. This file always beats application.yaml -- and still loses
|
||||
# to an environment variable, which is the point of docs/04-why-your-profile-file-lost.md.
|
||||
demo:
|
||||
greeting: from-application-prod-yaml
|
||||
datasource-url: jdbc:postgresql://prod-db:5432/orders
|
||||
@@ -0,0 +1,22 @@
|
||||
spring:
|
||||
application:
|
||||
name: profiles-and-config
|
||||
profiles:
|
||||
# A profile group: activating "prod" activates all three. Groups are resolved before
|
||||
# config data is processed, which is why a group can be declared here and still affect
|
||||
# which application-<profile>.yaml files are loaded.
|
||||
group:
|
||||
prod: prod-db,prod-metrics
|
||||
|
||||
server:
|
||||
port: 8080
|
||||
|
||||
logging:
|
||||
level:
|
||||
root: WARN
|
||||
|
||||
demo:
|
||||
# The property every scenario asks about. Each source below sets it to a string naming
|
||||
# itself, so the winner is self-identifying in the transcript.
|
||||
greeting: from-application-yaml
|
||||
datasource-url: jdbc:h2:mem:default
|
||||
@@ -0,0 +1,4 @@
|
||||
# Imported by application-import.yaml. Sets the same key, and loses.
|
||||
demo:
|
||||
greeting: from-imported-yaml
|
||||
imported-only: yes-this-file-was-read
|
||||
@@ -0,0 +1,102 @@
|
||||
package com.ankurm.profiles;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.WebApplicationType;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Pins the precedence claims the article makes, so a future Spring Boot upgrade that changes
|
||||
* any of them fails the build rather than quietly making the article wrong.
|
||||
*/
|
||||
class PrecedenceContractTests {
|
||||
|
||||
private ConfigurableApplicationContext run(String... args) {
|
||||
SpringApplication application = new SpringApplication(ProfilesApplication.class);
|
||||
application.setWebApplicationType(WebApplicationType.NONE);
|
||||
return application.run(args);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a profile-specific file beats application.yaml")
|
||||
void profileFileBeatsBaseFile() {
|
||||
try (var context = run("--spring.profiles.active=prod")) {
|
||||
assertThat(context.getEnvironment().getProperty("demo.greeting"))
|
||||
.isEqualTo("from-application-prod-yaml");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a profile group activates every profile it names")
|
||||
void profileGroupExpands() {
|
||||
try (var context = run("--spring.profiles.active=prod")) {
|
||||
assertThat(context.getEnvironment().getActiveProfiles())
|
||||
.containsExactlyInAnyOrder("prod", "prod-db", "prod-metrics");
|
||||
assertThat(context.getEnvironment().getProperty("demo.pool-size")).isEqualTo("40");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a command-line argument beats every config file")
|
||||
void commandLineBeatsConfigData() {
|
||||
try (var context = run("--spring.profiles.active=prod",
|
||||
"--demo.greeting=from-command-line")) {
|
||||
assertThat(context.getEnvironment().getProperty("demo.greeting"))
|
||||
.isEqualTo("from-command-line");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The counterintuitive one, and the reason the article has a callout about it: an
|
||||
* imported document outranks the document that imported it.
|
||||
*/
|
||||
@Test
|
||||
@DisplayName("spring.config.import: the IMPORTED file wins over the importing file")
|
||||
void importedFileWins() {
|
||||
try (var context = run("--spring.profiles.active=import")) {
|
||||
ConfigurableEnvironment environment = context.getEnvironment();
|
||||
assertThat(environment.getProperty("demo.imported-only"))
|
||||
.as("the import was processed at all")
|
||||
.isEqualTo("yes-this-file-was-read");
|
||||
assertThat(environment.getProperty("demo.greeting"))
|
||||
.as("and it beat application-import.yaml, which declared the import")
|
||||
.isEqualTo("from-imported-yaml");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("later documents in a multi-document file win over earlier ones")
|
||||
void multiDocumentOrdering() {
|
||||
try (var context = run("--spring.profiles.active=multidoc")) {
|
||||
assertThat(context.getEnvironment().getProperty("demo.greeting"))
|
||||
.isEqualTo("from-multidoc-default-document");
|
||||
}
|
||||
try (var context = run("--spring.profiles.active=multidoc,staging")) {
|
||||
assertThat(context.getEnvironment().getProperty("demo.greeting"))
|
||||
.isEqualTo("from-multidoc-staging-document");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("a config tree beats application.yaml but still loses to nothing above it")
|
||||
void configTreeIsConfigData(@org.junit.jupiter.api.io.TempDir java.nio.file.Path mount)
|
||||
throws Exception {
|
||||
java.nio.file.Files.writeString(mount.resolve("demo.greeting"), "from-config-tree");
|
||||
|
||||
try (var context = run("--spring.config.import=configtree:" + mount + "/")) {
|
||||
assertThat(context.getEnvironment().getProperty("demo.greeting"))
|
||||
.isEqualTo("from-config-tree");
|
||||
}
|
||||
// A command-line argument still outranks it: a config tree is config data.
|
||||
try (var context = run("--spring.config.import=configtree:" + mount + "/",
|
||||
"--demo.greeting=from-command-line")) {
|
||||
assertThat(context.getEnvironment().getProperty("demo.greeting"))
|
||||
.isEqualTo("from-command-line");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user