关于网友提出的“(jsp)ApplicationContextgetBean获取service无异常,但结果为null,获取dao可以得到实体”问题疑问,本网通过在网上对“(jsp)ApplicationContextgetBean获取service无异常,但结果为null,获取dao可以得到实体”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题:(jsp)ApplicationContextgetBean获取service无异常,但结果为null,获取dao可以得到实体
描述:junitspringbeanApplicationContext
整个系统运行正常,功能也无问题,只是我在写junit测试的时候获取service的bean无法得到实体,如果我把bean里面改个错误的名称又会报错,证我我配置也应该没有问题,而且获取DAO跟controller层都是可以的,就是service为空,求大神帮忙,如下:
ApplicationContext context = new FileSystemXmlApplicationContext( new String []{
"classpath:applicationContext-core.xml", "classpath:applicationContext-dao.xml",
"classpath:applicationContext-service.xml","classpath:spring-mvc.xml" });
testService = (TestService) context.getBean("testService"); =========结果为null
testDao = (TestDao) context.getBean("testDao"); =========可以取到值
testController = (TestController) context.getBean("testController");=======可以取到值
testController -----spring-mvc.xml 里面
testDao -----------applicationContext-dao.xml
testService ---------applicationContext-service.xml
系统运行的时候正常的,就是我写junit测试无法到service层进得测试,运行又无异常,完全通过,只是调用testService的方法时不有进去,testService 改一下又会报异常,说明我这里应该也没有问题,我试了一下别的模块,也都如上,就只是service获取不到,求大神!!
解决方案1: 通过controller get方法获取的service 是什么?
解决方案2: 你这个结果是null、
还是你想说是空指针异常呢?
解决方案3: 我记得可以applicationContext-*来做的;话说为什么写4个参数进去。。
解决方案4: debug “context.getBean("testService")”这个的结果是什么,强转可能会出现null。
解决方案5: TestService代码贴一下
解决方案6: ApplicationContext context = new ClassPathXmlApplicationContext( new String []{
"applicationContext-core.xml", "applicationContext-dao.xml",
"applicationContext-service.xml","spring-mvc.xml" });
解决方案7:testDao = (TestDao) context.getBean("testDao");
testService = (TestService) context.getBean("testService");
testController = (TestController) context.getBean("testController");
这样写看看能取到不
以上介绍了“(jsp)ApplicationContextgetBean获取service无异常,但结果为null,获取dao可以得到实体”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1432642.html