2

In the @Profile annotation documentation there is a statement:

If a given profile is prefixed with the NOT operator (!), the annotated component will be registered if the profile is not active — for example, given @Profile({"p1", "!p2"}), registration will occur if profile 'p1' is active or if profile 'p2' is not active.

And in the following example MyRepoImpl will be created as a Spring bean when the uat profile is active:

@Repository
@Profile({"dev", "!prod"})
public class MyRepoImpl implements MyRepo {...}

In this case what is the reason for specifying profiles other than !prod (dev in this example)?

As once prod is not active that beans will be created. Is not it kind of excessive unnecessary configuration?

DimaSan
  • 12,264
  • 11
  • 65
  • 75
  • 2
    I'd argue that the only reason is for illustration purposes. 1. It illustrates that profiles are "OR" and not "AND" when used this way. 2. It also illustrates how "NOT" works. I've never found a use case for this scenario. – Jaron F Aug 11 '19 at 01:08

1 Answers1

1

@JaronF comment is correct in most cases, It's mainly illustrates how Profile works with NOT operator,

But in cases you define multiple active profiles, then it will be relevant

If you want a Bean to be registered only if Profile dev active and when both dev and prod are active

Ori Marko
  • 56,308
  • 23
  • 131
  • 233