如题所述,得不到bean
具体的操作如下,我的前端是用DWR的
在tomcat服务器启动的时候自动加载spring配置文件
web.xml的配置如下:<!--  载入Spring配置文件-->      <context-param>           <param-name>contextConfigLocation</param-name>           <param-value>classpath*:/applicationContext_hibernate.xml</param-value> <!-- 自动载入的spring配置 -->      </context-param>      <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>spring配置文件中有要获取的这个ID的bean
我在action中要获取applicationContex实例来获取spring配置文件中的bean,写法如下:ServletContext servletContext = WebContextFactory.get().getSession().getServletContext();
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext); 

iaduserInfoService=(IaduserInfoService)ctx.getBean("aduserInfoService");
运行的时候问题出来了,页面提示“No bean named 'aduserInfoService' is defined”即aduserInfoService这个bean没有被找到。。可明明spring配置文件也被加载了,然后里面也有个aduserInfoService为ID的bean,为什么加载不了呢?
我自己的初步预料可能会是因为ServletContext servletContext = WebContextFactory.get().getSession().getServletContext();
ApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext); ctx对象没有被获取到,因为我前端程序是直接用DWR来调用服务器端的action的,action不在spring容器的管理范围内。不知道是不是这样的,各位大虾有遇到这个问题的帮我解答下了。。谢谢

解决方案 »

  1.   

    分析很正确,有这种可能。
    但如果没有获得ctx,那ctx不是应该为null么?如果为空,那应该是报NullPointerException呀?楼主断点调试一下吧。
      

  2.   

    我断点调试了下。。ctx的值有指向classpath*:/applicationContext_hibernate.xml可是还是取不出我需要的那个bean;
    我同时用手动加载aplicationContext的方式测试了下。。这时候都可以取得到"aduserInfoService"这个bean。。
    还有我现在的applicationContext_hibernate.xml是放在src目录下的一个包里的。。我重新测了下把它改到WEB-INFO/目录下。。即如下的代码:
     <context-param>           <param-name>contextConfigLocation</param-name>           <param-value>/WEB-INFO/applicationContext_hibernate.xml</param-value> <!-- 自动载入的spring配置 -->      </context-param>
    tomcat服务器启动的时候却报出了无法获取actionFactory这个bean的错。。这又是为什么呢???
    这个问题搞的我很头大啊。。有遇到类似问题的大虾们帮忙解答下哦。。小女子万分感激!!!!!