Ah, no, that's exactly what I was after for my use-case, thanks Matěj Novotný. As a quick summary of what I'm doing if you are interested:
- I write integration tests for a CDI based framework
- In my tests I define beans with which I test the functionality
- I must not have all beans in the container, but only the ones I need for the specific test
- Marking them as @Alternative and then using SeContainerInitializer#selectAlternatives does not work, as they would only be enabled in the synthetic bean archive unless I disable archive isolation
- Using @Priority to enable the alternatives globally also doesn't work, as this requires compile-time definition of what should be enabled and I need it at runtime
So my current tactic was (and is, just with the new method you told me about) to have all such beans annotated with @Alternative and then in the custom CDI extension add the types I need to the container. So now I do
beans.each {
beforeBeanDiscovery
.addAnnotatedType(it, null)
.remove { it.annotationType() == Alternative }
}
as addAnnotatedType(AnnotatedType<?>, String) does not return an AnnotatedTypeConfigurator where I could remove the @Alternative annotation. This works fine so far (except you see another flaw in this now I missed). |