EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot Profiles
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Spring Boot Profiles

Spring Boot Profiles

Definition of Spring Boot Profiles

While creating applications in spring boot we have different stages in the development and they often treated as dev, test, and prod. This helps us to have a different profile for every environment, we use @profile annotation to implement this in the spring boot application also. By the use of setting different profiles, we can set different seating for each environment without affecting the other environment. This is very useful by the use of it we can use the local, development environment the way we want. In the coming section of the tutorial, we will see what are the steps needed to set up this in our spring boot application. Also, we will look internally at how it works and implement it for beginners to understand it better.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

As we already know that we have to make some configuration in order to use this in spring boot, it is all related to making configuration only. We can have the properties set into the application.properties file. Let’s take a closer look at the syntax for beginners to use understand it better see below;

@Profile(value=" your profile name profile")

As you can see we have used @Profile annotation and inside it, we have provided the name for our profile. Let’s take a closer look at the sample piece of syntax for beginners which is easy to understand better see below;

e.g. :

@Profile(value="local")

The above piece of syntax will active the local profile for us. In the coming section of the tutorial, we will see how to use and configure it fully to use this while application development.

How profiles work in Spring boot?

In this section, we will see how we can use this while programming in spring boot, also the internal working of the profile setting in spring boot. As we now know that profiles are very important when it comes to set the different setting for every environment we have because the development of the spring boot includes different environment such as prod, dev, test and other if we have. This is important because we cannot have the same database and many more things with every environment we have, it also prevents us to make any conflict on the prod database also. In this section first, we will see how we can start setting up the profiles in the spring application see below;

Let’s get started;

1) The first step we need to make the application file for every environment we have. In spring boot we have to follow some standard given by it, we can create the application file in the below format;

application-{profile}.properties

As you can see you can create the application file like above, in the profile part you can mention the name of the profile you want.

2) Inside the application file we can give the name for the profile by using the key and properties defined by the spring boot application. Below see the syntax of how we can do this in our application see below;

e.g. :

spring:
application:
name: DemoProfiles
profile: local

3) For the name part of the application properties file we can have this in place see below;

a) application-local.properties: This properties file will be responsible to add the set of the environments for local.

b) application-dev.properties: This properties file will be responsible to add the set of the environments for development.

c) application-prod.properties: This properties file will be responsible to add the set environment for production.

d) application-test.properties: This properties file will be responsible to add the set of the environments for the test.

4) Now we will see how to make any profile active, using the application properties file in spring boot. To make any profile active in the application we have to make changes in the properties file commonly we have. Where we can set the profile we want to use for the current purpose. Below we will see how we can make use of this property we can make the necessary change;

e.g. :

spring. profiles.active=your_prfile_name

As you can see in the above line of syntax we are using the profile active property from the spring boot to make the profile active in the application. Let’s taken a look at the sample syntax for beginners to make use of this better see below;
e.g. :

spring. profiles.active=dev

Let’s take look at the steps need to perform while configuring the profile into the spring boot appellation, which is as follows

1) First we have to create the spring boot application from scratch by using a spring initializer after that we can import the change into the editor.

2) We do not need to add any specific dependency for this because spring boot provides this in the basic structure.

3) While using the annotation required for this we have to have an import statement at the place otherwise we will receive an error.

4) Let’s take a closer look at the file;

e.g. :

@SpringBootApplication
public class DemoProfileApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(DemoProfileApplication.class, args);
}
@Configuration
public class Config {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
@Profile({"dev"})
public void configDev() throws IOException {
// logic goes hee ..///
}
@Bean
@Profile({"prod"})
public void configProd() throws IOException {
// logic goes hee ..///
}
}
}

5) Make changes to the application.properties file like below;

e.g. :

spring. profiles.active=local

6) Make this necessary changes into the spring boot application and try to run it.

7) Right-click on the application then you will see below logs on your console when the application is up and running, for reference, I am attaching the screenshot;

Output:

spring boot profiles

Conclusion

By the use of profiler on the spring boot application we can make the different environut settings as per our need and run the application local without impacting any changes to the production environment data. so it is a best practice to use profiling in the application. This is a standard followed by the developers to make the best use of the application.

Recommended Articles

This is a guide to Spring Boot Profiles. Here we discuss definition, syntax, and parameters, How do profiles work in Spring boot? examples with code implementation. You may also have a look at the following articles to learn more –

  1. Spring Boot DevTools
  2. Spring Boot Actuator
  3. Spring Boot Starter Web
  4. Spring AOP
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more