In a Spring-Boot project, I use @ConditionalOnProperty
to choose whether some Beans get loaded or not. It looks like the following:
@ConditionalOnProperty(
prefix = "myservice",
name = "implversion",
havingValue = "a"
)
@Service
public class MyServiceImplA implements MyService {
// ...
}
This allows me to choose with specific profiles which Bean should be loaded, for example different implementations of an interface, depending on the value of myservice.implversion
being a
or b
or whatever other value.
I'd like to achieve the same effect with a user-friendlier annotation like such:
@OnMyServiceVersion(value = "a")
@Service
public class MyServiceImplA implements MyService {
// ...
}
How can one do this?
I've tried annotating my custom annotation with @Conditional
and implementing the Condition
interface but I don't understand how to check properties that way. The Spring-Boot OnPropertyCondition extends SpringBootCondition
is not public
so I cannot start from there, and extending annotations isn't allowed, so I'm kind of stuck.
I've also tried the following with no success:
// INVALID CODE, DO NOT USE
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ConditionalOnProperty(
prefix = "myservice",
name = "implversion",
havingValue = OnMyServiceVersion.value()
)
public @interface OnMyServiceVersion {
String value();
}