`
yanwenhan
  • 浏览: 114052 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring + XFire 构建webservic 客户端根据服务地址创建客户端调用

阅读更多
example-client.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="baseService" class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean" lazy-init="false" abstract="true"/>
	<bean id="echoService" parent="baseService">
		<property name="serviceClass">
			<value>com.hyw.example.Echo</value>
		</property>
		<property name="wsdlDocumentUrl">
			<value>http://localhost:8080/MyWebService/EchoService?wsdl</value>
		</property>
	</bean>
	<bean id="userService" parent="baseService">
		<property name="serviceClass">
			<value>com.hyw.example.UserService</value>
		</property>
		<property name="wsdlDocumentUrl">
			<value>http://localhost:8080/MyWebService/UserService?wsdl</value>
		</property>
	</bean>
</beans>


WebServiceClientTest.java文件
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hyw.example.Echo;

public class WebServiceClientTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		WebServiceClientTest test = new WebServiceClientTest();
		test.client();
	}
	
	public void client() {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("example-client.xml");
		Echo echoService = (Echo) ctx.getBean("echoService");
		System.out.println(echoService.echo("根据服务地址创建客户端调用程序"));
	}

}



UserWebServiceClient.java文件
package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hyw.example.User;
import com.hyw.example.UserService;

public class UserWebServiceClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		
		UserWebServiceClient test = new UserWebServiceClient();
		test.client();

	}
	
	public void client() throws Exception {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("example-client.xml");
		UserService userService = (UserService)ctx.getBean("userService");
		
		System.out.println("================== getUser() ==================");
		User user = userService.getUser("用户1");
		System.out.println(user);
		
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics