top of page
Writer's pictureRohit Modi

Implementing Map/List in Spring Boot property (yaml or .properties) file

Updated: Dec 18, 2022

In this article we will go through the implementation of Map/List in property (yaml or .properties) file.

Sometime while working in a project, we come across a scenario where we have to create a Map or List with multiple static values, this can be achieved either by writing a class and create a Map/List with all the static values or through properties file.


first approach where we create a class with Map/List object and putting all the static values is quite common but in this case for every request class will be loaded by class loader in memory but

if we go for second approach where we create the Map/List with static values in property file, this will be loaded only once hence we can save memory and reduce the access time.

Along with this, we don't have to create new class and we can separate business logic from static entity driven logic.


Now its time to see how this can be achieved in spring boot.


Go to application.properties or application.yml and create a Map/List in following way :


#Map can be created like below


roletype:

roles:

admin: Edit level - full access of employee details

employee: View level - can view the details but can edit few details

hr: Read level - can view the details but can not edit

security: Read Write level - can view the details and provide security



#List can be created like below


department:

computer:

- programming_lab

- class

- head_office

- security_lab

To access Map we have to create a class which must be annotated with


@Component

@ConfigurationProperties(prefix = "roletype")


here we have to provide roletype as prefix and then create a roles as a variable which must be a type of Map, and then provide setter and getter for setting the static value automatically and getter method to fetch the values.


Class will look like this


@Component

@ConfigurationProperties(prefix = "roletype")

public class AccessLevelConfig {


private Map<String, String> roles;


public Map<String, String> getRoles() {

return roles;

}


public void setRoles(Map<String, String> roles) {

this.roles = roles;

}

}


Now to access List object we have to follow the same principle and end up creating a class like below


@Component

@ConfigurationProperties(prefix = "department")

public class DepartmentListConfig {


private List<String> computer;


public List<String> getComputer() {

return computer;

}


public void setComputer(List<String> computer) {

this.computer = computer;

}

}


A small Spring Boot project has created to show how this can work collectively, for this two endpoint has exposed to see its real time working


1. Endpoint : http://localhost:8080/role?roletype=admin


When request comes to this endpoint with a parameter 'roletype', it looks for roletype in a map where map has stored roletype as key with corresponding value. This map has implemented in yaml file.


this returns a value associated to the provided roletype


2. Endpoint : http://localhost:8080/department


When request comes to this endpoint, it will fetch all the data in the form of list which has implemented in yaml file.


Git Location for the project : https://github.com/rohitmodi07/springboot-List-Map-yaml-demo


That's all folks in this article, I will come up with another article shortly.

A Big thank you for checking out my article, this really encourage me to come up with more topics.


Here are some resources you might be interested ------------------------








202 views0 comments

Recent Posts

See All

Comments


Spring Boot
bottom of page