【问题标题】:Getting WELD-001303 error when implementing custom CDI extension for @SessionScoped scope为 @SessionScoped 范围实现自定义 CDI 扩展时出现 WELD-001303 错误
【发布时间】:2015-05-25 13:36:27
【问题描述】:

我正在尝试实现一个自定义 CDI 范围,我认为从扩展现有的 @SessionScoped 开始是最简单的方法。我首先关注this tutorial。但是,当访问在我的范围内引用 CDI bean 的 XHTML 页面时,这会导致

org.jboss.weld.context.ContextNotActiveException: WELD-001303: No active contexts for scope type myscope.MyScoped
at org.jboss.weld.manager.BeanManagerImpl.getContext(BeanManagerImpl.java:687)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:740)
at org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:107)
at org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:90)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:188)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
at com.sun.el.parser.AstValue.getBase(AstValue.java:151)
at com.sun.el.parser.AstValue.getTarget(AstValue.java:170)
at com.sun.el.parser.AstValue.invoke(AstValue.java:272)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UIViewAction.broadcast(UIViewAction.java:562)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
... 31 more

我的注释是

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@NormalScope
public @interface MyScoped {
}

我的上下文是

public class MyScopeContext extends HttpRequestContextImpl {
    @Override
    public Class<? extends Annotation> getScope() {
        return MyScoped.class;
    }
}

(注意我继承自org.jboss.weld.context.http.HttpSessionContextImpl。)

我的扩展是

public class MyScopeExtension implements Extension {
    public void afterBeanDiscovery(@Observes AfterBeanDiscovery event, BeanManager manager) {
        event.addContext(new MyScopeContext());
    }
}

我在 GlassFish 4.1 上运行,类路径上有 javax:javaee-web-api:7.0org.jboss.weld:weld-core:1.1.29.Final

我几乎可以肯定我犯了一些基本错误,但这是什么?

【问题讨论】:

  • 您在代码中的任何时候都在激活/停用上下文吗?看起来您只是按照定义的方式将其丢弃。
  • @JohnAment,您能否详细说明如何执行此操作?我没有找到有关如何激活自定义范围的文档。如果它解决了我的问题,我已经准备好接受这个答案。
  • 您在文档中没有找到它,因为您是从焊接的内部类扩展而来的 - 不推荐。这是您审查的绑定上下文。 docs.jboss.org/weld/javadoc/2.0/weld-api/org/jboss/weld/context/…一会儿我会整理一个更完整的答案。

标签: cdi java-ee-7 named-scope jakarta-ee


【解决方案1】:

与此同时,我发现我做错了什么。

我不知道我必须通过创建文件 src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 来注册我的 CDI 扩展内容:

myscope.MyScopeExtension

我还在 GitHub 上找到了 CDI 自定义范围的示例实现:https://github.com/rmpestano/cdi-custom-scope

我还没有完全完成我的实现,因此我将继续我的研究......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多