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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gip4srpzMwjgoba6uEfbr5
This commit is contained in:
28
spring-aop/scripts/demo-broken-gallery.sh
Executable file
28
spring-aop/scripts/demo-broken-gallery.sh
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
# The broken-aspect gallery: six aspects that do not fire, and why.
|
||||
set -euo pipefail
|
||||
set +m
|
||||
cd "$(dirname "$0")/.."
|
||||
source scripts/env.sh
|
||||
{
|
||||
echo "== aspects that do not fire =="
|
||||
echo
|
||||
start_app > /dev/null
|
||||
curl -s "http://127.0.0.1:${APP_PORT}/aop/broken" | python3 -m json.tool
|
||||
echo
|
||||
echo "Reading it:"
|
||||
echo
|
||||
echo " 1 @Aspect without @Component - the class is never instantiated, so the pointcut"
|
||||
echo " is never registered. No warning is produced."
|
||||
echo " 2 pointcut typo - 'com.ankurm.aop.services' (plural) parses fine and"
|
||||
echo " matches nothing. An empty match set is not an error."
|
||||
echo " 3 private method - cannot be overridden, so cannot be intercepted."
|
||||
echo " 4 final method - CGLIB subclasses; a final method is inherited, not"
|
||||
echo " overridden. Note beanIsProxied is still true."
|
||||
echo " 5 self-invocation - innerAdvisedWhenCalledFromOuter is false and"
|
||||
echo " innerAdvisedWhenCalledDirectly is true. Same method,"
|
||||
echo " same advice: only the call path differs."
|
||||
echo " 6 created with new - no container, no proxy, no advice."
|
||||
stop_app
|
||||
} > docs/output/03-broken-gallery.txt 2>&1
|
||||
cat docs/output/03-broken-gallery.txt
|
||||
48
spring-aop/scripts/demo-designators.sh
Executable file
48
spring-aop/scripts/demo-designators.sh
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env bash
|
||||
# Every pointcut designator Spring AOP supports, with what it actually matched.
|
||||
set -euo pipefail
|
||||
set +m
|
||||
cd "$(dirname "$0")/.."
|
||||
source scripts/env.sh
|
||||
{
|
||||
echo "== which designator matched which join point =="
|
||||
echo
|
||||
echo "Five methods are called once each: OrderService.place, OrderService.cancel,"
|
||||
echo "InventoryService.reserve, InventoryService.finalCheck and"
|
||||
echo "DefaultOrderService.interfaceless."
|
||||
echo
|
||||
start_app > /dev/null
|
||||
curl -s "http://127.0.0.1:${APP_PORT}/aop/designators" | python3 -c '
|
||||
import json,sys
|
||||
d=json.load(sys.stdin)
|
||||
print(" orderService proxy kind :", d["orderServiceProxyKind"])
|
||||
print(" proxy is an instance of DefaultOrderService :",
|
||||
d["orderServiceIsDefaultOrderServiceInstance"])
|
||||
print()
|
||||
for k,v in d["matches"].items():
|
||||
print(" %-32s -> %s" % (k, ", ".join(v)))'
|
||||
echo
|
||||
echo "== what the parser accepts and refuses =="
|
||||
echo
|
||||
curl -s "http://127.0.0.1:${APP_PORT}/aop/parser" | python3 -c '
|
||||
import json,sys
|
||||
d=json.load(sys.stdin)
|
||||
print(" supported (all parsed and evaluated):")
|
||||
for r in d["supported"]:
|
||||
print(" %-52s %s" % (r["expression"], "OK" if r["accepted"] else "REJECTED"))
|
||||
print()
|
||||
print(" unsupported in Spring AOP:")
|
||||
for r in d["unsupported"]:
|
||||
print(" %-52s %s" % (r["expression"][:50], "accepted!" if r["accepted"] else "rejected"))
|
||||
print()
|
||||
first=[r for r in d["unsupported"] if not r["accepted"]][0]
|
||||
print(" the exception, in full:")
|
||||
print(" " + first["exception"])
|
||||
print(" " + first["message"])'
|
||||
echo
|
||||
echo "The reference documentation says these produce an IllegalArgumentException. They do"
|
||||
echo "not: UnsupportedPointcutPrimitiveException extends RuntimeException directly, so a"
|
||||
echo "catch of IllegalArgumentException will not catch it."
|
||||
stop_app
|
||||
} > docs/output/01-designators.txt 2>&1
|
||||
cat docs/output/01-designators.txt
|
||||
56
spring-aop/scripts/demo-proxy-types.sh
Executable file
56
spring-aop/scripts/demo-proxy-types.sh
Executable file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env bash
|
||||
# JDK dynamic proxies versus CGLIB, and what changes when you switch.
|
||||
#
|
||||
# Spring Boot sets spring.aop.proxy-target-class=true by default, so beans are proxied by
|
||||
# CGLIB even when they implement an interface. Setting it to false restores the framework's
|
||||
# own default and changes which designators match -- the same aspects, different results.
|
||||
set -euo pipefail
|
||||
set +m
|
||||
cd "$(dirname "$0")/.."
|
||||
source scripts/env.sh
|
||||
|
||||
snapshot() {
|
||||
curl -s "http://127.0.0.1:${APP_PORT}/aop/proxies" | python3 -c '
|
||||
import json,sys
|
||||
for r in json.load(sys.stdin):
|
||||
print(" %-22s %-18s target=%s" % (r["bean"], r["proxyKind"], r["targetClass"].split(".")[-1]))
|
||||
print(" class : %s" % r["class"].split(".")[-1])
|
||||
print(" interfaces : %s" % (", ".join(r.get("proxiedInterfaces") or []) or "(none)"))
|
||||
print(" advisors : %d" % r.get("advisorCount", 0))'
|
||||
echo
|
||||
curl -s "http://127.0.0.1:${APP_PORT}/aop/designators" | python3 -c '
|
||||
import json,sys
|
||||
d=json.load(sys.stdin)
|
||||
print(" proxy is an instance of DefaultOrderService :",
|
||||
d["orderServiceIsDefaultOrderServiceInstance"])
|
||||
for k in ("this(OrderService)","target(DefaultOrderService)","bean(*OrderService)"):
|
||||
print(" %-30s -> %s" % (k, ", ".join(d["matches"].get(k, ["(no match)"]))))'
|
||||
}
|
||||
|
||||
{
|
||||
echo "== Spring Boot default: spring.aop.proxy-target-class=true =="
|
||||
echo "\$ java -jar $JAR"
|
||||
echo
|
||||
start_app > /dev/null
|
||||
snapshot
|
||||
echo
|
||||
echo "== framework default restored: spring.aop.proxy-target-class=false =="
|
||||
echo "\$ java -jar $JAR --spring.aop.proxy-target-class=false"
|
||||
echo
|
||||
start_app --spring.aop.proxy-target-class=false > /dev/null
|
||||
snapshot
|
||||
echo
|
||||
echo "Same aspects, same beans, different proxy strategy:"
|
||||
echo
|
||||
echo " - With CGLIB the proxy is a SUBCLASS of DefaultOrderService, so it is an instance of"
|
||||
echo " the implementation class and methods that are not on the interface are advised."
|
||||
echo " - With a JDK proxy the proxy implements OrderService only. It is NOT an instance of"
|
||||
echo " DefaultOrderService, casting to that class throws ClassCastException, and any"
|
||||
echo " method absent from the interface is invisible to advice."
|
||||
echo
|
||||
echo "This is why this() and target() differ. this() tests the proxy; target() tests the"
|
||||
echo "object behind it. Under CGLIB they usually agree, which is exactly why the"
|
||||
echo "distinction only bites after somebody switches the proxy type."
|
||||
stop_app
|
||||
} > docs/output/02-proxy-types.txt 2>&1
|
||||
cat docs/output/02-proxy-types.txt
|
||||
16
spring-aop/scripts/demo-versions.sh
Executable file
16
spring-aop/scripts/demo-versions.sh
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
source scripts/env.sh
|
||||
{
|
||||
echo "== versions =="
|
||||
java -version 2>&1 | clean
|
||||
echo
|
||||
echo "spring-boot-starter-parent: $(grep -A2 '<artifactId>spring-boot-starter-parent' pom.xml | grep '<version>' | sed 's/.*<version>\(.*\)<\/version>.*/\1/')"
|
||||
echo "aspectjweaver: $(find ~/.m2/repository -name 'aspectjweaver-*.jar' | sed 's/.*aspectjweaver-//;s/\.jar//' | sort | tail -1)"
|
||||
echo
|
||||
echo "== the Boot 4 starter rename =="
|
||||
echo "spring-boot-starter-aop last published: 4.0.0-M2 (last GA 3.5.16)"
|
||||
echo "spring-boot-starter-aspectj first published: 4.0.0-M3"
|
||||
} > docs/output/00-versions.txt 2>&1
|
||||
cat docs/output/00-versions.txt
|
||||
61
spring-aop/scripts/env.sh
Executable file
61
spring-aop/scripts/env.sh
Executable file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env bash
|
||||
# Shared environment. Point JAVA_HOME at a JDK 25 (or newer) installation.
|
||||
: "${JAVA_HOME:?set JAVA_HOME to a JDK 25+ installation}"
|
||||
export PATH="$JAVA_HOME/bin:$PATH"
|
||||
MVN="${MVN:-mvn}"
|
||||
JAR="target/spring-aop-demo-1.0.0.jar"
|
||||
APP_MAIN="com.ankurm.aop.AopApplication"
|
||||
APP_PORT="${APP_PORT:-8080}"
|
||||
|
||||
# Strip environment noise that is an artefact of the machine, not of Spring:
|
||||
# the JVM prints a JAVA_TOOL_OPTIONS banner to stderr on every launch when a proxy
|
||||
# truststore is configured, and it would otherwise end up in every committed transcript.
|
||||
clean() { grep -v "Picked up JAVA_TOOL_OPTIONS" | grep -v "^OpenJDK 64-Bit Server VM warning"; }
|
||||
|
||||
# Start the demo jar detached, record its PID, and block until it answers.
|
||||
# Extra arguments are passed to the application. Environment variables for the run are
|
||||
# passed by setting them on the call: `APP_ENV="A=1 B=2" start_app --spring.profiles.active=x`
|
||||
start_app() {
|
||||
stop_app
|
||||
mkdir -p target
|
||||
# Deliberately NOT setsid: setsid forks when it is not already a process-group leader,
|
||||
# so $! would be the PID of a process that exits immediately and the JVM would survive
|
||||
# every later stop_app. A surviving JVM keeps the port, the next scenario fails to bind,
|
||||
# and curl answers from the previous scenario -- which reads exactly like the
|
||||
# configuration change under test having had no effect. Three wrong findings in this
|
||||
# repository came from that before it was tracked down.
|
||||
if [ -n "${APP_ENV:-}" ]; then
|
||||
# shellcheck disable=SC2086
|
||||
env $APP_ENV nohup java -jar "$JAR" "$@" > /tmp/aop-demo.log 2>&1 < /dev/null &
|
||||
else
|
||||
nohup java -jar "$JAR" "$@" > /tmp/aop-demo.log 2>&1 < /dev/null &
|
||||
fi
|
||||
echo $! > target/app.pid
|
||||
for _ in $(seq 1 60); do
|
||||
curl -s -o /dev/null "http://127.0.0.1:${APP_PORT}/aop/proxies" 2>/dev/null && return 0
|
||||
kill -0 "$(cat target/app.pid)" 2>/dev/null || { echo "JVM exited during startup:"
|
||||
tail -20 /tmp/aop-demo.log; return 1; }
|
||||
sleep 1
|
||||
done
|
||||
echo "application did not answer"; tail -20 /tmp/aop-demo.log; return 1
|
||||
}
|
||||
|
||||
# Stop it by recorded PID. Never by pattern: `ps | grep <jar name>` also matches the shell
|
||||
# running the script, because the jar name is on that shell's own command line.
|
||||
stop_app() {
|
||||
if [ -f target/app.pid ]; then
|
||||
pid=$(cat target/app.pid)
|
||||
if [ -n "$pid" ] && grep -qa "spring-aop-demo" "/proc/$pid/cmdline" 2>/dev/null; then
|
||||
kill -9 "$pid" 2>/dev/null || true
|
||||
wait "$pid" 2>/dev/null || true # reap, so bash prints no "Killed" notice
|
||||
fi
|
||||
rm -f target/app.pid
|
||||
fi
|
||||
for _ in $(seq 1 40); do
|
||||
if ! (exec 3<>/dev/tcp/127.0.0.1/"${APP_PORT:-8080}") 2>/dev/null; then break; fi
|
||||
sleep 0.25
|
||||
done
|
||||
exec 3<&- 2>/dev/null || true
|
||||
}
|
||||
|
||||
# Print the precedence report for one property, compactly.
|
||||
16
spring-aop/scripts/run-all.sh
Executable file
16
spring-aop/scripts/run-all.sh
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env bash
|
||||
# Regenerate every transcript under docs/output/.
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
source scripts/env.sh
|
||||
|
||||
"$MVN" -B -q package -DskipTests
|
||||
|
||||
for demo in versions designators proxy-types broken-gallery; do
|
||||
echo "=== $demo ==="
|
||||
"scripts/demo-$demo.sh" > /dev/null
|
||||
done
|
||||
stop_app
|
||||
echo
|
||||
echo "regenerated:"
|
||||
ls -1 docs/output/
|
||||
Reference in New Issue
Block a user