Spring Profiles
Spring Profiles
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.
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. }
==========================
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
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) How properties files are provided from outside of our project (or) jar file?
- External Config (Spring Cloud Config Server)
Q) Can we define multiple profiles data in single properties file like YAML file?
- As of now NO.
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. }