java hessian和spring 结合使用
2017-04-10 11:53
381 查看
服务器端配置和代码
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";
}
}
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";
}
}
相关文章推荐
- 结合SPRING 2.0使用JAVA PERSISTENCE API
- hessian与spring的完美结合(使用spring注解自动注入bean)
- 使用PHPHessian调用Java Spring Hessian暴露服务­
- Java Jetty 结合 hessian 使用小例子
- 使用Spring AOP结合自定义Java注解实现动态数据源设置
- 结合SPRING 2.0使用JAVA PERSISTENCE API
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- ActiveMQspring结合使用
- Java -- 在Eclipse上使用Spring
- 关于hibernate与spring的结合使用中的配置文件
- 面向Java开发人员的Ajax: 结合Direct Web Remoting使用Ajax
- struts,hibernate , spring结合使用
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- Log4j结合spring的使用配置
- Java -- 在Eclipse上使用Spring
- .Net使用Hessian调用JAVA的函数
- java:log4j学习(2)-转帖:Common-logging 与 Log4j的结合使用
- Java -- 在Eclipse上使用Spring
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax
- 面向 Java 开发人员的 Ajax: 结合 Direct Web Remoting 使用 Ajax----数据序列化不可能比这更简单了!