A profile can be describe as a set of properties and configuartion settings. An application can have multiple profiles which we may use to execute in different environments, like Dev, Stage, Production. Also, we can have different databases for different profile for a particular application.
In Spring Boot, we create a different properties file for every new profile. We can control the active state of these profiles in application.properties file, which is the master properties file.
Note: If more than one profile is specified, then the last one will gain precedence.
Also Read: Spring Boot Data Rest
Create a properties file for a profile: Define file name as - application-{profile}.properties
Example- application-Dev.properties
spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1 spring.datasource.username=root spring.datasource.password=root
Specify profile in application.properties file
Here, in spring.profile.active property, we mention the profile name that we want to active
spring.profile.active=Dev spring.application.name=Profiles
Next step is to configure database in DBConfiguration.java
@Profile annotation is used to set a bean to a defined profile
@Configuration @ConfigurationProperties("spring.datasource") public class DBConfiguration{ private String driverClassName; private String url; private String username; private String password; @Profile("Dev") @Bean public String devDatabaseConnection(){ return "Dev database connection"; } }
Note: If we user @Profile annotation as @Profile(!Dev) then this specify that this bean does not belong to Dev. And if a bean does not specify a profile then it belongs to a default bean.
Also Read: Spring Boot Application Configured As Eureka Server
That's it! This is is how you create a profile and set it's status active.
Why You Should Choose Oodles For SaaS Product Development?
We are a 360-degree software development company that provides cross-platform SaaS app development services to address varied software project requirements. We have an experienced team of Java, PHP, and Python developers who use advanced frameworks, tools, and SDKs to build scalable web and mobile applications with custom features. For more detail, reach us out at [email protected].