In my Java EE 7 program, I want to use @Alternative
to inject different implementation depending on the context, production or test for example. What I did is to declare my class annotated with @Alternative
in my beans.xml file. It works great and my alternative class is injected wherever I want instead of the default one. But I don't know if there is a way to skip this behavior and inject the default class other than removing the declaration in the beans.xml file. Which is not possible easily when the application is packaged. It would be great if I can choose if I want to use the default classes or the alternative ones in a configuration file, for example in my standalone.xml file of my WildFly server. Is this possible?
In my opinion the simpliest solution was to create a maven profile like it's mentionned in some comments.
In my pom.xml file, I added a resources filtering section and a profile:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>default</id>
<properties>
<MyBean></MyBean>
</properties>
</profile>
<profile>
<id>alternative</id>
<properties>
<MyBean><![CDATA[<class>com.MyBean</class>]]></MyBean>
</properties>
</profile>
</profiles>
The beans.xml file is now like that:
<beans ...>
<alternatives>
${MyBean}
</alternatives>
</beans>
Finally I just need to execute the maven command with the useful profil: mvn package -P alternative
.