2016-12-19 00:00:00嘉辉 J2EE培训
spring框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架。下面yjbys小编为大家准备了关于集成spring与Web容器教程,欢迎阅读。
1.创建HelloWorld 接口类
package com.googlecode.garbagecan.cxfstudy.helloworld;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
@WebResult String sayHi(@WebParam String text);
}
2.创建HelloWorld实现类
package com.googlecode.garbagecan.cxfstudy.helloworld;
public class HelloWorldImpl implements HelloWorld {
public String sayHi(String name) {
String msg = "Hello " + name + "!";
return msg;
}
}
3.修改web.xml文件
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
cxfstudy
cxf
org.apache.cxf.transport.servlet.CXFServlet
1
cxf
/ws/*
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath*:**/spring.xml
4.创建spring配置文件并放在classpath路径下
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
5.创建测试类
package com.googlecode.garbagecan.cxfstudy.helloworld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
HelloWorld helloworld = (HelloWorld)context.getBean("helloworldClient");
System.out.println(helloworld.sayHi("kongxx"));
}
}
6.测试
6.1 首先启动tomcat或者使用maven的jetty,并访问http://localhost:9000/ws/HelloWorld?wsdl来验证web service已经启动并且生效;
6.2 然后运行测试类来验证web service。
863
人