"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot - Layered Application Date : 26/09/2024 (Session - 33) _____________________________________________________________________________________________________________________________ Spring Boot Profiles ==================== * Spring Boot Profiles are a feature provided by the Spring Framework and Spring Boot that allows you to define and configure application environments for different deployment scenarios. * Profiles help you customize the behavior of your Spring Boot application based on the specific environment it is running in,such as development, production, testing, or any other custom profile you define. * With profiles, you can have different configurations, beans, and properties activated based on the active profile. This allows you to keep your application codebase unchanged while providing different settings and behaviors depending on the deployment environment. * Here's how you can work with Spring Boot Profiles: 1) Defining Profiles ==================== * We can define the each property file/YAML File for each environment with following syntax "application-.properties/application-.yml" i) application-dev.properties >>>>> Dev environment Configurations ii) application-qa.poperties >>>>> QA environment Configurations. iii) application-preprod.properties >>>>> preprod environment Configurations. iv) application-prod.properties >>>>> prod environment Configurations. application-dev.properties ========================== database.username=ashokit_dev database.password=ashokit_dev application.emails=dev.ashokit@gmail.com application-qa.properties ========================== database.username=ashokit_qa database.password=ashokit_qa application.emails=qa.ashokit@gmail.com application-preprod.properties ============================== database.username=ashokit_preprod database.password=ashokit_preprod application.emails=preprod.ashokit@gmail.com application-prod.properties =========================== database.username=ashokit_prod database.password=ashokit_prod application.emails=ashokitschools@gmail.com * In your Spring Boot application, you can define profiles by using the @Profile annotation on classes or methods. Example ======= @Configuration @Profile("dev") public class DevelopmentConfig { // Configuration specific to the development profile } @Configuration @Profile("prod") public class ProductionConfig { // Configuration specific to the production profile } ------------------------------------------------ @Configuration public class Config{ @Bean @Profile("dev") public DataSource getDataSourceObj(){ DriverManagerDataSource dmd = new DriverManagerDataSource(); dmd.setDriverClassName(MySQLJdbcDriverClassName); dmd.setUrl(MySqlJdbcURL); dmd.setUsername(MySqlUsername); dmd.setPassword(MySqlPassword); } @Bean @Profile("prod") public DataSource getDataSourceObj1(){ DriverManagerDataSource dmd = new DriverManagerDataSource(); dmd.setDriverClassName(OracleJdbcDriverClassName); dmd.setUrl(OracleJdbcURL); dmd.setUsername(OracleUsername); dmd.setPassword(OraclePassword); } } 2) Activate Profiles ==================== * Using the "spring.profiles.active" property in the application.properties or application.yml file spring.profiles.active=dev * Using the System Property i.e.,commandLine Arguments -Dspring.profiles.active=dev * Programmatically activating profiles using the SpringApplication class: SpringApplication.setAdditionalProfiles("dev"); NOTE ==== * If there is no profile activated in Spring Boot Application then it will fall into "default" profile. IIQ) Is it possible to activate multiple profiles at a time ? ---> Yes we can activate it with following line of code i.e.,spring.profiles.active=prod,qa It is not recommended approach for activating the multiple profiles in spring boot application Example OUTPUT =============== The following 2 profiles are active: "prod", "qa" ProdConfig Class Constructor ashokit_qa=====ashokit_qa=====qa.ashokit@gmail.com Activated Profile Name :::::[prod, qa]