Introduction
In this tutorial iam providing you complete guide on profiles of Spring Boot. We will see what is the use of profiles, how to set different properties for each profile and bean configurations on each profile.Profiles can help us to set different properties and different bean configurations. There may be different profiles like development, production, testing etc..Each profile has its own activities in the application.
What is profile?
Profile is an identity setting to different environment of application execution, for example an application can be deployed in different environments like development, staging, testing and production, each environment of application requires different configurations of properties and beans.Profile sets possibility to arrange/provide different configuration properties and beans to be injected by spring container.
How to create different configuration properties for different profiles?
Let us consider there are two profiles ( to make explanation simple iam working with two profiles only) named as demo and live. We need to create different configurations for these profiles, to do this, we need to create two .properties files named as application-demo.properties and application-live.properties.Syntax: application-profileName.properties
msg=Welcome to demo site
msg=Welcome to Live site
Using @Profile annotation for creating profile specific beans
In some situations while developing applications we need to inject beans based on active profile, in this cases we can use @Profile annotated bean which will be injected based on active profile.Do not get the confusion : properties will be accessed by any bean of the application, but that properties will be taking from profile specific .properties file only, but profile speciifc bean will be injected based on active profile.
@Component @Profile("demo") public class DemoBean { @Value("${msg}") String message; public String getMsg() { return message; } }The above bean will be injected at the time application booting when active profile is demo.
@Component @Profile("live") public class LiveBean { @Value("${msg}") String message; public String getMsg() { return message; } }The above bean will be initiated when the active profile is live.
Let us consider active profile is live, then guess what returns by getMessage() method of the following bean Message.java
@Component public class Message { @Value("${msg}") String message; public String getMsg() { return message; } }Notice that in the above class we are not using @Profile annotation, that means this Bean will be initiated for all profiles, and current profile is live , so getMessage() method returns Welcome to Live site.
If active profile is demo then getMessage() method will return Welcome to demo site.
Using @Profile with @Bean and @Configuration annotations
As we know bean can be crated/initiated by using @Bean annotation at method live in @Configuration class. Here we can use @Profile annotation at method level ( together with @Bean annotation ).@Configuration public class AppConfig { @Bean @Profile("demo") Bank bankBean() { return new Bank(); } }
Post a Comment
Post a Comment