【问题标题】:Glassfish 4 CDI Producer and Alternative with libraryGlassfish 4 CDI Producer and Alternative 带库
【发布时间】:2015-08-26 07:09:36
【问题描述】:

Java EE Web 应用程序正在使用一个库,该库实现了一个带有注入对象的类。

首先,我在提供部分依赖对象的库中声明一个默认生产者。是否可以使用 @Alternative 注释在 Web 应用程序中声明的另一个生产者覆盖该生产者?

其次,我声明了一个使用@Default 注解实现接口的类。在 Web 应用程序中使用 @Alternative 注释声明一个类并使用该类更新对应的类不会替换默认 bean。它与其他接口一起工作正常。为什么?

第二种情况的源代码示例

加载器类:

@Dependent
@Default
public class DefaultLoader implements MyLoader {

    @Inject
    protected DefaultLoader(MyBinder binder) {
        map = binder.getMap();
    }

    ...

}

默认绑定器类:

@Dependent
@Default
public class DefaultBinder implements MyBinder {

    public DefaultBinder() {
        define();
    }

    ...

}

替代粘合剂类:

@Dependent
@Alternative
public class ProtoBinder extends DefaultBinder {

    public ProtoBinder() {
        super();
    }

    ...

}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
       http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" version="1.1"
    bean-discovery-mode="annotated">
    <alternatives>
        <class>com.proto.ProtoBinder</class>
    </alternatives>
</beans>

【问题讨论】:

标签: jakarta-ee cdi glassfish-4


【解决方案1】:

@Alternative 仅与其他 @Alternative 实现相关。它不会覆盖 @Default 限定 bean。如果你真的想走这条路,那么你可能想看看@Specializes。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多