ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath*:springService.xml");
  
ServiceA serviceA = (ServiceA ) ctx.getBean("serviceA "); 其中springService.xml中定义了bean serviceA ,但springService.xml与该段java不在同一个目录这样取不到serviceA,请问问题出在哪?
Many thanks!!

解决方案 »

  1.   

    springService.xml路径写成全路径试试,估计是你路径写错了
      

  2.   

    把配置文件放在项目的src目录下,
      

  3.   

    我之前查的这样可以得到ctx,而且我debug的时候发现确实ctx也得到了问题是ServiceA serviceA = (ServiceA ) ctx.getBean("serviceA "); 
    这一步没有得到,难道是ctx是错误的?再问下全路径怎么写,我java文件与配置文件隔好远
      

  4.   

    springService.xml的配置文件和java类在什么位置是没有影响的。是说让你在 springService.xml 里面的内容<bean id= 'serviceA ' class="这个地方写你的java类的全路径"
      

  5.   

    那个里面我写的是全路径 
    之前我是用ApplicationContext ctx=new ClassPathXmlApplicationContext("springService.xml"); 
    没有classpath*:,发现得不到ctx
    然后网上查了下用classpath*可以得到ctx
    但ServiceA serviceA = (ServiceA ) ctx.getBean("serviceA "); 这句完后报错,not found source
      

  6.   

    哦,那就把你的这个 springservice.xml贴出来看看。
      

  7.   


    我springService.xml是这样<bean id="serviceA"
    class="com.XXX.XX.service.bean.ServiceA"
    />
      

  8.   

    放src 目录下: <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/springService-*.xml,classpath*:springService.xml,classpath*:springService-*.xml</param-value>
    </context-param> <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
      

  9.   

    springService.xml所在的目录为src/main/resource/springconfig/springService.xml
    在springService.xml中定义了
    <bean id="serviceA" 
    class="com.XXX.XX.service.bean.ServiceA" 
    />
    而serviceA在com.XXX.XX.service.bean.ServiceA这个目录底下我的代码如下:ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath*:springService.xml");
     ServiceA serviceA = (ServiceA ) ctx.getBean("serviceA"); 
        return serviceA .isMethod();可以通过ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath*:springService.xml");获得ctx
    但执行到ServiceA serviceA = (ServiceA ) ctx.getBean("serviceA"); 就报错source not found请教什么原因啊?
      

  10.   

    ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath*:springService.xml"); 这个出的问题,把路径写清楚了,如果再不行,就把springService.xml里面那个bean的路径写清楚了。
    问题应该就出在这两个路径上的
      

  11.   

    ActionServlet actionServlet=new ActionServlet();
    actionServlet.init();
    ServletContext servletContext = actionServlet.getServletContext();
    WebApplicationContext webApplicationContext = 
    (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    ServiceA serviceA=
    (ServiceA ) webApplicationContext.getBean("serviceA");
     return serviceA.isxx();
    执行到ServletContext servletContext = actionServlet.getServletContext();这里就报错 了请教是什么原因哦,郁闷