cxf和spring集成注入值为NULL问题

用cxf+spring开发web service程序很简单,不过有一些集成问题要注意。  

1、关于bean的声明 

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

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

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

写了一个类来证明这一点: 
Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  ;
  7.                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  ;
  8.                             http://www.springframework.org/schema/context   ;
  9.                             http://www.springframework.org/schema/context/spring-context-3.0.xsd  ;
  10.                             http://cxf.apache.org/jaxws   ;
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  15.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  16.       
  17.     <context:component-scan base-package="com.huawei.framework" />  
  18.       
  19.     <bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">  
  20.         <property name="remedy" ref="remedy" />  
  21.     </bean>  
  22.   
  23.     <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  24.         implementor="#helloWorldImpl" />  
  25.   
  26.     <jaxws:client id="remedy"  
  27.         serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
  28.         address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
  29.   
  30. </beans>  

Java代码   收藏代码
  1. protected void doGet(HttpServletRequest request,  
  2.             HttpServletResponse response) throws ServletException, IOException {  
  3.   
  4.         ServletContext context = request.getServletContext();  
  5.   
  6.         WebApplicationContext wc = WebApplicationContextUtils  
  7.                 .getWebApplicationContext(context);  
  8.   
  9.         String[] beans = wc.getBeanDefinitionNames();  
  10.   
  11.         for (String beanName : beans) {  
  12.             System.out.println(beanName);  
  13.         }  
  14.           
  15.         RemedyWebServiceCM remedy = (RemedyWebServiceCM) wc.getBean("remedy");  
  16.           
  17.         AcknowledgeRequest message = new AcknowledgeRequest();  
  18.         remedy.acknowledge(message);  
  19.   
  20.     }  

在控制台可以看到以下几个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的话,要注意: 

错误的写法: 
Xml代码   收藏代码
  1. <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  2.         implementor="com.huawei.framework.webservice.HelloWorldImpl" />  

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

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

正确的写法: 
Xml代码   收藏代码
  1. <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  2.         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="">的方式来注入 
Java代码   收藏代码
  1. @WebService  
  2. public interface RemedyWebServiceCM {  
  3.   
  4.     RemedyResponse acknowledge(AcknowledgeRequest arg0);  
  5.   
  6.     RemedyResponse close(CloseRequest arg0);  
  7.   
  8.     RemedyResponse notify(NotifyRequest arg0);  
  9.   
  10. }  

Java代码   收藏代码
  1. public class HelloWorldImpl implements HelloWorld {  
  2.   
  3.     private RemedyWebServiceCM remedy;  
  4.   
  5.     public String sayHi(User theUser) {  
  6.           
  7.         AcknowledgeRequest message = new AcknowledgeRequest();  
  8.         remedy.acknowledge(message);  
  9.           
  10.         return "Hello " + theUser.getName() + " ,your age is "  
  11.                 + theUser.getAge();  
  12.     }  
  13.   
  14.     public void setRemedy(RemedyWebServiceCM remedy) {  
  15.         this.remedy = remedy;  
  16.     }  
  17.       
  18. }  

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  ;
  7.                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  ;
  8.                             http://www.springframework.org/schema/context   ;
  9.                             http://www.springframework.org/schema/context/spring-context-3.0.xsd  ;
  10.                             http://cxf.apache.org/jaxws   ;
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  15.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  16.       
  17.     <context:component-scan base-package="com.huawei.framework" />  
  18.       
  19.     <bean id="helloWorldImpl" class="com.huawei.framework.webservice.HelloWorldImpl">  
  20.         <property name="remedy" ref="remedy" />  
  21.     </bean>  
  22.   
  23.     <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  24.         implementor="#helloWorldImpl" />  
  25.   
  26.     <jaxws:client id="remedy"  
  27.         serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
  28.         address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
  29.   
  30. </beans>  

用这种<bean id="">的方式就可以 
Java代码   收藏代码
  1. @Controller  
  2. public class HelloWorldImpl implements HelloWorld {  
  3.   
  4.     @Autowired  
  5.     private RemedyWebServiceCM remedy;  
  6.   
  7.     public String sayHi(User theUser) {  
  8.           
  9.         AcknowledgeRequest message = new AcknowledgeRequest();  
  10.         remedy.acknowledge(message);  
  11.           
  12.         return "Hello " + theUser.getName() + " ,your age is "  
  13.                 + theUser.getAge();  
  14.     }  
  15.       
  16. }  

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans  ;
  7.                             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  ;
  8.                             http://www.springframework.org/schema/context   ;
  9.                             http://www.springframework.org/schema/context/spring-context-3.0.xsd  ;
  10.                             http://cxf.apache.org/jaxws   ;
  11.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  12.   
  13.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  14.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  15.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  16.       
  17.     <context:component-scan base-package="com.huawei.framework" />  
  18.       
  19.     <jaxws:endpoint id="helloWorld" address="/HelloWorld"  
  20.         implementor="#helloWorldImpl" />  
  21.   
  22.     <jaxws:client id="remedy"  
  23.         serviceClass="com.huawei.remedy.webservice.RemedyWebServiceCM"  
  24.         address="http://10.78.229.199:8080/remedy/webservice/RemedyWebService" />  
  25.   
  26. </beans>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值