2017-03-15 56 views
0

嗨我有一個春天的問題。當我從managedbean調用一個服務,我有一個NullPointerException 在我的applicationContext我有configurarion:管理bean中的spring service null

<context:annotation-config/> 
<context:component-scan base-package="com.sertec" /> 

我的源代碼是:

@ManagedBean(name="estacionController") 
@Scope("view") 
public class EstacionController { 
    private List<Estacion> estacionesActivas; 
    private Estacion estacionSeleccionada; 

    @Autowired 
    EstacionService estacionService; 

    @PostConstruct 
    private void init() { 
     estacionesActivas = estacionService.getEstacionesActivas(); 
    } 
    public List<Estacion> getEstacionesActivas() { 
     return estacionesActivas; 
    } 
    public void setEstacionesActivas(List<Estacion> estacionesActivas) { 
     this.estacionesActivas = estacionesActivas; 
    } 
    public Estacion getEstacionSeleccionada() { 
     return estacionSeleccionada; 
    } 
    public void setEstacionSeleccionada(Estacion estacionSeleccionada) { 
     this.estacionSeleccionada = estacionSeleccionada; 
    } 
} 

站服務接口:

public interface EstacionService { 
    public List<Estacion> getEstacionesActivas(); 
    public Estacion findEstacionByAcronimo(String acronimo); 
} 

而實施是:

@Service("estacionService") 
public class EstacionServiceImpl implements EstacionService { 

    @Autowired 
    EstacionDao estacionDao; 

    @Override 
    public List<Estacion> getEstacionesActivas() { 
     return estacionDao.getEstacionesActivas(); 
    } 

    @Override 
    public Estacion findEstacionByAcronimo(String acronimo) { 
     return estacionDao.findEstacionByAcronimo(acronimo); 
    } 

} 

日誌是:

GRAVE: com.sun.faces.mgbean.ManagedBeanCreationException: Se ha producido un error al realizar la inyección de recurso en el bean administrado estacionController 
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:227) 
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103) 
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409) 
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269) 
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:257) 
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:117) 
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176) 
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203) 
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:94) 
    at org.apache.el.parser.AstValue.getValue(AstValue.java:137) 
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184) 
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109) 
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) 
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) 
    at javax.faces.component.UISelectItems.getValue(UISelectItems.java:129) 
    at org.primefaces.renderkit.InputRenderer.getSelectItems(InputRenderer.java:54) 
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:80) 
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:74) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeDynamicBody(PanelGridRenderer.java:131) 
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeTableBody(PanelGridRenderer.java:102) 
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeTableLayout(PanelGridRenderer.java:65) 
    at org.primefaces.component.panelgrid.PanelGridRenderer.encodeEnd(PanelGridRenderer.java:37) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176) 
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889) 
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85) 
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:72) 
    at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49) 
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859) 
    at com.sun.faces.context.PartialViewContextImpl.renderAll(PartialViewContextImpl.java:455) 
    at com.sun.faces.context.PartialViewContextImpl.processPartial(PartialViewContextImpl.java:313) 
    at javax.faces.context.PartialViewContextWrapper.processPartial(PartialViewContextWrapper.java:219) 
    at org.primefaces.context.PrimePartialViewContext.processPartial(PrimePartialViewContext.java:57) 
    at javax.faces.component.UIViewRoot.encodeChildren(UIViewRoot.java:1004) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856) 
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:430) 
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133) 
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:78) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1437) 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: com.sun.faces.spi.InjectionProviderException 
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:115) 
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokePostConstruct(WebContainerInjectionProvider.java:95) 
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221) 
    ... 72 more 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:113) 
    ... 74 more 
Caused by: java.lang.NullPointerException 
    at com.sertec.controllers.EstacionController.init(EstacionController.java:25) 
    ... 79 more 

所有類新的包裝com.sertec

+0

是否堆棧跟蹤進入更深的錯誤? – Jason

回答

3

出現的空指針異常,因爲estacionService是在時間點空當您嘗試調用estacionService.getEstacionesActivas()內。

你可以改變這一點:

@Autowired 
EstacionService estacionService; 

@PostConstruct 
private void init() { 
    estacionesActivas = estacionService.getEstacionesActivas(); 
} 

...這樣的:

EstacionService estacionService; 

@Autowired 
private void setEstacionService(EstacionService estacionService) { 
    this.estacionService = estacionService; 
    estacionesActivas = estacionService.getEstacionesActivas(); 
} 

...這將確保調用estacionService.getEstacionesActivas()只發生在響應時自動連接。

我懷疑問題的根源存在於代碼的其他地方(其他自動裝配和後構建的副產品)。

也許this SO post可能會有所幫助嗎?

+0

非常感謝您的回覆。最後它可以工作,但是,我不明白爲什麼相同的代碼在其他項目中工作正常,我的配置出現問題,我將在下一條評論中發佈配置。 –

0

最後還有在我的faces-config.xml文件

<faces-config> 
....... 
<application> 
..... 
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
....... 
</application> 
....... 
</faces-config> 
+0

你應該考慮改進你的答案。至少對我來說這並不明確,這與其他答案有什麼關係。 – GhostCat