A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

profiles in spring boot

profiles in spring boot

Introduction: Profiles in spring?

Do not be confused with the name profiles; it is not going to relate you from Facebook profile and Instagram profile or any other social-media web app profile.

What is profiles in spring boot?

In the spring framework, it is the name of environment like dev, sit and prod. Profiles are a core feature of the framework that allows mapping beans to different profiles like as discussed above: dev, test, prod.

What will be the benefit of profile?

Java developers can activate different profiles in different environments to bootstrap by using the spring beans.

What is @profile?

If Java developer wants to define a bean as a profile that may be a dev, sit or prod environment, then they have to use @profile annotation that spring boot has provided. Let us assume a scenario that we have a bean and we want to be active only in development but it should not be present after deployment in production server. To do this task, you have to follow this annotation, the bean should be annotated with dev profile and it will be seen in the container during development and in production but the dev profile will not be active.


@Component
@Profile("dev")
public class DevDatasourceConfig

How to exclude the profile in spring boot?

Let us assume again the above scenario and think if you are a little bit afraid of java @profile and thinking that it may cause in a production environment after deployment, if it gets loaded the dev environment, then you can exclude it by using simple NOT operator. The profile names can also be prefixed with a NOT operator e.g. “!dev”. The component will be activated only if “dev” profile is not active:

@Component
@Profile("!dev")
public class DevDatasourceConfig

Download SourceCode

LEARN TUTORIALS

.

.