前提:我使用的是spring框架
一、在aaa-servlet.xml中加入1、2、3和4
在beans中加入1和 2
1、xmlns:jaxws=" http://cxf.apache.org/jaxws "
2、xsi:schemaLocation="http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
(加入是为了能使用jaxws标签)
3、
< import resource="classpath*:META-INF/cxf/cxf.xml"/ >
< import resource="classpath*:META-INF/cxf/cxf-servlet.xml"/ >
4、
< jaxws:endpoint id="cxfService" address="/CxfSer"
implementor="com.qnoa.webservice.CxfServiceImpl"/ >
(implementor 注入的是一个继承接口的类(自定义的,类名是CxfServiceImpl)
com.qnoa.webservice是自定义的包名,红色文字都是自定义的。)
二、在web.xml中加入5、6、7和8
5、定义上下文
< context-param >
< param-name >contextConfigLocation< /param-name >
< param-value >
WEB-INF/aaa-servlet.xml
< /param-value >
< /context-param >
6、监听上下文
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
7、(因为用的是apache的解决方案CFX,所以必须使用
CXFServlet
这个名字)
<servlet>
<servlet-name >CXFServlet</servlet-name>
<servlet-class >org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup >2</load-on-startup>
</servlet >
8、
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/* </url-pattern>
</servlet-mapping>
三、java程序部分
9、定义一个接口类
@WebService
public interface CxfService
{
public String getSharefileList( @WebParam(name = "userName") String userName);
}
10、定义方法类
@WebService(endpointInterface = "com.qnoa.webservice.CxfService")
(endpointInterface必须指向接口类)
public class CxfServiceImpl implements CxfService
{
public String getSharefileList(String userName)
{
String name = userName + "123";
return name ;
}
}
11、测试类
public class CXlin
{
public static void main(String args[])
{
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(CxfService.class);
factory.setAddress("http://127.0.0.130:8080/qnew/service/CxfSer");
CxfService cxfService = (CxfService)factory.create();
String user = cxfService.getSharefileList("bushi");
}
}
测试就会得到bushi123
qnew是工程的名字,CxfSer是在aaa-servlet.xml中自定义的,service是在web.xml中自定义的