spring+cxf No bean named 'cxf' available
最近项目中需要用到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,亲测这个不存在版本不匹配问题。