Spring Read Messge Property File for Hashmap

Reading message.properties(i18n) file programmatically

Nakul Pant

In this weblog, we will see how tin can nosotros access the properties file in Spring Boot programmatically.

Some time back I was working on a utilize example to get the localized letters from the database instead of message.properties. Since the application was a multi-tenant application nosotros needed to provide the bulletin customization feature to our users too.

Sounds good? But the master issue was where to store the default messages. Equally it would take been hectic to every fourth dimension create the entries in the database for every tenant. The problem became more hectic when we started developing the awarding. Then to overcome this trouble nosotros used the properties file to contain default messages for each category similar validations, API responses, etc.

This backdrop file then volition be loaded at the booting up of the application and will create the letters with their keys into the databases. I will encompass internationalized messages from the database in a separate story.

So let's first with a bones example of how to go this done.

So here's our backdrop file that had the validation messages.

So these letters were the default messages that will be created for every tenant in the database. Next, we create a database schema to hold these records in the database. We are using MongoDB for the database.

Here locale will store the language code, the key will store the message key from the backdrop file, the content will store the message content from the properties file. tenantId stores the unique key of the tenant for which the message tape is created. For simplification, I have categorized the message types to exist of types of VALIDATION, API, etc stored in messageType.

@UniqueMessageKey(bulletin = "validation.message.key.exists") is a custom validator to check that duplicate bulletin keys don't get created for every tenant. You can read more about Custom Validators in my previous blog hither.

Now nosotros will need a service form that processes our backdrop file and stores them in the database.

Our validation service course implements the InitializingBean which will call afterPropertiesSet() overridden method after the initialization. The properties field is a hashmap which will store the key values from the properties file and volition exist used for other processing. MessageRepository is our repository grade for the Message model.

Next, we load the validation.backdrop every bit a classpath resource in the afterPropertiesSet() method. We will use to load our resource as backdrop instance.
PropertiesLoaderUtils.loadProperties() is a convenient method that loads a resource for programmatically accessing the content as key-value. We iterate on this backdrop instance and store the key-value based message in our divers properties map.

At present we create a method in this service grade that volition be used to create the database records of these messages for all tenants.

In this createValidationMessagesForAllTenants() method, we iterate over the properties map and create database records for the messages per tenants. For the sake of simplicity, nosotros are using default locale in this example.

Looks skillful so far. But what if we have to add/modify a new holding? Do we have to create a new record every fourth dimension? Let's add i more than method which will update the records in the database.

This updateValidationMessagesForAllTenants() will check if a tape exists past the message key, if non and so this will create the new message record for the tenants.

Now you must be wondering that we accept created these two methods merely nosotros aren't using these methods anywhere. So let'due south create a service that volition be bootstrapping these messages on application startup and updating the records if changed.

Our MessageBootstrap.coffee is a service class that contains a method createData() that tin can be called in your application-level bootstrap which caters to the application level bootstrapping of information. We have a boolean flag isUpdatingMessagesAllowed that can be turned on or off to let updating of messages. This tin can be achieved in diverse means but for simplicity, I have taken it in the method itself.

In our next story, we volition encounter how to configure and use these database enlightened localized letters in our application.

simpsonhungloned.blogspot.com

Source: https://medium.com/techcret/reading-message-properties-file-programmatically-17f2ab4e488

0 Response to "Spring Read Messge Property File for Hashmap"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel