Java 托管bean中的spring服务为空
嗨,我对春天有点问题。当我从managedbean调用服务时,我有一个NullPointerException 在我的applicationContext中,我有以下配置:Java 托管bean中的spring服务为空,java,spring,spring-mvc,Java,Spring,Spring Mvc,嗨,我对春天有点问题。当我从managedbean调用服务时,我有一个NullPointerException 在我的applicationContext中,我有以下配置: <context:annotation-config/> <context:component-scan base-package="com.sertec" /> 所有类都在包com.sertec中发生NullPointerException,因为当您尝试调用estacionService.getE
<context:annotation-config/>
<context:component-scan base-package="com.sertec" />
所有类都在包com.sertec中发生NullPointerException,因为当您尝试调用
estacionService.getEstacionesActivas()
时,estacionService
为空
您可以更改此选项:
@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()
的调用仅在响应自动连线时发生
我怀疑问题的根源存在于代码的其他地方(其他自动连接和后期构造的副产品)
可能会有所帮助?发生NullPointerException是因为当您尝试调用
estacionService.getEstacionesActivas()
时,estacionService
为空
您可以更改此选项:
@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()
的调用仅在响应自动连线时发生
我怀疑问题的根源存在于代码的其他地方(其他自动连接和后期构造的副产品)
也许会有所帮助?最后,我的faces-config.xml文件中出现了一个错误
<faces-config>
.......
<application>
.....
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
.......
</application>
.......
</faces-config>
.......
.....
org.springframework.web.jsf.el.SpringBeanFacesELResolver
.......
.......
最后,我的faces-config.xml文件中出现了一个错误
<faces-config>
.......
<application>
.....
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
.......
</application>
.......
</faces-config>
.......
.....
org.springframework.web.jsf.el.SpringBeanFacesELResolver
.......
.......
堆栈跟踪更深吗?堆栈跟踪更深吗?非常感谢您的回复。最后,它工作了,但是,我不明白为什么相同的代码在其他项目中工作得很好,我的配置有问题,我将在下一篇评论中发布配置。非常感谢您的回复。最后,它成功了,但是,我不明白为什么相同的代码在其他项目中工作得很好,我的配置有问题,我将在下一篇评论中发布配置。你应该考虑改进你的答案。现在还不清楚(至少对我来说)这和另一个答案有什么关系。你应该考虑改进你的答案。现在还不清楚(至少对我来说)这与另一个答案有什么关系。
<faces-config>
.......
<application>
.....
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
.......
</application>
.......
</faces-config>