DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • JavaFX Goes Mobile
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • Execution Type Models in Node.js
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Stop Using Spring Profiles Per Environment

Stop Using Spring Profiles Per Environment

This article discusses Spring's feature, Profiles, which some may consider a bad practice. Learn an alternative way to solve this issue.

Bukaj Sytlos user avatar by
Bukaj Sytlos
·
Mar. 24, 23 · Code Snippet
Like (3)
Save
Tweet
Share
5.68K Views

Join the DZone community and get the full member experience.

Join For Free

Lately, I've come across a lot of discussions and articles about Spring's feature called Profiles that are promoting them as a way to separate environment-specific configurations, which I consider a bad practice.

Common Examples

The typical way profiles are presented is by having multiple configuration files within the resources folder that will be bundled within the application artifact with application-prod.yml like:

Application Prod.


YAML
 
some-resource.address: prod-address
some-resource.username: prod-user
some-resource.password: prod-password


Issues

I hope one can immediately see some of the issues:

  • Applications' production credentials are committed and available for everyone with access to the repository, which is a very serious security issue.
  • Changing configuration value on a given environment would require recompiling and the creation of a new artifact.
  • The introduction of a new environment would require recompiling and the creation of a new artifact.

Recompilation and release of a new application version without really changing any application logic feels stupid.

Solution

How can this issue be solved?

Well, config values have to be put outside of the application's artifact and VCS repository as recommended by Twelve-Factor App.

There are at least two ways I have experience:

  1. Having a config file beside application.jar (or specifying spring.config.additional-location) on a given environment overrides only specific keys.
  2. Use environment variables.

In the latter case, config keys are bound with environment variables e.g. some-resource.username <=>SOMERESOURCE_USERNAME.

If a custom key name is needed, an "alias" can be made as:

YAML
 
some-resource.username: ${OTHER_ENV_KEY}


In either case, what's the need for config files per environment?

All that is needed is a single application.yml file with both internal and external properties required by the application.

These properties can have either empty or default/local values.

YAML
 
some-resource.address:
some-resource.username: username
some-resource.password: ${OTHER_ENV_KEY:123456}


When to Use Profiles

So far, I have never had a need for profiles per environment. 

However, there is one "special" profile that I would not consider an environment configuration file, and that is test the profile located in src/test/resources/. This profile and its corresponding configuration file allow overriding only present keys.

Having application.yml file in the given folder would require providing all config properties defined in the main file (if that's not an issue, go for it).

To activate this profile, use @ActiveProfiles annotation on test classes.

The only other usage of profiles I can think of is some optional feature config grouping.

One advantage of the profile's mechanism in configuration files is its ability to merge/override config properties (for details, check Piotr's TechBlog and his Github playground project). 

If we have some optional feature, which requires a separate set of config attributes that we want to be added only if this feature is active, we could have application-{feature-name}.yml, which can be activated in the main config file via spring.profiles.active/include properties.

For simple feature flagging, I would use @ConditionalOnProperty annotation.

Conclusion

To conclude, don't store environment-specific configuration inside the application and instead use externalized configuration managed/injected on a given environment.

Note: Please feel free to share your opinion and experience on the usage of profiles for environment-specific configuration files or in general. I might be missing something or made a mistake, and I would like to broaden my knowledge.

application Artifact (UML) Profile (engineering) Repository (version control) security Spring Integration

Opinions expressed by DZone contributors are their own.

Trending

  • JavaFX Goes Mobile
  • Why You Should Consider Using React Router V6: An Overview of Changes
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • Execution Type Models in Node.js

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: