为@SessionScoped 范围实现自定义 CDI 扩展时出现 WELD-001303 错误
Getting WELD-001303 error when implementing custom CDI extension for @SessionScoped scope
我正在尝试实现一个 自定义 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.0
和 org.jboss.weld:weld-core:1.1.29.Final
。
我几乎可以肯定我犯了一些基本错误,但这是什么错误?
与此同时,我发现了我做错了什么。
我不知道我必须通过创建包含以下内容的文件 src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 来注册我的 CDI 扩展:
myscope.MyScopeExtension
我还在 GitHub 上找到了 CDI 自定义范围的示例实现:https://github.com/rmpestano/cdi-custom-scope
我还没有完全完成我的实现,所以我会继续我的研究...
我正在尝试实现一个 自定义 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.0
和 org.jboss.weld:weld-core:1.1.29.Final
。
我几乎可以肯定我犯了一些基本错误,但这是什么错误?
与此同时,我发现了我做错了什么。
我不知道我必须通过创建包含以下内容的文件 src/main/resources/META-INF/services/javax.enterprise.inject.spi.Extension 来注册我的 CDI 扩展:
myscope.MyScopeExtension
我还在 GitHub 上找到了 CDI 自定义范围的示例实现:https://github.com/rmpestano/cdi-custom-scope
我还没有完全完成我的实现,所以我会继续我的研究...