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); } }