spring+cxf 開發webService(主要是記錄遇到spring bean注入不進來的解決方法)

這裏不介紹原理,只是記錄本身spring+cxf的開發過程和遇到的問題java

場景:第三方公司須要調用咱們的業務系統,以xml報文的形式傳遞數據,以後咱們解析報文存儲到咱們數據庫生成業務單據;web

WebService的框架由多種,這裏選cxf,與Spring的集成比較好;spring

直接看代碼數據庫

1 項目用的maven,首先添加依賴(這個依賴啊  ,教程引用幾個的都有,這個看須要吧,我是用了四個)apache

          <dependency>
              <groupId>org.apache.cxf</groupId>
              <artifactId>cxf</artifactId>
              <version>2.7.11</version>
              <type>pom</type>
          </dependency>
          <dependency>
              <groupId>org.apache.cxf</groupId>
              <artifactId>cxf-rt-frontend-jaxws</artifactId>
              <version>2.7.11</version>
          </dependency>
          <dependency>
              <groupId>org.apache.cxf</groupId>
              <artifactId>cxf-rt-transports-http</artifactId>
              <version>2.7.11</version>
          </dependency>
          <dependency>  
              <groupId>org.apache.cxf</groupId>  
              <artifactId>cxf-rt-transports-http-jetty</artifactId>  
              <version>2.7.11</version>  
         </dependency>

2 配置web.xml,在原來基礎上添加(這裏我只是添加cxf配置,spring中基礎的配置以前項目中確定有,像什麼context-param什麼的我想大家確定早就有了)app

 <!-- cxf -->
  
    <servlet>  
        <servlet-name>CXFServlet</servlet-name>  
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>CXFServlet</servlet-name>  
        <url-pattern>/webservice/*</url-pattern>  
    </servlet-mapping>  

3 配置applicationContext.xml(仍是在原來的基礎上添加以下)框架

   3.1 首先在該xml添加命名空間,這是我在原來基礎上添加的(紅色部分是我本身添加的)frontend

  

   3.2 在原來基礎上添加maven

 <!-- cxf配置 -->
    <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" />  

    <bean id="reimBillHandler" class="com.ufgov.lp.xml.sax.handler.ReimBillHandler" />
    
    <bean id="reciveBillServiceImpl" class="com.ufgov.lp.bill.webservice.impl.ReciveBillServiceImpl">  
        <property name="reimBillHandler" ref="reimBillHandler"></property>  
    </bean> 
    
    <jaxws:endpoint id="reciveBillService"  
        implementor="#reciveBillServiceImpl" address="/reciveBillService" /> 
    <!-- cxf配置結束 -->

 接下來開始寫代碼ide

4 定義接口

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;


/**
 * 
 * <p>接收報文接口</p>
 * @author shangcg
 * @since 2017年11月1日
 */

@WebService
public interface ReciveBillService {
  
  @WebMethod  
  public String request(@WebParam(name = "xmlStr") String xmlStr);   

}

5 建立實現類

import javax.annotation.Resource;
import javax.jws.WebService;

import com.ufgov.lp.bill.webservice.ReciveBillService;
import com.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect;
import com.ufgov.lp.xml.sax.handler.ReimBillHandler;


@WebService
public class ReciveBillServiceImpl implements ReciveBillService {

  @Resource 
  ReimBillHandler reimBillHandler;
  
  
 
  @Override
  public String request(String xmlStr) {
    

    try {
      /**與數據庫結構一致的javaBean對象*/
      LpBizBillDataCollect lpBizBillDataCollect  = new LpBizBillDataCollect();
      
      //把傳過來的報文直接裝到對象中(實際上業務系統通常得對XML解析,這裏不說解析)
      lpBizBillDataCollect.setField01(xmlStr);
      
      //插入數據庫(reimBillHandler注入進來才能調用方法)
      reimBillHandler.excuteBillInsert(lpBizBillDataCollect);
      
    } catch (Exception e) {
      e.printStackTrace();
    }
    return "這裏是返回的報文";
  }

  
  public ReimBillHandler getReimBillHandler() {
    return reimBillHandler;
  }

  //注意這裏的set方法,否則稍後配置bean後會注入不進來
  public void setReimBillHandler(ReimBillHandler reimBillHandler) {
    this.reimBillHandler = reimBillHandler;
  }

}

6看ReimBillHandler (這個類就是原本業務系統已經存在的用@service標註的類,成功注入該類後,若是該類在注入其餘類我們就能夠不用管了)

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ufgov.lp.bill.dao.LpBizBillDao;
import com.ufgov.lp.bill.webservice.bean.LpBizBillDataCollect;


/**
 * 
 * <p>注入LpBizBillDao插入數據</p>
 * @author shangcg
 * @since 2017年11月3日
 */
@Service
public class ReimBillHandler{
  
  @Autowired
  LpBizBillDao lpBizBillDao;
  
  
  /**插入數據庫表數據*/
  public int excuteBillInsert(LpBizBillDataCollect lpBizBillDataCollect){
    
    return lpBizBillDao.insert(lpBizBillDataCollect);//插入數據庫數據
  }
  
}

 

說一下這些過程當中我遇到的錯誤:

1 spring的bean注入不進來  解決:以@WebService註解的類,引用spring中bean事須要幹兩件事,第一是用@Resource註解注入,不是@autowire;   第二必須有set方法 ; 第三是ApplicationContext.xm必須配置bean和引用

相關文章
相關標籤/搜索