Finally! Smart profiling in Spring 5.1 🙌🏼

Finally! Smart profiling in Spring 5.1 🙌🏼

The day has come!

Spring 5.1 release a smart profiling mechanism that allows you to use logical expressions to express how your @Components are going to become active.

If you're a Spring and Spring Boot fan like me, you probably had your nightmare times with profiling.

Before Spring 5.1

Previously in Spring you could define profiles for your application by negating and composing with a logical OR operator. Let's take a look at this example:

@Component
@Profile({"dev","docker"})
public class MyComponent{

In this case, MyComponent will become active if either dev OR docker profiles are active. You could also negate a profile like in the following example:

@Component
@Profile({"!dev"})
public class MyComponent{

In that case the component becomes active when anything that it is NOT dev is activated. Which can bring some headache because from the moment you start using multiple profiles this will get activated by accident and you'll have to readjust all of your profiles to make it work as you expect.

In Spring 5.1

Now you are able to compose profiles by using AND(&) and OR(|) operators inside of the profile string.

@Component
@Profile({"!dev & generate_events"})
public class MyComponent{

This component will get activated when dev is not active AND generate_events is for example. That opens a whole new world of possibilites for Spring devs without having to resort to things like custom Condition's.

Happy coding everyone! 🤓


Awesome, dude! Thanks for letting me know 😃

To view or add a comment, sign in

More articles by Kirill Lassounski

Explore content categories