This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MultiProfileExtension implements Extension { | |
private Set<String> activeProfileNames = new HashSet<>(); | |
protected void determineActiveProfiles(@Observes BeforeBeanDiscovery beforeBeanDiscovery) { | |
String activeProfileString = | |
ConfigResolver.getPropertyValue("active-profiles", "Production"); | |
Collections.addAll(activeProfileNames, activeProfileString.split(",")); | |
} | |
protected void matchActiveProfiles(@Observes ProcessAnnotatedType pat, BeanManager bm) { | |
Set<Annotation> annotations = pat.getAnnotatedType().getAnnotations(); | |
Profile profile = AnnotationUtils.findAnnotation( | |
bm, annotations.toArray(new Annotation[annotations.size()]), Profile.class); | |
if (profile == null) { | |
return; | |
} | |
for (String supportedProfile : profile.value()) { | |
if (activeProfileNames.contains(supportedProfile)) { | |
return; | |
} | |
} | |
pat.veto(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Profile("X") | |
//further cdi annotation/s | |
public class BeanProfileX { | |
} |
a type-safe approach based on cdi-stereotypes is supported as well. the usage of the type-safe approach is also straightforward:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ProfileA | |
//further cdi-annotation/s | |
public class BeanProfileA { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
@Stereotype | |
@Profile("A") | |
public @interface ProfileA { | |
} |
the implementation including tests are available at ds-multi-profile-addon.