Passing a profile as a parameter depends on how you run your app. Be careful, the doc you mentioned is referring to the maven spring-boot plugin
.
- With maven plugin :
mvn spring-boot:run -Dspring-boot.run.jvmArguments=-Dspring.profiles.include=MASTER
- Classic java app :
java -Dspring.profiles.include=MASTER -jar ./myapp.jar
In both cmd line, you can pass more than one parameter, if separated by a ,
. See the documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config-profile-specific-properties
Since the upgrade, you now have to define your custom profile annotation like this :
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) // Only this one is really needed
@Profile("SLAVE")
public @interface SlaveProfile {
}
Explaination:
In java, an annotation has a RetentionPolicy, which is similar to a scope. (See this: https://docs.oracle.com/javase/7/docs/api/java/lang/annotation/RetentionPolicy.html).
Without any RetentionPolicy set, the default behavior is an annotation not visible for the JVM (i.e at runtime).
When you want to run your application, you first compile it, which implies converting your .java
files into .class
file. Your class is only a bunch of byte code, converting your human readable file into a computer language.
Then, when Spring is loading the ApplicationContext
, what it does under the hood, among many other things, is reading your .class
files. During this process (see class name: org.springframework.asm.ClassReader
) Spring loads the annotations that you declare. With what I've said above, during the Runtime, you end up with "two kinds" of annotations :
- InvisibleAnnotation: @Retention(RetentionPolicy.COMPILE)
- VisibleAnnotation: @Retention(RetentionPolicy.RUNTIME)
To conclude and understand why it was working before:
Spring-boot 2.1.0
uses spring-core-5.1.2
, which interprets at runtime the visible and invisible annotations, which explain why your @SlaveProfile
and @MasterProfile
have the expected behaviour.
Spring-boot 2.2.0
uses spring-core-5.2.0
, which interprets at runtime ONLY the visible annotations, which explain why your @SlaveProfile
and @MasterProfile
haven't the expected behaviour.
Let's say that Spring "silently" fixed a bug that was reading Invisible Annotation when they shouldn't, but didn't mention it.
Hope it helps!