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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Migration of Microservice Applications From WebLogic to Openshift
  • How to Use Java to Build Single Sign-on
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project

Trending

  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  • Debugging With Confidence in the Age of Observability-First Systems
  • The Cypress Edge: Next-Level Testing Strategies for React Developers
  • Spring and PersistenceContextType.EXTENDED
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Use Profiles in Spring Boot

How to Use Profiles in Spring Boot

Want to learn more about the features available in Spring Boot? Check out this post to learn more about how to use profiles in your Spring Boot application.

By 
Sovan Misra user avatar
Sovan Misra
·
Updated Sep. 07, 18 · Tutorial
Likes (42)
Comment
Save
Tweet
Share
473.5K Views

Join the DZone community and get the full member experience.

Join For Free

Spring Boot is gaining popularity like never before, and I know it will be a persistent player in the coming months. There are some features that every technology has and it is very useful in enterprise applications. In this post, we will talk about one feature in particular: profiles.

What Are Profiles?

Every enterprise application has many environments, like:

Dev | Test | Stage | Prod | UAT / Pre-Prod

Each environment requires a setting that is specific to them. For example, in DEV, we do not need to constantly check database consistency. Whereas in TEST and STAGE, we need to. These environments host specific configurations called Profiles.

How Do we Maintain Profiles?

This is simple — properties files!
We make properties files for each environment and set the profile in the application accordingly, so it will pick the respective properties file. Don't worry, we will see how to set it up.

This article will demonstrate how to setup Profiles for your Spring Boot application.

Let's start with setting up a Spring Boot application from the Spring Starter.

Screen Shot 2018-09-02 at 12.43.23 AM.png

Next, we need to import the project into STS as a Maven Project. Below is the project structure:
Screen Shot 2018-09-02 at 12.48.17 AM.png

In this demo application, we will see how to configure different databases at runtime based on the specific environment by their respective profiles.

As the DB connection is better to be kept in a property file, it remains external to an application and can be changed. We will do so here. But, Spring Boot — by default — provides just one property file ( application.properties). So, how will we segregate the properties based on the environment?

The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile.

Then, we need to create three  application.properties:

  1.  application-dev.properties 
  2.  application-test.properties 
  3.  application-prod.properties 

Of course, the application.properties will remain as a master properties file, but if we override any key in the profile-specific file, the latter will gain precedence.

I will now define DB configuration properties for in respective properties file and add code in DBConfiguration.class to pick the appropriate settings.

Here is the base  application.properties:

Screen Shot 2018-09-02 at 8.33.25 AM.png

In DEV, we will use an in-memory database:

Screen Shot 2018-09-02 at 8.33.58 AM.png

In TEST, we will be using a lower instance of RDS MySQL database, and in PROD, we will use a higher instance of the MySQL database. (It's the price that matters...)

Screen Shot 2018-09-02 at 8.43.08 AMScreen Shot 2018-09-02 at 8.43.23 AM

Now, we are done with properties files. Let's configure in the DBConfiguration.class to pick the correct one.

Screen Shot 2018-09-02 at 8.48.41 AM.png

We have used the @Profile("Dev")   to let the system know that this is the BEAN  that should be picked up when we set the application profile to DEV. The other two beans will not be created at all.

One last setting is how to let the system know that this is DEV, TEST, or PROD. But, how do we do this?

We will use the application.properties to use the key below:

spring.profiles.active=dev


From here, Spring Boot will know which profile to pick. Let's run the application now!

With the profile in DEV mode, and it should pick H2 DB.

Screen Shot 2018-09-02 at 9.05.26 AM.png

Screen Shot 2018-09-02 at 9.07.25 AM

Now, change the profile to PROD. We will see MySQL with High Config for DB. This should be picked, and the message will be overridden with the PROD message.

Screen Shot 2018-09-02 at 9.09.29 AMScreen Shot 2018-09-02 at 9.09.44 AM

That's it! We just have to change it once at the application.properties to let Spring Boot know which environment the code is deployed in, and it will do the magic with the setting.

Please visit this repository to access the code to learn more!

Spring Framework Spring Boot Profile (engineering)

Published at DZone with permission of Sovan Misra, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migration of Microservice Applications From WebLogic to Openshift
  • How to Use Java to Build Single Sign-on
  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!