禁用@Alternative类 [英] Disable @Alternative classes

查看:189
本文介绍了禁用@Alternative类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Java EE 7程序中,我想使用 @Alternative 来注入不同的实现,具体取决于上下文,生产或测试。我所做的是在我的beans.xml文件中声明我的类用 @Alternative 注释。它工作得很好,我的替代类被注入到我想要的任何地方,而不是默认的。但我不知道是否有一种方法可以跳过此行为并注入默认类,而不是删除beans.xml文件中的声明。打包应用程序时,这是不可能的。如果我可以选择是否要在配置文件中使用默认类或备用类,例如在我的WildFly服务器的standalone.xml文件中,那将是很好的。这可能吗?

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?

推荐答案

在我看来,最简单的解决方案是创建一个maven配置文件,就像在一些评论中提到的那样。

In my opinion the simpliest solution was to create a maven profile like it's mentionned in some comments.

在我的pom.xml文件中,我添加了一个资源过滤部分和一个配置文件:

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>

beans.xml文件现在是这样的:

The beans.xml file is now like that:

<beans ...>
    <alternatives>
        ${MyBean}
    </alternatives>
</beans>

最后我只需要用有用的profil执行maven命令: mvn包-P替代

Finally I just need to execute the maven command with the useful profil: mvn package -P alternative.

这篇关于禁用@Alternative类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆