"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Spring Boot - Profiles Date : 06/04/2025 (Session - 21) _____________________________________________________________________________________________________________________________ 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. * If there is no configuration file for activated profile 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] What we covered from Spring Boot -------------------------------- 1) Spring Boot Introduction - Features - Advantages & Disadvantages - Project Creation - Environmental setup - Internals of springboot 2) Spring Boot Runners - CommandLineRunner - ApplicationRunner 3) Working with Properties & YAML File 4) Spring Boot Layered Application 5) Spring Boot Profiles Spring Data =========== * This Module contains four sub modules 1) Spring Data JPA 2) Spring Data Jdbc 3) Spring Data MongoDB 4) Spring Data Redis (Upcoming Session) * Spring Data provides Abstraction on Relational Databases & Non-Relational Databases 1) Relational Databases >>>>>> The data which we can have in structured format (Tables(Rows,Columns)) Example : Oracle,MySQL,Postgresql,DB2,SQLServer etc., * Relational Databases are also called "SQL Databases". 2) Non Relational Databases >>>>>> The data which doesn't have any proper Structure(Documents,Key-value,graphs,columns) Example : MongoDB,Redis,Neo4J,Apache Cassandar * Non-Relational Databases are also called "NoSQL Databases". For More Information about Spring Data Module i.e.,https://spring.io/projects/spring-data * Typically every Java Application contains Multiple logics 1) Presentation Logic/Presentation Layer 2) Business Logic/Service Layer 3) Secondary Logic/Intermediate Layer 4) Persistence Logic/Persistence Layer * By Using Spring Data Module we can build the "Persistence logic". * The logic which interacts with Database Software for Storing & Retrieving information is called "Persistence Logic". * Earlier If Java Application wanted to communicate with Database Software we used Core technology "JDBC". * We have some drawbacks with Jdbc Technology 1) Explictly programmer need to get the connection object & closing Connection Object in Java Jdbc Application. 2) All the Exceptions are in Jdbc Technology i.e.,Checked Exception using try,catch blocks or throws keyword. 3) While performing the Database operations such as insert,update,delete internally data will transferred in the form text. Ex: === Connection con = DriverManager.getConnection("JdbcURL","Databaseusername","DatabasePassword"); PreparedStatement pstmt = con.prepareStatement("insert into ashokit_customers values(?,?,?)"); pstmt.setInt(1,1020); pstmt.setString(2,"Ashok"); pstmt.setString(3,"Hyderabad"); 4) Writing the SQL Query and table need to be created manually when we working with Jdbc Technology. 5) ResultSet Object is not Serializable object by default.so to send the data over network we need convert the ResultSet object into another Collection object. 6) We need to write lot of boiler plate code (Repeation code). 7) Changing the database software of the project is very difficult process. 8) Whatever the queries we are writing in the Jdbc Application all queries are "Database Dependent Queries". * Inorder to overcome the problems of Jdbc Technology third party vendors provides some abstraction on Jdbc i.e.,ORM Frameworks