java hessian和spring 结合使用

服务器端配置和代码

pom 配置

<dependency>
    <groupId>com.caucho</groupId>
    <artifactId>hessian</artifactId>
    <version>4.0.38</version>
</dependency>


web.xml 配置


<!-- Hessian通过Servlet提供远程服务,需要将某个匹配的模式映射到hessian服务中, -->  
<!-- spring的dispatcherServlet能完成此功能,DispatcherServlet可将匹配模式的请求转发到Hessian服务, -->  
<!-- web.xml只是定义了“请求转发器”,该转发器将匹配/remoting/*的请求截获, 转发给context的bean处理。 -->  
<!-- 而HessianServiceExporter提供bean服务。 -->  
<servlet>  
    <servlet-name>hessian-servlet</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</servlet-name>  
    <url-pattern>/remoting/*</url-pattern>  
</servlet-mapping>  
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext-base.xml</param-value>
    </context-param>



编写接口

public interface UserInfo {


String GetUser(int userid);
}

实现类

@Component("userInfoImplRome")
public class UserInfoImpl implements UserInfo {


public String GetUser(int userid) {
// TODO Auto-generated method stub
return userid+"ss";
}


}


hessian接口注册

hessian-servlet.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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-4.1.xsd">
  <!-- 用户服务 -->  
    <bean name="/userClientServiceRemote" class="org.springframework.remoting.caucho.HessianServiceExporter">  
        <property name="service" ref="userInfoImplRome"/>  
        <property name="serviceInterface" value="com.hisan.domel.UserInfo"/>  
    </bean>
    </beans>


服务器端的配置和简单的代码完成


客户端配置和代码

spring 配置

<!-- 用户接口 -->  
<bean id="userClientService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">  
    <property name="serviceUrl" value="http://127.0.0.1:8010/com.hesan/remoting/userClientServiceRemote" />  
    <property name="serviceInterface" value="com.hesan.domels.UserInfo" />  
     <property name="chunkedPost" value="false" />  
    <property name="overloadEnabled" value="true" />
</bean>

Controller 调用

@Controller
public class OrderInfo {


@Resource(name="userClientService")
private UserInfo userClientService;  

@RequestMapping("getorder")
@ResponseBody
public String GetOrder(){
System.out.println(userClientService.GetUser(7788));
return "98568564546546";
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值