Spring Boot :Type Safe Configuration Properties
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
Search for a command to run...
I am a developer who loves Java, Spring, Quarkus, Micronaut, Open source, Microservices, Cloud
No comments yet. Be the first to comment.
Spring AI Tool Calling: From Chatbot to AI Agent with @Tool Your AI is smart. It knows an enormous amount. But it's frozen. It doesn't know what time it is right now. It doesn't know what's on your ca
Spring AI Advisors API Explained Series: Spring AI Complete Course — Lecture 4 of 12Reading Time: 8 minutesLevel: Intermediate Most developers stop at ChatClient. That is enough for demos. It is not
Working with Multiple AI Models in Spring AI Spring AI Complete Course — Lecture 3 of 12Previous: Lecture 2 — ChatClient API | Next: Lecture 4 — Advisors API In production AI applications, you rarel
Spring AI ChatClient API: The Fluent Heart of AI Integration Introduction If you've ever tried integrating AI models into a Java application, you know the pain. HTTP clients, API keys scattered everywhere, vendor-specific SDKs that never quite fit. W...
What is Spring AI? — Why Java Developers Need This in 2026 Every AI tutorial you see is in Python. LangChain, LlamaIndex, OpenAI SDK — all Python. But here's the uncomfortable truth: 80% of enterprise backends run Java. So who's building AI into thos...
property to be read from yaml or properties file. The number of variable we will be declaring would be way too many.
This problem looks more ugly in case if we require similar kind (read group) of properties. For example in case of properties related to Database or Security related configurations. Below is such a example :
# application.properties
#For using @ConfigurationProperties
application.name=User Application
application.version=1.0.0
application.description = ${app.name} - ${app.version} is an application to store user details
.application.security.enabled=true
application.security.user=admin
application.security.email= unk@unk.com
application.security.roles[0]=USER
application.security.roles[1]=ADMIN
Here if we try to read variables , tough all variables are related to app(which is at top level of hierarchy ), we will end up creating many variable to read each property from YAML , this might even make our class look ugly if number of properties increases significantly.
Spring provides a simple way to help us. We can create a class and bind all the properties from configuration files like YAML or properties . We can create a class with
@ConfigurationProperties
For above configuration let’s create a class and verify that it works.
@ConfigurationProperties(prefix = "application")
@Component
public class AppConfig {
private String name;
private String version;
private String description;
private Security security;
//getters and setters
}
We can create a security class and use it inside AppSecurity , values corresponding to app.security will be set inside it.
Security.java
public class Security {
private String user;
private String email;
private String [] roles;
//getters and setters
}
Now let’s autowire AppConfig class in our CommonController and create a request mapping to see that it works.
Inside CommonController.java
Add a Autowired variable for AppConfig
@Autowired private AppConfig appConfig;
And then add a request mapping which returns AppConfig. If bindings are correct ,it will return all the values.
@RequestMapping("app-config")
protected AppConfig appConfig(){
return appConfig;
}
Once done , lets verify if the entire code is working well . Just hit URL : http://localhost:8081/v1/app-config
You should be getting following response.
{
"name": "User Application",
"version": "1.0.0",
"description": "User Application - 1.0.0 is an application to store user details",
"security": {
"user": "admin",
"email": "unk@unk.com",
"roles": [
"USER",
"ADMIN"
]
}
}
This shows how easily we can have a type safe and less verbose property file reading using , @ConfigurationProperties.