<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>context</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet> 这二个应该需要配置

解决方案 »

  1.   

    你直接注入到Controller里不就好了,何必这么麻烦。若真想如此获取Bean,你可以去实现ApplicationContextAware接口,得到ApplicationContext
      

  2.   

    唉,好久都不写XML文件了人了,现在都有点看不懂这些配置了
      

  3.   

    配置肯定是有的,我也不想这么配置啊
    网上说,他们是2个不同的context,一个是root(spring)的,一个是servlet(mvc)的,servlet里的能获取root里的,反之就获取不到。但是没有给出怎么才能获取到
    最后只能把bean的配置挪到applicationContext.xml里了
      

  4.   

    WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
    这个拿到的是父容器,而testA装配在了applicationContext-servlet子容器里。父容器无法取得子容器bean,反之则可以。
    因此,要取得子容器里的testA bean,需要先拿到子容器,如下:WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext,"org.springframework.web.servlet.FrameworkServlet.CONTEXT.vdc_ui" );
    TestA service=(TestA)webApplicationContext.getBean("testA");