Spring中的远程访问和web服务
一、介绍
目前,Spring提供对下面四种远程访问技术的支持:
package com.logcd.server.service;public interface IHelloService { public String doHello(String name); }package com.logcd.server.service.impl;import com.logcd.server.service.IHelloService;public class HelloService implements IHelloService{public String doHello(String name) {return "Hello , " + name; }}<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet><servlet-name>Hessian</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:Hessian-servlet.xml </param-value></init-param> <load-on-startup>1</load-on-startup></servlet> <servlet-mapping><servlet-name>Hessian</servlet-name><url-pattern>/hessian/*</url-pattern></servlet-mapping></web-app>
<?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="helloService" ref="helloService"/><property name="serviceInterface"><value>com.logcd.server.service.IHelloService</value></property></bean></beans>
<?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="helloService" name="code">package com.logcd.client.test;import java.net.MalformedURLException;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.caucho.hessian.client.HessianProxyFactory;import com.logcd.server.service.IHelloService;public class HessianTest {public static void main(String args[]){clientSpringTest();}public static void clientSpringTest(){ApplicationContext context= new ClassPathXmlApplicationContext("Hessian-client.xml"); IHelloService helloService = (IHelloService)context.getBean("helloService"); System.out.println(helloService.doHello("logcd")); }public static void clientJavaTest(){String url = "http://localhost:8080/hessian/helloService"; HessianProxyFactory factory = new HessianProxyFactory(); try {IHelloService helloService =(IHelloService)factory.create(IHelloService.class, url);System.out.println(helloService.doHello("logcd")); } catch (MalformedURLException e) {e.printStackTrace();}}}<?xml version="1.0" encoding="UTF-8"?><web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet><servlet-name>httpInvoker</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:httpinvoker-servlet.xml </param-value></init-param> <load-on-startup>1</load-on-startup></servlet> <servlet-mapping><servlet-name>httpInvoker</servlet-name><url-pattern>/httpInvoker/*</url-pattern></servlet-mapping></web-app>
<?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="helloService" lazy-init="false"> <property name="service"> <ref bean="helloService"/> </property> <property name="serviceInterface"> <value>com.logcd.server.service.IHelloService</value> </property> </bean> </beans>
<?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="helloService" /></property></bean>
package com.logcd.client.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;import com.logcd.server.service.IHelloService;public class HttpInvokerTest {public static void main(String args[]){clientJavaTest();}public static void clientSpringTest(){ApplicationContext context= new ClassPathXmlApplicationContext("httpinvoker-client.xml"); IHelloService helloService = (IHelloService)context.getBean("helloService"); System.out.println(helloService.doHello("logcd")); }public static void clientJavaTest(){String url = "http://localhost:8080/httpInvoker/helloService"; HttpInvokerProxyFactoryBean factory = new HttpInvokerProxyFactoryBean (); factory.setServiceInterface(IHelloService.class);factory.setServiceUrl(url);factory.afterPropertiesSet();IHelloService helloService = (IHelloService)factory.getObject();System.out.println(helloService.doHello("logcd")); }}