1
我有以下JSF2控制器/ bb,我注入了一個會話bean。但是,它無法獲得對EJB本地會話Bean的引用。JSF2支持Bean無法獲得對@注入會話Bean的引用
JSF2輔助Bean:
@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
謝謝馬特。我拿出了多餘的「@Named(」scorecardBackingBean「)」,但注入的資源是一個EJB會話bean。它仍然給予同樣的感覺。 – 2012-07-23 09:47:37
我已經從@Inject更改爲EJB,它工作。我需要檢查一下,爲什麼。但是非常感謝。 – 2012-07-23 10:08:51
@Chin這確實很奇怪。兩種注射方法都應該起作用(從理論上講),也許你會在我的答案中添加的第三個鏈接中得到一些解釋。 (我只是看着我的最新項目,我使用'@ Inject'來@Named bean和@EJB來注入ejbs)。 – 2012-07-23 12:49:10