最近項目中需要用到webservice,在spring中集成cxf時一直報錯:
嚴重: StandardWrapper.Throwable org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cxf' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1213) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1086) at org.apache.cxf.transport.servlet.CXFServlet.loadBus(CXFServlet.java:80)
網上給出的最多的解決方案是:
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
另外就是說spring版本和cxf版本不匹配等。
試了很多種方案都不行,最后嘗試修改配置文件解決了;
修改前:
web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml,classpath:springContext-cxf.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
修改方案:將springContext-cxf.xml配置文件導入到springmvc-servlet.xml文件中
修改后代碼
web.xml
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
springmvc-servlet.xml
<!-- 導入webservice接口配置文件 -->
<import resource="classpath:applicationContext-cxf.xml" />
</beans>
問題解決,我用的是spring5.0+cxf 3.1,親測這個不存在版本不匹配問題。