提问者:小点点

JSF2 Backing Bean无法获取对@Inject'ed Session Bean的引用


我有以下JSF2控制器/bb,我已经注入了一个会话bean。但是,在它无法获得对EJB本地会话Bean的引用。

JSF2支持豆:

@ManagedBean
@Named("scorecardBackingBean")
@SessionScoped
public class ScorecardBackingBean implements Serializable {
    private static final long serialVersionUID = 7231502115861150753L;

    @Inject
    private IScorecardServiceBean scorecardServiceBean;

    @PostConstruct
    private void prepareMainLineChart() {
        this.mainLineChart = new CartesianChartModel();


         List<OrderVolume> orderVolumeList = this.scorecardServiceBean
         .getOrderVolume(); //ERROR here, scorecardServiceBean is null.
}

EJB:

@Stateless
public class ScorecardServiceBean implements IScorecardServiceBean {
}

EJB接口:

@Local
public interface IScorecardServiceBean {
    List<OrderVolume> getOrderVolume();
}

遇到的错误:

java.lang.IllegalStateException: JBAS011048: Failed to construct component instance
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:163)
    at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:95)
    at org.jboss.as.web.deployment.component.WebComponentInstantiator$2.<init>(WebComponentInstantiator.java:96)
    at org.jboss.as.web.deployment.component.WebComponentInstantiator.initializeInstance(WebComponentInstantiator.java:94)
.................
Caused by: java.lang.NullPointerException
    at sg.java.bus.controller.scorecard.ScorecardBackingBean.prepareMainLineChart(ScorecardBackingBean.java:79)
    at sg.java.bus.controller.scorecard.ScorecardBackingBean.init(ScorecardBackingBean.java:61)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.as.ee.component.ManagedReferenceLifecycleMethodInterceptorFactory$ManagedReferenceLifecycleMethodInterceptor.processInvocation(ManagedReferenceLifecycleMethodInterceptorFactory.java:130)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.ManagedReferenceInterceptorFactory$ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptorFactory.java:106)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288)
    at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61)
    at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:161)
    ... 52 more

共1个答案

匿名用户

您正在混合CDI托管bean(@命名)和JSF托管bean(@ManagedBean)。只需要其中一个注释。

如果选择一个或另一个,请确保导入正确的范围类。

CDI:javax. Enterprise.context.SessionScoped

JSF:javax. faces.bean.SessionScoped

看这些相关问题

  • JSF2.0 Tomcat7中@命名和@ManagedBean注释之间的区别
  • CDI@命名bean中的ManagedProperty返回null
  • 我应该使用@EJB还是@Inject