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.
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.
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.
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.
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 + AuditingBeanPostProcessor | An @Aspect with @Around (Spring AOP), or Micrometer's @Timed |
LogBeanFactoryPostProcessor | PropertySourcesPlaceholderConfigurer (resolves ${...} in definitions) and ConfigurationClassPostProcessor (turns @Configuration into definitions) |
JpaCondition | The 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 |