How to wire up and use externally configurable variables in Spring Boot + Java

Tremaine Eto
Mar 16 · 3 min read
Original photo by Aniket Bhattacharya; logo by Pivotal; text by Tremaine Eto

Spring Boot is awesome for many reasons, and one of them is the ease of setting up external configuration so that you can run the same application code in different environments or update variables at any time without going in and having to change the code itself.

This article will go over how to achieve this in your Java application with Spring Boot using the annotations @Component and @ConfigurationProperties.

Config class declaration

First, you’ll have to create a Java class that will be a Spring Boot bean by way of the @Component annotation.

In it, the variables will be the ones that you wish to be externally configurable. If these are String values— like usernames or e-mail addresses, for example — then declare them as such; if these are int values— like port numbers or thread counts, for instance — then declare those accordingly.

In this example, we have an EmailConfig class that is used to configurable variables for sending e-mails in our code. We are allowing for the flexibility to:

  • turn the functionality on and off with the enabled Boolean variable;
  • edit the sender e-mail with the senderEmail String value;
  • edit the list of recipient e-mails with the recipientEmails list of String values;
  • edit the subject of the e-mail with the subject String variable

application.properties

We can define default values for these in our application.properties (sometimes application.yml) file as follows:

What is important here is that these all have the prefix email — if you scroll back up to the bean we defined, you will note that there is a @ConfigurationProperties annotation which is specifically looking for this prefix.

Spring Boot will work its own magic and then match up the variables according to their names; be careful with the capitalization, because Spring Boot will try to match according to its relaxed binding rules.

In this example, I adhered to the camelCase convention, but you also could have defined email.senderEmail as email.sender-email (using a hyphen) or email.sender_email (using an underscore) and would have been fine as well thanks to Spring Boot.

Using the configs in code

Now that we have the bean EmailConfig, we can use it in our Java application as follows:

Note that for simplicity’s sake, this is just an example where we are testing that the configuration set-up works by printing out the values into our console. In your real application, you would then be able to use those variables like you would hard-coded ones in order to carry out the logic of sending e-mails.

External configuration is absolutely crucial when it comes to reducing manual steps and unnecessary code changes and deployments, and it’s a key feature of Spring Boot that needs to be taken advantage of.

I hope that you have a better idea of how to do it with this specific approach, and for further reading I would strongly recommend reading the Spring Boot documentation on this subject.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Tremaine Eto

Written by

Full-time software engineer since 2016. UCLA Computer Science B.S. with Atmospheric and Oceanic Sciences minor, class of ‘16.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store