[go: up one dir, main page]

0% found this document useful (0 votes)
47 views9 pages

Spring Profiles

The document discusses application environments in software development projects and how Spring profiles can be used to manage configuration for different environments. It describes how to define Spring profiles using properties files or YAML, and how to activate a profile when running an application. It also discusses using the @Profile annotation to execute code only in certain environments.

Uploaded by

Java Pro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views9 pages

Spring Profiles

The document discusses application environments in software development projects and how Spring profiles can be used to manage configuration for different environments. It describes how to define Spring profiles using properties files or YAML, and how to activate a profile when running an application. It also discusses using the @Profile annotation to execute code only in certain environments.

Uploaded by

Java Pro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

What is Application Environment?

- Application environment means, A complete setup to run application. (Linux VM, DB


Server, Log Server)
- Application Environments are used to test our application before delivering to client
Every Project will have multiple Environments in the Realtime like-
 Dev Env: Developers will use it for code Integration testing.
 SIT(QA) Env: Testers will use it application end to end testing.
 (User Acceptance tasting) UAT Env: Client / Client-Side Team will use it for acceptance
testing.

Note: In UAT client / client-side team will decide GO or NO-GO.


GO: Approved for Production deployment
NO-GO: Denied for Production Deployment
 PILOT Env: Pre-Prod Env for testing with live data.
 PROD(Production) Env: Live Environment (end users will access our prod app).

Every Environment will have its own Database and every database will have separate
configuration properties. If we want to deploy our code into multiple environments then we
have to change configuration properties data in application.properties file for every
deployment. (It is not recommended). To avoid this problem, we will use Profiles in Spring Boot.

What is Spring profile?


- Spring Boot profiles are used for loading Application environment specific Configuration
input file by the Spring container.
- Two types of input file in Spring boot
 .properties
 .yml/yaml

How to define profile in .properties file?


- Application<-profileName>.properties
- Application.properties (default)
- Application-dev.properties
- Application-uat.properties
- Application-qa.properties
Note: Loading Input file based on profileName like uat,qa,dev etc.

======================= Coding Example Using .properties file


=======================

Spring Bean:
1. import org.springframework.boot.context.properties.ConfigurationProperties;
2. import org.springframework.stereotype.Component;
3.
4. import lombok.Data;
5.
6. @Component
7. @Data
8. @ConfigurationProperties("my.app")
9. public class DBConnection {
10. private String driverName;
11. private String url;
12. private String userName;
13. private String password;
14. }

Starter Class with Runner


1. package org.nadim;
2.
3. import org.nadim.entity.DBConnection;
4. import org.springframework.beans.factory.annotation.Autowired;
5. import org.springframework.boot.CommandLineRunner;
6. import org.springframework.boot.SpringApplication;
7. import org.springframework.boot.autoconfigure.SpringBootApplication;
8.
9. @SpringBootApplication
10. public class Application implements CommandLineRunner{
11.
12. @Autowired
13. private DBConnection con;
14.
15. public static void main(String[] args) {
16. SpringApplication.run(Application.class, args);
17. }
18. @Override
19. public void run(String... args) throws Exception {
20. System.out.println(con);
21. }
22. }
Properties files
========================
1. application.properties (Default)
========================
1. my.app.driverName = oracle.jdbc.OracleDriver
2. my.app.url = jdbc:oracle:thin:@localhost:1521:orcl
3. my.app.userName = System
4. my.app.password = Oracle19

==========================
2. application-qa.properties
==========================
1. my.app.driverName = MySQLDriver
2. my.app.url = jdbc:MySQL:@localhost:1521
3. my.app.userName = root
4. my.app.password = root

===========================
3. application-uat.properties
===========================
1. my.app.driverName = PoSTGress
2. my.app.url = jdbc:PST:@localhost:878
3. my.app.userName = root
4. my.app.password = root

How to run profile?


- We can run 3 ways

1. Using Spring Boot Tab (Only STS IDE)


> Right click on Main class >> Run as >> Run Configuration
> Spring Boot tab > Profiles: Change to any dropdown option
> Apply > Run
2. Using External Arguments
> Right click on Main class > Run as > Run Configuration
> Arguments tab > Under Program Arguments enter this
--spring.profiles.active=qa
> Apply > Run

3.*** Build Application (Code ----> .jar/.war)


> Right click on Project > Run as > Maven Clean > next time
> Maven install (Wait for Build success message)
> Right click on target folder (then refresh)
> Choose properties
> Copy location (or) click explorer symbol to open that location
> Open that location in cmd prompt
> now execute command:
java -jar <jarname>.jar --spring.profiles.active=qa
java -jar <warname>.war --spring.profiles.active=qa

Note:
- If a key is not found in our current profile (ex: prod) then container reads them from
default profile(falling back) application.properties
(or)
- If duplicate key=val are exist in our profile (ex: dev) just remove or comment them , they
are loaded from default profile.
Q) Which profile is loaded if we did not specify any while running app?
- Default profile : application.properties

Q) I want to activate a profile which is not present?


--spring.profiles.active=sample (sample is not exist)

- Default profile : application.properties is loaded

Q) If few keys are not found in our current profiles?


- (Ex: prod is current profile) Container will try to load keys-val from default profile for keys which
are not found in current profile.

Q) How properties files are provided from outside of our project (or) jar file?
- External Config (Spring Cloud Config Server)

Q) How to run Jar file using CMD?


- Java -jar <jarName>.jar

Another way to config profile**


- active profile inside default application.properties file
Profiles using YAML

1. Using Multiple YAML Files (same process as properties file)


- application-{profileName}.yml
- application-dev.yml
- application-qa.yml
- application-uat.yml

2. Using Single YAML File (3 dash symbols)


- Separate every application environment specific configuration by using 3 dash(---)
symbols.
Syntax:

Example: ------------------------------- Application.yml ------------------------------------------------------


1. spring:
2. config:
3. activate:
4. on-profile:
5. - qa
6. - dev
7.
8. my:
9. app:
10. driver: OracleDriver
11. url: Oracle@localhost
12. userName: System
13. password: Oracle19
14.
15. ---
16. spring:
17. config:
18. activate:
19. on-profile:
20. – prod
21. my:
22. app:
23. driver: MySqlDriver
24. url: Mysql@localhost
25. userName: root
26. password: root
27.
28. ---
29. spring:
30. config:
31. activate:
32. on-profile:
33. - uat
34. my:
35. app:
36. driver: PostGressDriver
37. url: PostGress@localhost
38. userName: tiger
39. password: root
40.
41. ---
42. my:
43. app:
44. driver: Defaultdriver
45. url: Default@localhost
46. userName: default
47. password: root
48.

Q) Can we define multiple profiles data in single properties file like YAML file?
- As of now NO.

Q) What is VM/JVM arguments (or -Dkey=val) in Java?


Q) What is application arguments / command line args
/ main method inputs / String[] ?

Ex: -Dspring.profiles.active=qa
@Profile Annotation
@Profile:
- To execute a logic (create obj/call method) on a selected environment, we can use this.
- We can use @profile annotation over the methods and Classes.
Example: I Want to inform spring container that create object EmailConfig, DbBackService
classes only at Prod Environment.

1. @Component
2. @Profile("prod")
3. public class EmailConfig {
4. //logic
5. }

Case analysis:
 No @Profile annotation: Logic will be executed in all environment.
 @Profile("default"): Execute only in dev environment. If I do not select any environment
profileName.
 @Profile("profileName"): Execute in given profile env.
Example: @Profile("prod"), @Profile({“dev”,”qa”,”uat”})

Note:
- If we did not provide any profile name while running application then Spring Container
selected profile "default"
- @Profile is independent of properties/yaml. If a specific profile proeprties/yml file is not present
in code/workspace then container selected default one application.properties/application.yml.

Q) How can we inform container to create selected classes objects only in Prod
Environment?
- use @Profile over Component or Bean.
Example#01

1. @Component
2. @Profile("prod")
3. public class EmailConfig {
4. //logic
5.}
Example#02: @Profile over the Bean

1. package org.nadim.config;
2.
3. import org.nadim.entity.DbConnection;
4. import org.springframework.context.annotation.Bean;
5. import org.springframework.context.annotation.Configuration;
6. import org.springframework.context.annotation.Profile;
7.
8. @Configuration
9. public class AppConfig {
10. @Bean
11. @Profile({"dev","default"})
12. public DbConnection objCreation() {
13. DbConnection con = new DbConnection();
14. con.setDriver("OracleDriver");
15. con.setUrl("jdbc:Oracle");
16. return con;
17. }
18. }

You might also like