Spring Boot services · Chapter 7 of 41
The bean lifecycle
By the end of this chapter you can run your own code at the right moments of a bean's life: after injection, once the app has started, when it is ready, on first use and at shutdown. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Spring foundations
- 6 tasks
- Spring Boot
- In the all labs plan
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.
Why you need this
The 6 am rush at New Delhi starts the moment the app does. Ravi needs four things: the menu already in memory when the first order arrives, the counter opened for the station given on the command line, a "ready" line in the shift log so the night staff can hand over, and the stock saved when the app stops. "Every time."
So far your code runs in constructors, while the context is still being built. Fields injected later are still null there, a "ready" written there comes long before the app is ready, and nothing in a constructor runs at shutdown.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MenuCache.java: load the menu at startup, once the dependencies are in
Have Spring call MenuCache.load once at startup, after the bean is built and its dependencies are injected. load fills items with Masala chai, Cutting chai, Bun maska and Parle-G, in that order, and writes "menu loaded" to the shift log.
Right after startup, items() returns those four, and "menu loaded" is in the shift log exactly once. Nobody calls load by hand.
- Task 2
CounterOpener.java: open the counter for --station with an ApplicationRunner
Make CounterOpener an ApplicationRunner, so Spring Boot calls run(args) once the app has started. run writes "counter opened at " and the value of the --station argument to the shift log: "counter opened at NDLS" when started with --station=NDLS.
With no --station argument at all it writes "counter opened at SBC". The menu (task 1) is loaded before the counter opens.
- Task 3
CounterOpener.java: announce that Tapri Cloud is ready on ApplicationReadyEvent
Have Spring call CounterOpener.ready on ApplicationReadyEvent. It writes "ready" to the shift log, and "ready" must be the last entry written at startup (after "counter opened at ...").
- Task 4
ReportPrinter.java: create the report printer only when someone first asks for it
Make Spring create ReportPrinter only when someone first asks the context for it, not at startup. Its constructor writes "report printer created", so that entry must not be in the shift log at startup, and must be there after the first request for the bean.
Keep the constructor as it is. print() still answers "Shift report: " and the number of entries.
- Task 5
MenuCache.java: save the stock in @PreDestroy when the app shuts down
Have Spring call MenuCache.save when the context closes (the app shutting down, as with Ctrl+C). save writes "stock saved" to the shift log: not before shutdown, and exactly once after it.
- Task 6 · interview question
Interview: put the lifecycle steps in the order Spring runs them
Reorder the list LifecycleQuiz.order() returns so the six steps are in the order Spring runs them for one singleton bean that has all of them, from startup to shutdown. The six are the ones the starter lists: ApplicationRunner, @PostConstruct, @PreDestroy, constructor, ApplicationReadyEvent and setter injection.
Keep the six strings exactly as they are: no renames, no steps added or removed. Be ready to explain what happens to a bean, in order, from startup to shutdown.
What you will learn
- @PostConstruct and @PreDestroy: a method Spring calls once the bean is injected, and one it calls before the bean is destroyed.
- Runners and ApplicationReadyEvent: code that runs once the app has started, and the moment it is ready.
- @Lazy: a bean created on first use instead of at startup.
More in Spring foundations
Build The bean lifecycle yourself
Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.