前面我们已经介绍了简单的jax-ws服务程序,关于SEI接口,接口的实现还有服务的发布。这边将介绍另外一种服务的发布形式——基于spring容器。
其中SEI接口和接口实现类代码不改变,参见上一次的apachecxf笔记。
在原有的项目的src目录先创建server-beans.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"
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">
<jaxws:endpoint
id="ProjectManager" implementor="demo.cxf.helloworld.HelloWorldImpl"
address="http://localhost:8080/HelloWorld"/>
</beans>
编写测试代码:
SpringServer类:
package demo.cxf.helloworld.server;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringServer {
public static void main(String[] args) throws IOException {
SpringServer springServer = new SpringServer();
springServer.startServer();
System.out.println("Startting ready...");
System.in.read();//按任意键退出
System.out.println("Server exiting...");
System.exit(0);//程序退出
}
public void startServer(){
System.out.println("Starting Server.....");
//通过spring容器读取服务配置信息,创建服务
new ClassPathXmlApplicationContext(new String[]{"classpath:server-beans.xml"});
}
}
运行,控制台为:
浏览器访问:http://localhost:8080/HelloWorld?wsdl