Spring Boot services · Chapter 3 of 41
Beans and the application context
By the end of this chapter you can turn your classes and library objects into Spring beans, look them up in the application context, and choose whether a bean is shared. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Spring foundations
- 5 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
Ravi asks what nobody can answer yet: "When Tapri Cloud starts, what is actually running in there?" Meera needs every receipt in India time, although the server runs in UTC. And each counter needs a fresh order ticket for each customer.
So far Spring created one thing for you, the controller. Anything else you would make with new wherever it is needed: every class its own MenuService, Clock.systemDefaultZone() in fifty places (UTC on the server), and no way to list, share or swap any of them.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MenuService.java: make MenuService a bean Spring finds by scanning
Mark MenuService as a component so that component scanning registers it: the context must hold exactly one MenuService bean, under the default name menuService (do not give it another name).
Leave priceOf as it is: the test reads priceOf("Masala chai") through the bean and expects 40.
- Task 2
TapriConfig.java: declare an India-time Clock bean
Make TapriConfig's clock method a bean definition, so the context holds exactly one java.time.Clock bean, named clock and made by the tapriConfig bean.
The clock must tell the system time in the Asia/Kolkata zone: its zone is exactly Asia/Kolkata, whatever zone the server runs in.
- Task 3
BeanReport.java: list Tapri's own beans by name
Write BeanReport.tapriBeans() so it returns the names of every bean in the context whose class is in the package tapri (the package name is exactly tapri), sorted alphabetically. Ask the context for the names and their types; do not list names by hand.
With tasks 1 and 2 done the list includes beanReport, menuService, orderTicket, tapriApplication and tapriConfig. Spring's own beans and clock (a JDK class) are not in it. Skip names whose type the context does not know (it answers null), and keep tapriConfig, whose class is a generated subclass that is still in package tapri.
- Task 4
BeanReport.java: tell a singleton bean from a prototype one
Write BeanReport.scopeOf(beanName) so it returns "prototype" when the bean with that name is a prototype, and "singleton" otherwise.
Ask the context about the bean; do not decide from the name. scopeOf("tapriConfig") and scopeOf("beanReport") are "singleton", and the Check also asks about a prototype bean of its own that is not in your code.
- Task 5 · interview question
Interview: give every order its own ticket, since singletons are shared
Make OrderTicket a prototype bean, so two calls to getBean(OrderTicket.class) give two different objects, and an item added to the first ticket is not on the second.
Only OrderTicket changes: BeanReport must stay a singleton. Be ready to explain why a singleton bean must not keep one request's data in its fields.
What you will learn
- Component scanning: a component annotation makes Spring create your class as a bean.
- @Bean methods: beans made from classes you did not write.
- The application context: ask it for beans, their names and types.
- Scopes: a singleton is shared; a prototype is new every time.
More in Spring foundations
Build "Beans and the application context" 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.