Spring Boot services · Chapter 5 of 41
Configuration and profiles
By the end of this chapter you can move settings out of the code into properties files, bind and validate them, and switch beans and values with profiles and environment variables. 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
Tapri Cloud is about to run in three places: Kabir's laptop, the test server, and production for the Bengaluru station. Ravi lists what differs: the station code, the GST rate, the hour the counter opens, and whether an alert goes out as a real SMS. "Please don't make me ask for a new build every time one of those changes."
So far every value lives in the Java code. A constant like "SBC" means one build per station, a wrong GST rate is found by a customer, and choosing the SMS notifier on the server means editing a class.
What you build: 6 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
application.properties: set Tapri Cloud's station and GST
In application.properties, set tapri.station to SBC and tapri.gst-percent to 5, using exactly those property names, one per line. Keep spring.application.name as it is.
- Task 2
TapriProperties.java: bind the tapri properties to the record
Make TapriProperties a configuration properties record for the prefix tapri, so tapri.station fills station and tapri.gst-percent fills gstPercent, and make TapriApplication scan for configuration properties records so Spring registers it.
With tapri.station=NDLS and tapri.gst-percent=12 the context must start and hold exactly one TapriProperties bean, with station NDLS and gstPercent 12.
- Task 3
TapriProperties.java: refuse to start with a blank station or a GST over 28
Validate TapriProperties when the app starts: the context must fail to start when tapri.station is blank (a single space counts as blank), when tapri.gst-percent is above 28 (40), or when it is below 0 (-1).
Valid values, such as station CSMT with 18 percent, must still start the app with exactly one TapriProperties bean. 0 and 28 are allowed.
- Task 4
CounterService.java: read the opening hour with @Value, 6 when it is not set
Give CounterService a constructor that receives the int value of tapri.counter.opens-at, with a default of 6 when the property is not set, keep it in a field, and return it from opensAt().
With no tapri.counter.opens-at, opensAt() is 6; with tapri.counter.opens-at=7, it is 7.
- Task 5
Notifier.java: SMS in the prod profile, the console everywhere else
Make SmsNotifier a bean only when the prod profile is active, and ConsoleNotifier a bean in every other case: no profile at all, dev, or any profile that is not prod. There must be exactly one Notifier bean each time.
With no profile or with dev, send("Milk is low") is "Console: Milk is low"; with prod it is "SMS: Milk is low".
- Task 6
application-prod.properties: open at 5 in prod, and let the environment override both files
In application-prod.properties, set tapri.counter.opens-at to 5. With the prod profile, opensAt() is 5 and the station is still SBC from application.properties; with no profile, opensAt() is still 6.
An environment variable beats both files: with TAPRI_STATION=CSMT the bound station is CSMT. That part works by itself once tasks 1, 2 and 4 are done.
What you will learn
- application.properties and @Value: settings outside the code, read into a constructor, with a default.
- @ConfigurationProperties: a whole group of settings in one validated record.
- Profiles: @Profile beans and application-{profile}.properties.
- What wins: command line, environment variables, then the files.
More in Spring foundations
Build "Configuration and profiles" 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.