在为@SessionScoped范围实现自定义CDI扩展时出现WELD-001303错误

时间:2015-05-25 13:36:27

标签: java-ee cdi java-ee-7 named-scope java-ee-web-profile

我正在尝试实施自定义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());
    }
}

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

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

1 个答案:

答案 0 :(得分:0)

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

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

myscope.MyScopeExtension

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

我仍然没有完成我的实施,因此我将继续我的研究......