Part 7 · 1 chapters · ~8 min

Spring Internals

The ApplicationContext and bean lifecycle, component scanning, auto-configuration and conditions, proxies (JDK and CGLIB) and the self-invocation trap, transaction propagation and isolation, Spring Data query derivation, the Security filter chain, and WebFlux with Reactor.

10

Wiring and proxies

code
java -jar ledger.jar --debug | grep -A2 "DataSourceAutoConfiguration matched"   # why a bean exists

// the self-invocation trap
@Service class Payouts {
    public void submitAll(List<Payout> ps) { for (var p : ps) submit(p); }   // calls this.submit: NO proxy, NO transaction
    @Transactional public void submit(Payout p) { ... }
}
// fix: move submit to another bean, or inject the proxy, or use TransactionTemplate explicitly

@Transactional(propagation = Propagation.REQUIRES_NEW)   // a separate transaction (an audit record that must persist even on rollback)
@Transactional(isolation = Isolation.SERIALIZABLE)       // rarely; handle serialization failures with retries
HOW SPRING WIRES AN APPLICATION
scan, define, auto-configure, instantiate, proxy
component scan@Component, @Service, @RestControllerbean definitionswhat to createauto-configuration@ConditionalOnClass / OnMissingBeaninstantiateconstructor injection, dependency orderproxies@Transactional, @Cacheable, @AsyncApplicationContextready
swipe the figure sideways, or tap expand for full screen
1/5
scanning
At start-up Spring scans packages for annotated classes and records bean definitions: what to create and with which dependencies.
find annotated classesthis is part of start-up time