`

cxf和spring集成的一些事

阅读更多
环境:
cxf-2.1.3,jdk6,jboss7.0.2,spring3.0.6

用cxf+spring开发web service程序很简单,不过有一些集成问题要注意。在此把这几天发现的一些总结一下,最后有一个遗留的问题

1、关于bean的声明

要发布或者要调用的web service接口,需要用@WebService注解声明。不过要注意的是,@WebService注解不会把类声明为spring的bean

可以声明为bean的方式有以下4个:

<jaxws:endpoint>
<jaxws:client>
<bean id="" class="">
@Component

写了一个类来证明这一点:
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       						http://www.springframework.org/schema/context 
      			 			http://www.springframework.org/schema/context/spring-context-3.0.xsd
        					http://cxf.apache.org/jaxws 
        					http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<context:component-scan base-package="com.huawei.framework" />
	
	<bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">
		<property name="remedy" ref="remedy" />
	</bean>

	<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

	<jaxws:client id="remedy"
		serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"
		address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />

</beans>

protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		ServletContext context = request.getServletContext();

		WebApplicationContext wc = WebApplicationContextUtils
				.getWebApplicationContext(context);

		String[] beans = wc.getBeanDefinitionNames();

		for (String beanName : beans) {
			System.out.println(beanName);
		}
		
		RemedyWebServiceCM remedy = (RemedyWebServiceCM) wc.getBean("remedy");
		
		AcknowledgeRequest message = new AcknowledgeRequest();
		remedy.acknowledge(message);

	}

在控制台可以看到以下几个bean:
20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) helloWorldImpl
20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) helloWorld
20:02:24,120 INFO  [stdout] (http--0.0.0.0-8888-2) remedy

以上3个bean,就分别是由<bean>、<jaxws:endpoint>、<jaxws:client>生成的

2、如果要在<jaxws:endpoint>的实现类中注入bean的话,要注意:

错误的写法:
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="com.huawei.framework.webservice.HelloWorldImpl" />

用这种写法,虽然HelloWorldImpl类已经被声明成bean了,但是注入是失败的

准确来说,在spring容器中存在有HelloWorldImpl对应的bean,并且相关的依赖也已经注入了。但是cxf框架得到的HelloWorldImpl,与上述的bean不是同一个对象,这个HelloWorldImpl对象可能是用反射或者其他机制创建出来的,没有任何组件被注入

正确的写法:
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

在implementor属性中,用#beanName,这样的话,cxf框架得到的HelloWorldImpl,就是spring容器持有的那个bean。当然这有个前提,就是系统中已经存在这个bean。用<bean id="">或者@Component都是OK的,这里支持注解

3、上面说的是<jaxws:endpoint>,比较简单。但是<jaxws:client>就比较麻烦

我目前感觉,<jaxws:client>声明的bean,没办法在别的bean里用@Autowired注入,只能用<bean id="">的方式来注入
@WebService
public interface RemedyWebServiceCM {

	RemedyResponse acknowledge(AcknowledgeRequest arg0);

	RemedyResponse close(CloseRequest arg0);

	RemedyResponse notify(NotifyRequest arg0);

}

public class HelloWorldImpl implements HelloWorld {

	private RemedyWebServiceCM remedy;

	public String sayHi(User theUser) {
		
		AcknowledgeRequest message = new AcknowledgeRequest();
		remedy.acknowledge(message);
		
		return "Hello " + theUser.getName() + " ,your age is "
				+ theUser.getAge();
	}

	public void setRemedy(RemedyWebServiceCM remedy) {
		this.remedy = remedy;
	}
	
}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       						http://www.springframework.org/schema/context 
      			 			http://www.springframework.org/schema/context/spring-context-3.0.xsd
        					http://cxf.apache.org/jaxws 
        					http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<context:component-scan base-package="com.huawei.framework" />
	
	<bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">
		<property name="remedy" ref="remedy" />
	</bean>

	<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

	<jaxws:client id="remedy"
		serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"
		address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />

</beans>

用这种<bean id="">的方式就可以
@Controller
public class HelloWorldImpl implements HelloWorld {

	@Autowired
	private RemedyWebServiceCM remedy;

	public String sayHi(User theUser) {
		
		AcknowledgeRequest message = new AcknowledgeRequest();
		remedy.acknowledge(message);
		
		return "Hello " + theUser.getName() + " ,your age is "
				+ theUser.getAge();
	}
	
}

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       						http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       						http://www.springframework.org/schema/context 
      			 			http://www.springframework.org/schema/context/spring-context-3.0.xsd
        					http://cxf.apache.org/jaxws 
        					http://cxf.apache.org/schemas/jaxws.xsd">

	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
	<context:component-scan base-package="com.huawei.framework" />
	
	<jaxws:endpoint id="helloWorld" address="/HelloWorld"
		implementor="#helloWorldImpl" />

	<jaxws:client id="remedy"
		serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"
		address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />

</beans>

用@Autowired加@Component的方式来做,就不行,报以下异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.huawei.remedy.webservice.RemedyWebServiceCM com.huawei.framework.webservice.HelloWorldImpl.remedy; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.huawei.remedy.webservice.RemedyWebServiceCM] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) [org.springframework.beans-3.0.6.RELEASE.jar:]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) [org.springframework.beans-3.0.6.RELEASE.jar:]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) [org.springframework.beans-3.0.6.RELEASE.jar:]
... 21 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.huawei.remedy.webservice.RemedyWebServiceCM] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) [org.springframework.beans-3.0.6.RELEASE.jar:]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) [org.springframework.beans-3.0.6.RELEASE.jar:]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) [org.springframework.beans-3.0.6.RELEASE.jar:]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) [org.springframework.beans-3.0.6.RELEASE.jar:]
... 23 more

说自动注入失败,因为找不到RemedyWebServiceCM类型的bean

试了很久,完全搞不定。也想不通是为啥,因为RemedyWebServiceCM这个类型的bean,明明已经用<jaxws:client>声明了,而且用配置文件的方式也是能搞定的
分享到:
评论
7 楼 hegengxi 2015-08-26  
错误的写法:
Xml代码
<jaxws:endpoint id="helloWorld" address="/HelloWorld"
        implementor="com.huawei.framework.webservice.HelloWorldImpl" />
=======================================================================
赞,解决了好几天都没解决的问题,太棒了
6 楼 liukai 2014-03-19  
liukai 写道
liukai 写道
我也碰到这个问题了.
无法注入成功.楼主最后解决了么?

如果是跑controller里,是注入成功了.
刚是在junit跑测试报错,但是依然很奇怪,为什么在junit注入失败~
持续解决中~

果然...测试类中要加上junit的注入配置啊
@ContextConfiguration({ "classpath:applicationContext.xml" , "classpath:applicationContext-api.xml"})


web.xml我是将applicationContext-api.xml和applicationContext.xml一起配置了.
但是junit没走web.xml.所以才注入失败....


5 楼 liukai 2014-03-19  
liukai 写道
我也碰到这个问题了.
无法注入成功.楼主最后解决了么?

如果是跑controller里,是注入成功了.
刚是在junit跑测试报错,但是依然很奇怪,为什么在junit注入失败~
持续解决中~
4 楼 liukai 2014-03-19  
我也碰到这个问题了.
无法注入成功.楼主最后解决了么?
3 楼 kyfxbl 2012-10-25  
是的,不用激动,呵呵
2 楼 ssetnegl1990 2012-10-25  
哥们华为公司的???看包得命名!!
1 楼 zhaoshunxin 2012-04-21  
错误的写法:
Xml代码 
<jaxws:endpoint id="helloWorld" address="/HelloWorld" 
        implementor="com.huawei.framework.webservice.HelloWorldImpl" /> 

cxf2.5.2已经支持了这种写法了

相关推荐

Global site tag (gtag.js) - Google Analytics