Spring Profiling

Nikhil Bhatnagar
3 min readJan 12, 2022

Over the years, spring framework has developed a lot. From, providing support for simple API’s to develop large scale microservices, Spring provides a huge scale of capabilities. Moreover, spring applications can easily be dockerized and scaled up to large extents. Now, suppose we work on various environments/parts having requirements which are not similar to each other or may vary slightly depending upon the demand. Suppose a feature A runs with some modification or some encryption whereas the feature B needs decrypted data or mysql data base or an encryption key to run. Similarly, there can be many features which require separate environment to run with or which may require different keys, databases or external connectivity.

Generally we shove these thing into the applications file or config files and represent/use then with the environment variables. This can be a bad practice because the properties file is probably a manual effort to set up things and is practically out of user’s programmatic control. Further, during deployment the code needs to wait for the environment variables to set up or to be entered manually. Thus, a better way was required and spring came up with the option of Spring Profiling.

Photo by Tracy Adams on Unsplash

Profiles is a feature to help us and set up different values for different configurations. Interestingly profiles are always used in spring, irrespective of us setting it up or not and by default, spring uses “default” profile. To create a new profile:-

· Create a new file in the root of the project. This file will be our placeholder for the new profile. The naming convention of the file should be as follows:-

application-<profile-name>.extension

Here, we’ll create a yml file.

· Put the appropriate configurations we need to as per our requirements.

· Inside application.properties or application.yml and add the following line to make any needed profile active:-

spring.profiles.active: PROFILE_NAME

Note: The PROFILE_NAME above should be the name, without the standard “application” prefix. Eg:- For a yml file with the name application-first.yml, the PROFILE_NAME would be first.

Apart from choosing data source values or configuration values, profiles also help with selecting beans. So, to use these beans via profiling, just add @Profile(“Your_profile_name”). The bean would automatically be picked up at the time of running the application and would be instantiated simultaneously. Suppose, we don’t add any @Profile annotation in our bean. Then in that case the the default profile kicks in and loads the bean for us. Programmatically: -

@Profile(“default”) is equivalent to no usage of @ Profile.

Profiling in spring works in a hierarchical way. The bottom-most or the lower tier/base profile is the default profile. All the other profiles are built over it (as shown in fig 1.0). The lower profile can’t be overridden by the above, new profile and the properties of the bottom one can be used further up the hierarchy.

Fig 1.0

Hope you found it informative. If you want similar articles based on any java or node/js based technology, please feel free to share that in the comments. I’ll be happy to dig into and write something similar for the same.

Thanks for reading :)

--

--

Nikhil Bhatnagar

A tech enthusiast who loves to read and write about new technologies and trends. Software engineer @HashedinByDeloitte