Hessian与Spring整合
2024-10-20 08:54:07
1.服务端与Spring的整合
1.1:web.xml中配置控制器
<servlet>
<servlet-name>hessian</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- Spring的配置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
</servlet> <servlet-mapping>
<servlet-name>hessian</servlet-name>
<url-pattern>/hessian/*</url-pattern>
</servlet-mapping>
1.2:编写接口和实现类
接口:
package com.hessian.service; import com.hessian.domain.User; public interface HessianFunc { public String getAddressByMobille(String phone); public void saveUser(User user); }
实现类:
package com.hessian.service; import com.hessian.domain.User; public class HsessianFuncImpl implements HessianFunc { @Override
public String getAddressByMobille(String phone) { String result="手机号"+phone+"的归属地是上海....."; return result;
} @Override
public void saveUser(User user) { System.out.println(user.getName()+"---"+user.getAge());
}
}
需要一个实体类:
package com.hessian.domain; import java.io.Serializable; public class User implements Serializable{ /**
* 一定要实例化
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private Integer age; public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
1.3:编写applicationContext.xml 交给Spring管理
<?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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="/mobile" class="org.springframework.remoting.caucho.HessianServiceExporter">
<!-- 接口类型 -->
<property name="serviceInterface" value="com.hessian.service.HessianFunc"></property>
<!-- 接口对象 -->
<property name="service" ref="HsessianFuncImpl"></property>
</bean> <bean id="HsessianFuncImpl" class="com.hessian.service.HsessianFuncImpl"></bean> </beans>
访问地址:http://localhost:8080/HessionSpringServer/hessian/mobile
2.客户端与Spring整合:
2.1:因为要用到User和接口,这里包装成jar包即可
2.2:配置applicationContext.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"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--创建代理工厂的核心对象-->
<bean id="hessianProxy" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
<!--服务接口-->
<property name="serviceInterface" value="com.hessian.service.HessianFunc"></property>
<!--服务地址-->
<property name="serviceUrl" value="http://localhost:8080/HessionSpringServer/hessian/mobile"></property>
</bean>
</beans>
2.3:编写测试代码:
package com.hessian.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hessian.domain.User;
import com.hessian.service.HessianFunc; public class TestHessian {
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); HessianFunc func = (HessianFunc) ac.getBean("hessianProxy"); String mobille = func.getAddressByMobille("1888888888"); System.out.println(mobille); User user = new User();
user.setAge(12);
user.setName("jack"); func.saveUser(user);
}
}
最新文章
- sicily 猴子选大王
- .net core 基本概念
- 关于使用FusionCharts生成图表时出现invalid xml data错误提示的解决方法
- python:爬虫
- Linux的学习--配置LNMP环境
- Eclipse PHP Studio(EPP)
- Unity 3D 关于给APK包加广告的流程
- SQLServer获取随机数据
- PHP标签的格式
- jQuery_基础
- C++STL中map容器的说明和使用技巧(杂谈)
- 2017ecjtu-summer training # 11 POJ 2492
- java之SpringMVC的controller配置总结
- M1-Flask-Day4
- Linq 中的 in 与 not in 的使用
- linux脚本文件执行的方法之间的区别
- linux 安装二进制包程序一般步骤
- Linux内核分析(第五周)
- linux netcat命令使用技巧
- CentOS rar
热门文章
- PHP实现自己活了多少岁
- Scala之模式匹配(Patterns Matching)
- 构造三层时报错“程序 “D:\MyTest\....”不包含适合于入口点的静态";Main";方法”
- VLC 媒体播放器
- pythpn的zip函数
- MD5,SHA256,时间戳获取
- java 抽象类实现接口
- C++ Primer笔记14_面向对象程序设计
- git发布代码到github
- make menuconfig 时出现 mixed implicit and normal rules: deprecated syntax