标签:
服务器端开发,分为以下3步:
第一步,在服务端定义好接口和实现
public interface EchoService {
String say(String var1);
}
import com.caucho.hessian.server.HessianServlet;
import org.springframework.stereotype.Service;
@Service
public class EchoServiceImpl implements EchoService {
public String say(String msg) {
return "Hello:"+msg;
}
}
第二步,在spring配置文件中暴露服务
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:component-scan base-package="com.guge.group" />
<context:annotation-config />
<bean id="echoService" class="com.guge.group.service.EchoServiceImpl">
</bean>
<bean name="/echoService" class="org.springframework.remoting.caucho.HessianServiceExporter">
<property name="service" ref="echoService"/>
<property name="serviceInterface" value="com.guge.group.service.EchoService"/>
</bean>
</beans>
第三步,在web.xml中配置servlet
<servlet>
<servlet-name>echo-dispatch</servlet-name>
<servlet-class>
com.caucho.hessian.server.HessianServlet
</servlet-class>
<init-param>
<param-name>service-class</param-name>
<param-value>com.guge.group.service.EchoServiceImpl</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>echo-dispatch</servlet-name>
<url-pattern>/call/echo</url-pattern>
</servlet-mapping>
客户端调用服务,有以下两种方式:
第一种,通过HessianProxyFactory调用
import com.caucho.hessian.client.HessianProxyFactory;
import com.guge.group.service.EchoService;
public class BlogList {
public static void main(String[] args){
try {
String url = "http://localhost:2011/call/user";
HessianProxyFactory factory = new HessianProxyFactory();
EchoService echoService = (EchoService) factory.create(EchoService.class,url);
System.out.println(echoService.say("guest"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
第二种,通过spring的bean配置获取服务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="echoService " class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<property name="serviceUrl" value="http://localhost:2011/call/echo " />
<property name="serviceInterface" value="com.guge.group.service.EchoService"/>
</bean>
</beans>
import com.guge.group.service.EchoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by google on 15/5/22.
*/
public class BlogList {
public static void main(String[] args){
ApplicationContext contex = new ClassPathXmlApplicationContext(
"config/spring/appcontext-client.xml");
EchoService echo = (EchoService)contex.getBean("echoService");
System.out.println(echo.say("nb"));
}
}
标签:
原文地址:http://www.cnblogs.com/showxiaxian/p/4527881.html