1.首先创建接口类:
package cn.itcast.cxf;
import javax.jws.WebService;
@WebService
public interface IHiService {
public String sayHi(String name);
}
2.然后,创建接口的实现类:
package cn.itcast.cxf;
public class HiServiceImpl implements IHiService{
@Override
public String sayHi(String name) {
System.out.println("正在调用sayHi()方法");
return "hi " +name;
}
}
3.配置cxf-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: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://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">
<!-- 引入CXF Bean定义如下,早期的版本中使用 -->
<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" />
<!-- 第一张发布方式简单发布方式(没有接口的发布方式) -->
<!-- id:唯一性标示 implementor:提供服务的类 adress:服务的请求URL-->
<jaxws:endpoint id = "helloService" implementor= "cn.itcast.cxf.HelloService" address = "/hello">
<!-- 消息拦截器 -->
<jaxws:inInterceptors>
<!--加入请求的消息拦截器 -->
<bean class = "org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<!-- 加入响应的消息拦截器 -->
<bean class = "org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
<!-- 第二种发布方式(带有接口的发布方式 )-->
<!-- id:唯一性标示 seviceClass:接口类型 adress:服务的请求URL-->
<jaxws:server id = "hiSercice" serviceClass = "cn.itcast.cxf.IHiService" address = "/hi">
<jaxws:serviceBean>
<bean class= "cn.itcast.cxf.HiServiceImpl"></bean>
</jaxws:serviceBean>
<!-- 消息拦截器 -->
<jaxws:inInterceptors>
<!--加入请求的消息拦截器 -->
<bean class = "org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<!-- 加入响应的消息拦截器 -->
<bean class = "org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:server>
</beans>
然后启动服务即可;
综上,在访问wsdl文件的过程中,我们可以发现在第一次访问的时候,速度总是比较慢,是由于是在第一次访问时,要读取cxf-servlet.xml的配置文件,而这个过程较慢,那么
我们能不能再项目启动的时候,就读取这个配置文件呢,这样的话,用户访问的时候,速度就比较快了。
可以修改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>CXF_3</display-name>
<!-- 通过上下文参数制定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置cxf框架的核心Servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<!-- 通过初始化参数配置cxf文件的位置 -->
<!--
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</init-param>
-->
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
其中的
<!-- 通过上下文参数制定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
这段代码说明要在项目启动时,读取cxf-servlet.xml文件,不过我们要把下面的
<pre name="code" class="html"><init-param>
<param-name>config-location</param-name>
<param-value>classpath:cxf-servlet.xml</param-value>
</init-param>
代码注释掉,防止重复读取配置文件,影响效率,这样的话,用户的访问速度将会加快很多。