Spring Boot services · Chapter 6 of 41
How auto-configuration works
By the end of this chapter you can write an auto-configuration that gives an app a bean only when it makes sense, and explain how Spring Boot decides what to configure. 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
Partner cafés are joining Tapri Cloud, and Anjali has their list: "Some cafés have our thermal printer, some bring their own, and the airport kiosk has no printer at all." Every Tapri app should get the receipt printer for free, without breaking any of them.
Today the printer is a plain @Bean in a @Configuration, so every app gets one. A café that adds its own printer has two ReceiptPrinter beans, so asking for "the" printer fails, and the kiosk gets a printer it cannot drive. With a profile per café, someone has to remember to switch it on, everywhere.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
ThermalPrinter.java: lay out a receipt line to the paper width
Write ThermalPrinter.line(item, price) so it returns exactly width() characters: the item on the left, then spaces, then the price as "₹" and the number, ending at the paper's edge. With width 32, line("Masala chai", 40) is "Masala chai", 18 spaces and "₹40".
An item too long for the line is cut so that one space still separates it from the price: with width 12, line("Kulhad chai with elaichi", 60) is "Kulhad c ₹60" and line("Chai", 40) is "Chai ₹40".
- Task 2
ReceiptPrinterAutoConfiguration.java: take the paper width from tapri.printer.width
Make the receiptPrinter bean take its width from the property tapri.printer.width, and use 32 when the property is not set.
With tapri.printer.width=24, width() is 24 and line("Cutting chai", 25) has 24 characters, so task 1 must work too.
- Task 3
ReceiptPrinterAutoConfiguration.java: back off when the app has its own printer
When the app defines its own ReceiptPrinter bean (a café's printer, with any bean name), the configuration must not add its receiptPrinter: the context then holds only the app's printer, and asking for a ReceiptPrinter gives the app's one.
When the app has no printer of its own, receiptPrinter is still there.
- Task 4
ReceiptPrinterAutoConfiguration.java: switch the printer off with tapri.printer.enabled=false
With tapri.printer.enabled=false there is no ReceiptPrinter bean at all.
With tapri.printer.enabled=true, and also when the property is not set at all, there is exactly one: the printer is on unless someone says false.
- Task 5 · interview question
Interview: make it an auto-configuration that needs the thermal driver on the classpath
Make ReceiptPrinterAutoConfiguration an @AutoConfiguration instead of a plain configuration, kept only when the class ThermalDriver is on the classpath. Put this class condition on the class itself: with ThermalDriver hidden there is no ReceiptPrinter bean, and Boot's condition report for ReceiptPrinterAutoConfiguration says it did not find ThermalDriver. With the driver there, there is one printer.
Afterwards run-app finds no printer, because Spring Boot keeps auto-configuration classes out of component scanning; the Check loads the class the way Boot loads one. Be ready to explain how Spring Boot decides what to configure.
What you will learn
- Auto-configuration: beans Boot adds after yours, only when they make sense.
- Conditions: @ConditionalOnMissingBean, @ConditionalOnProperty and @ConditionalOnClass decide whether a bean arrives.
- The condition evaluation report: --debug shows every decision and its reason.
More in Spring foundations
Build "How auto-configuration works" 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.