ApplicationContext.getBean()获取service无异常,但结果为null,获取dao可以得到实体

user_ljx 2014-11-20 11:43:12
整个系统运行正常,功能也无问题,只是我在写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 里面
<bean id="testController" class="com.lmxf.post.test.web.TestController">
<property name="methodNameResolver">
<ref bean="methodNameResolver" />
</property>
<property name="testService">
<ref bean="testService" />
</property>
</bean>

testDao -----------applicationContext-dao.xml
<bean id="testDao" class="com.lmxf.post.test.repository.TestDao">
<property name="sessionFactory" ref="sqlSessionFactory"/>
</bean>

testService ---------applicationContext-service.xml
<bean id="testService" class="com.lmxf.post.test.service.TestService">
<property name="testDao" ref="testDao"/>
</bean>

系统运行的时候正常的,就是我写junit测试无法到service层进得测试,运行又无异常,完全通过,只是调用testService的方法时不有进去,testService 改一下又会报异常,说明我这里应该也没有问题,我试了一下别的模块,也都如上,就只是service获取不到,求大神!!
...全文
6123 22 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
贾柯 2017-01-21
  • 打赏
  • 举报
回复
@Autowired private LoginService loginService; @Autowired private ResponseData responseData; //LoginService loginService = (LoginService) applicationContext.getBean("loginService"); responseData = loginService.doProcess(request, response, requestParams); assertEquals("main", (String) responseData.getRespData()); 上面通过注入的方式也可以了。
贾柯 2017-01-21
  • 打赏
  • 举报
回复
今天我也碰到完全一样的问题,花了4个多小时调查,是这样解决的: 异常信息: java.lang.ClassCastException: com.sun.proxy.$Proxy53 cannot be cast to com.afis.trade.service.impl.LoginServiceImpl at com.afis.trade.service.LoginServiceTest.testDoProcess(LoginServiceTest.java:120) ... 程序结构: LoginService //接口类,没有添加任何注解。 @Transactional @Service("loginService") LoginServiceImpl //实现类,一定要加上面的注解。 spring.xml配置文件,其实只需要扫描实现类(必须): <context:component-scan base-package="com.afis.trade.service.impl" /> 测试类: @ContextConfiguration(locations = {"classpath*:/applicationContext-dataSource.xml","classpath*:/spring.xml", "classpath*:/applicationContext.xml","classpath*:/spring-mvc.xml"}) public class LoginServiceTest extends AbstractTransactionalJUnit4SpringContextTests { 关键点: LoginService loginService = (LoginServiceImpl) this.applicationContext.getBean("loginService"); 改成以下,测试成功: LoginService loginService = (LoginService) this.applicationContext.getBean("loginService"); 注意: 需要把获取的bean强制成接口类型即可,之前我的LoginService不是接口,是一个具体实现类,怎么找方法都测试不通过,通过调查发现Spring的Cglib动态代理,如果接口没有做任何继承,是通过Cglib得到实例的,如何你的接口有继承关系存在,就是能过JDK的动态proxy去完成。Spring框架的applicationContext.getBean()实例的获取是基于接口Prototype响应机制的。你们看看框架源码就知道了。
Ansore 2016-09-13
  • 打赏
  • 举报
回复
testDao = (TestDao) context.getBean("testDao"); 这样就可以了
久眠深巷 2016-04-11
  • 打赏
  • 举报
回复
大神解决没有,遇到一模一样的错误,麻烦指点下。
砍我一刀 2015-04-11
  • 打赏
  • 举报
回复
哎呦,我今天也碰到了。
meng020712 2014-11-21
  • 打赏
  • 举报
回复
你这个结果是null、 还是你想说是空指针异常呢?
user_ljx 2014-11-21
  • 打赏
  • 举报
回复
user_ljx 2014-11-21
  • 打赏
  • 举报
回复
引用 16 楼 wobuxiangnila 的回复:
通过controller get方法获取的service 是什么?
程序运行的时候controller里获取是正常的,测试的时候用junit或都写个main方法都是null
猎魔人-不纯 2014-11-21
  • 打赏
  • 举报
回复
通过controller get方法获取的service 是什么?
user_ljx 2014-11-21
  • 打赏
  • 举报
回复
引用 14 楼 wsad227 的回复:
你这个结果是null、 还是你想说是空指针异常呢?
]您老人家没看上面问题么?都说了,运行无异常,就是得到的结果打印出来是null
Defonds 2014-11-20
  • 打赏
  • 举报
回复
testDao = (TestDao) context.getBean("testDao");                      
testService = (TestService) context.getBean("testService");     
testController = (TestController) context.getBean("testController");
这样写看看能取到不
wyx100 2014-11-20
  • 打赏
  • 举报
回复
路过,加油
user_ljx 2014-11-20
  • 打赏
  • 举报
回复
求救啊,来大神帮忙啊!!!!
user_ljx 2014-11-20
  • 打赏
  • 举报
回复
引用 9 楼 littlebrain4solving 的回复:
我记得可以applicationContext-*来做的;话说为什么写4个参数进去。。
因为用*代的时候异常了,应该是读取的先后顺序问题,所以我只能一个个写,而且这几个前后顺序搞错都会出现异常
  • 打赏
  • 举报
回复
我记得可以applicationContext-*来做的;话说为什么写4个参数进去。。
user_ljx 2014-11-20
  • 打赏
  • 举报
回复
引用 6 楼 ooppookid 的回复:
debug “context.getBean("testService")”这个的结果是什么,强转可能会出现null。

看了一下可能是这个里问题,但是具体是什么不知道,debug如下


DAO结果如下
user_ljx 2014-11-20
  • 打赏
  • 举报
回复
引用 5 楼 l359122505 的回复:
TestService代码贴一下
TestService是没有问题的,不然我系统不会运行正常,只是我在测试的时候取不到bean,而且我这里几十个Sercice我试了几个都不行,但DAO层跟controller就可以,所以跟TestService应该是没有关系的
猿人林克 2014-11-20
  • 打赏
  • 举报
回复
debug “context.getBean("testService")”这个的结果是什么,强转可能会出现null。
繁华终归落尽 2014-11-20
  • 打赏
  • 举报
回复
TestService代码贴一下
user_ljx 2014-11-20
  • 打赏
  • 举报
回复
引用 3 楼 suciver 的回复:
ApplicationContext context = new ClassPathXmlApplicationContext( new String []{ "applicationContext-core.xml", "applicationContext-dao.xml", "applicationContext-service.xml","spring-mvc.xml" });
用过了,刚开始就是用的ClassPathXmlApplicationContext,只是后来改来改去才换了一下,跟这个应该是没有关系的,因为这里读取是没有问题的,不然dao层跟controller也不会读取的到,而且我这里读取service层的时候没有异常,只是到头来结果确为null,我试过把bean名称改一下,就会出找不到这个bean的异常,这样说明这里还是进行了读取,testService也注入了,只是不知道为什么结果确为null
加载更多回复(2)

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧