dmdev course · Spring · 2022 → 2026

Spring under the hood

Annotations do not do anything by themselves. Somebody reads them: post-processors look at bean definitions, create objects, inject fields and wrap beans into proxies. In this course of 2022 I wrote those "somebodies" myself, and here they run step by step, with the real output.

A call to companyCrudRepository.findById goes through two JDK proxies made by my own BeanPostProcessors (transaction, then auditing), reaches the real CompanyCrudRepository, then the Spring Data repository, Hibernate and PostgreSQL repository.findById(1)CourseRunner $Proxy · TransactionBeanPostProcessor Open transaction … Close transaction $Proxy · AuditingBeanPostProcessor Audit method … Time execution CompanyCrudRepository @InjectBean pool1 · @Transaction · @Auditing → CompanyRepository (Spring Data) Hibernate → PostgreSQL

The life of a bean

Every bean of the course goes through the same stages. Most of the course is about hooking into one of them. Pick a stage to see which class of the course works there and what it printed in the recorded run.

bean definitions only, no objects yet one bean is being created the context is closed

The course, step by step

In 2022 every lesson overwrote the previous one, and only the last state survived. Now every topic is a step of CourseRunner (./run.sh bpp) that starts its own ApplicationContext. On the left is the step and the classes of the course it uses: my code of 2022 with my comments in gold and 2026 notes in blue. On the right is what the run printed.


          

One call, four objects

The bean called companyCrudRepository is not a CompanyCrudRepository. Two of my BeanPostProcessors replaced it with JDK proxies, one inside the other. Walk through the call and watch the lines appear in the order they were printed in the recorded run.

This is how Spring's own @Transactional, @Cacheable and @Async work. Spring makes CGLIB subclasses when there is no interface. A JDK proxy can only implement interfaces, which is why CompanyCrudRepository needs CrudRepository.

What was broken in 2022

The commit of 2022 was called "ALL WORKS!!!", and it did start. These are the things that did not work, each one found by running it in 2026 and covered by a test.

validate was never on

The Hibernate keys in application.yaml were one level too high, so properties.hibernate was empty.

jpa: properties.hibernate: hbm2ddl.auto: validate
jpa: properties.hibernate: hbm2ddl.auto: validate

Checked on the 2022 code: with a column dropped, the application still started.

The BeanPostProcessors did nothing

@Transaction, @Auditing and @InjectBean were written, but no class used them. CompanyCrudRepository is restored from the commented-out CrudRepository<Integer, Company> lines, so the proxies run again.

The proxy threw the wrong exception

method.invoke wraps the real exception.

UndeclaredThrowableException
IllegalArgumentException: Company must not be null

LazyInitializationException outside the tests

CompanyService.findById published an event, and the listener printed Company. Lombok's toString read the lazy locales after the session was closed. The test did not see it because @IT kept a transaction open. Fix: @Transactional(readOnly = true).

Tests that could run once

@Commit really wrote "Apple" into the database, and delete() used a hard-coded id 7. Maven also ran 1 test of 7, because *IT classes need Failsafe.

Tests run: 18, Failures: 0 (and again)

Smaller things

data.sql started with NSERT. java -jar said "no main manifest attribute", because the Boot plugin sat in pluginManagement. It needed Java 18. @InjectBean failed with three ConnectionPool beans. The listener printed Entityspring.... Package annotattion.

My classes and Spring's classes

Everything I wrote by hand in this course has a real counterpart inside Spring that works the same way.

In this course (2022)The real thing in Spring
Container.get(Class)ApplicationContext.getBean(Class): DefaultListableBeanFactory creates beans from definitions, resolves constructor arguments and caches singletons
@InjectBean + InjectBeanPostProcessor@Autowired + AutowiredAnnotationBeanPostProcessor: the same reflection on fields, plus constructors and setters
@Transaction + TransactionBeanPostProcessor@Transactional + TransactionInterceptor: a proxy that really begins, commits or rolls back a database transaction
@Auditing + AuditingBeanPostProcessorAn @Aspect with @Around (Spring AOP), or Micrometer's @Timed
LogBeanFactoryPostProcessorPropertySourcesPlaceholderConfigurer (resolves ${...} in definitions) and ConfigurationClassPostProcessor (turns @Configuration into definitions)
JpaConditionThe whole Spring Boot auto-configuration: @ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty
Spring Boot 2.7.18 (javax)Spring Boot 3: jakarta.* instead of javax.*, Java 17+, @ConstructorBinding no longer needed on a single constructor