cxf开发restful接口报Can't find the request for xx's Observer错误解决

​ 最近在做springmvc与cxf框架整合,发现springmvc需要配置DispatcherServlet,而cxf需要配置CXFServlet,刚开始我把配置文件都交给DispatcherServlet去加载,启动后调用restful api报错:Can’t find the request for xx’s Observer。怀疑是配置文件加载有问题,改为全部交由ContextLoaderListener加载的方式,又报错找不到spring-servlet.xml。后来在网上发现这段:

	the ContextLoaderListener looks for applicationContext.xml (or for the file specified by the context-param contextConfigLocation. Again the path is relative to the context-root. I usually place mine in /WEB-INF/classes/applicationContext.xml, and set this as a value of the context-param).
	The dispatcherServlet-servlet.xml is a child context to the one defined byapplicationContext.xml. The child context can access beans from the parent context, but the opposite is not true. So imagine you have a "web" context, with all controllers and web-related stuff, and a "main" context with everything else
	It is advisable to have as few contexts as possible (for the sake of simplicity). But you can define multiple dispatcher servlets, and hence have multiple "child" contexts.
	Spring lets you define multiple contexts in a parent-child hierarchy. 
	
    The applicationContext.xml defines the beans for the "root webapp context", i.e. the context associated with the webapp.  
    The spring-servlet.xml (or whatever else you call it) defines the beans for one servlet's app context. There can be many of these in a webapp, one per Spring servlet (e.g. spring1-servlet.xml for servlet spring1, spring2-servlet.xml for servlet spring2).  
    Beans in spring-servlet.xml can reference beans in applicationContext.xml, but not vice versa.  
    All Spring MVC controllers must go in the spring-servlet.xml context.  
    In most simple cases, the applicationContext.xml context is unnecessary. It is generally used to contain beans that are shared between all servlets in a webapp. If you only have one servlet, then there's not really much point, unless you have a specific use for it.

ContextLoaderListener加载的上下文与servlet加载的上下文是父子关系,子可以引用父里定义的bean,而父不能访问子的。applicationContext.xml是全局共享,而spring-servlet.xml是每个servlet各自加载不共享。 理解了这个,只要把controller里用到的bean交由spring-servlet.xml加载,而剩下的全都由applicationContext.xml加载即可。

下而是我的web.xml配置,供参考:

<!--加载spring配置文件,注意这里跟cxf集成要用这种方式启动-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>cxf</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>cxf</servlet-name>
    <url-pattern>/service/*</url-pattern>
  </servlet-mapping>
发布了16 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/shipaiYang/article/details/100515531