How To Export application.properties To A Java Class in Spring Boot

Brian Kitunda
1 min readMar 21, 2021
Photo by Sigmund on Unsplash

Spring Boot is built around configurations. An example of a configuration file is the application.properties

Sometimes we need to export/import the properties into a Java class so as to make them usable.

Suppose we have a JWT(Json Web Token) key. If we want to make use of it in our project, an ideal solution is to export into a Java class.

This tutorial is going to show you how to export application.properties into a Java class.

  1. Edit the application.properties file
application.jwt-key=12bansmammanshjamamma

Notice the prefix, in this case, application. It is essential in the next step.

2. Create a Java class

Create a java class with a name of your liking that we shall use to import the application.properties

3. Edit the Java class

import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="application")
public class ApplicationProperties{
private String jwtKey;// getters and setters}

Import the ConfigurationProperties annotation. Have a prefix property, in this case application.

RECAP : Remember the prefix we set in the application.properties i.e application.jwt-key.

Have the appropriate getters and setters to change and read the value of the class variables or properties.

4. Ready to use

We are now ready and able to use the application.properties anywhere we want to.

--

--