Spring MVCプロジェクトの中でCXF webservice@Autowiredは実戦でピットに登ります.

18632 ワード

プロジェクト環境:Spring 4.2.3 apphe cxf 3.1.11 jdk 7 tomcat 7
ポイント:(1)正しいパッケージ(2)appration.xmlを導入し、x-servlet.xmlとweb.xml
(1)正しいパッケージを導入する
  • maven方式:国内の現在利用可能なミラーはアリ雲マベントがあり、その中のcxfバージョンは公式サイトよりいくつか遅れています.
  • <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>apache-cxf</artifactId>
            <version>${          }</version>
            <type>pom</type>
      </dependency>
  • 公式サイトでjarパッケージをダウンロードする方法:http://cxf.apache.org/download.html
  • (2)appication.xml、x-servlet.xmlとweb.xml
    二つのポイント:
  • は、controllerに関する自動注釈スキャンをx-servletに配置し、他のクラスの注釈スキャンをappication.xmlに配置する.そうでないと、@Webservice類で@Autowiredを使っている例がnullという問題があります.
  • @Webserviceでインターフェースを修飾し、クラス継承を実現します.実現類は@Component、@Controller、@Service、@Repositoryなどのコメントは使えませんが、xmlにbeanを配置することでspring注入が可能です.これはバグです.現在4.2.3バージョンはまだこのバグがあります.
  • 具体的なコード:appication.xml
    <?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/websocket" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd ">
        <!-- #   : 1.       ,       2. cxf webservice   -->
        <context:component-scan base-package="cn.paic.rep.pare" >
        <!--      xml      Controller   -->
             <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        </context:component-scan>
    
        <bean id="riskMailWebserviceImpl" class="cn.paic.rep.pare.webservice.RiskMailWebserviceImpl"/>
        <jaxws:endpoint id="riskMailService" implementor="#riskMailWebserviceImpl" address="/sendRiskMail"/>
    </beans>    
    x-servlet.xml
    <?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:context="http://www.springframework.org/schema/context" xmlns:task="http://www.springframework.org/schema/task" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd ">
        <!--   : xml      http        ,  Controller          -->
        <!-- #   : 1.   Controller        -->
        <context:component-scan base-package="cn.paic.rep.pare.controller" />
    </beans>
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      <display-name>x</display-name>
      <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>parisk.root</param-value>
      </context-param>
      <listener>
        <listener-class>
                org.springframework.web.util.Log4jConfigListener
            </listener-class>
      </listener>
      <context-param>
          <param-name>log4jConfigLocation</param-name>
          <param-value>classpath:log4j.properties</param-value>
      </context-param>
      <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>6000</param-value>
      </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
      </context-param>
      <context-param>
        <param-name>kmRootKey</param-name>
        <param-value>km.root</param-value>
      </context-param>
        <!--   apache cxf   webservice -->
        <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>/ws/*</url-pattern>
        </servlet-mapping>
        <!--   spring MVC   controller -->
        <servlet>
            <servlet-name>x</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>x</servlet-name>
            <url-pattern>/</url-pattern>
            <url-pattern></url-pattern>
        </servlet-mapping>
    </web-app>
    javaクラスコード
    package cn.paic.rep.pare.webservice;
    
    import javax.jws.WebParam;
    import javax.jws.WebService;
    
    import cn.paic.rep.pare.util.DataWrapper;
    @WebService  
    public interface RiskMailWebservice {  
    
        String sendRiskMail(@WebParam(name = "text")String text);  
    
    }  
    package cn.paic.rep.pare.webservice;
    
    
    import javax.jws.WebService;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Qualifier;
    
    import cn.paic.rep.pare.service.mail.risk.RiskForecastMailSender;
    import cn.paic.rep.pare.util.DataWrapper;
    
    @WebService//     @Component ,   xml  bean     
    public class RiskMailWebserviceImpl implements RiskMailWebservice {  
        @Autowired
        @Qualifier("serviceA ")
        ServiceA serviceA ;
        @Autowired
        @Qualifier("serviceB ")
        ServiceB serviceB;
    
        public String doSth(String text) {
           // 
        }  
    }  
    テストクラス
    package cn.paic.rep.pare.webservice;
    
    import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
    
    public class RiskMailWebserviceClient {
    
        public static void main(String[] args) {
            String address = "http://localhost:8080/${        }/ws/sendRiskMail";
            JaxWsProxyFactoryBean jwpFactory = new JaxWsProxyFactoryBean();
            jwpFactory.setAddress(address);
            jwpFactory.setServiceClass(RiskMailWebservice.class);
            RiskMailWebservice helloWorld = (RiskMailWebservice) jwpFactory.create();
            System.out.println(helloWorld.doSth("12"));
        }
    }