您的位置:首页 > 编程语言 > Java开发

spring_webservice_Hessian和Burlap配置示例

2013-10-07 13:56 465 查看
Hessian 是简化RMI的框架

 Hessian服务器端



Userinfo.java 
   package com.lilin.entity;

import java.io.Serializable;

public class Userinfo implements Serializable {

private String name;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

IUserinfoService.java

  package com.lilin.iservice;

import com.lilin.entity.Userinfo;

public interface IUserinfoService {

public void addUserinfo(Userinfo  userinfo) throws Exception;

}

UserinfoServiceImpl.java

package com.lilin.serviceimpl;

import com.lilin.entity.Userinfo;

import com.lilin.iservice.IUserinfoService;

public class UserinfoServiceImpl implements IUserinfoService{

public void addUserinfo(Userinfo  userinfo) throws Exception{

System.out.println("添加了一个新的用户"+userinfo.getName());

}

}

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
id="WebApp_ID" version="2.5">
  <display-name>spring_webservice</display-name>
  
 <!-- 加载spring begin-->
  <context-param>       
      <param-name>contextConfigLocation</param-name>       
      <param-value>classpath:applicationContext.xml</param-value>   
  </context-param>       
  <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  </listener>  
  <!-- 加载spring end-->
  
  <servlet>
    <servlet-name>remoting</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    <init-param>
      <param-name>contextConfigLocation</param-name>       
       <param-value>classpath:remoting-servlet.xml</param-value>
    </init-param>
</servlet> 
<servlet-mapping>
    <servlet-name>remoting</servlet-name>
    <url-pattern>/remoting/*</url-pattern> 
</servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

remoting-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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans ;
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop ;
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx ;
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ;
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache ;
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ;
">
  
 <bean id="userinfoService" class="com.lilin.serviceimpl.UserinfoServiceImpl"></bean>
 
 <bean name="/UserinfoService" class="org.springframework.remoting.caucho.HessianServiceExporter">
    <property na
112fc
me="service" ref="userinfoService"/>
    <property name="serviceInterface" value="com.lilin.iservice.IUserinfoService"/>
</bean>
  
     
</beans>

jar:

spring.*.jar 
com.springsource.com.caucho-3.2.1.jar

启动 ok

Hessian 客户端



Userinfo.java 同上

IUserinfoService.java 同上

SimpleObject.java

package com.lilin.test;

import com.lilin.entity.Userinfo;
import com.lilin.iservice.IUserinfoService;

public class SimpleObject {

private IUserinfoService iUserinfoService;

public IUserinfoService getiUserinfoService() {
return iUserinfoService;
}

public void setiUserinfoService(IUserinfoService iUserinfoService) {
this.iUserinfoService = iUserinfoService;
}

public void add(Userinfo name) throws Exception{

iUserinfoService.addUserinfo( name );
}

}

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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans ;
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop ;
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx ;
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ;
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/cache ;
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd ;
">
   
 
<bean id="accountService" class="org.springframework.remoting.caucho.HessianProxyFactoryBean">
    <property name="serviceUrl" value="http://localhost:8080/spring_webservice/remoting/UserinfoService"/>
    <property name="serviceInterface" value="com.lilin.iservice.IUserinfoService"/>
</bean>

<bean id="test" class="com.lilin.test.SimpleObject">
    <property name="iUserinfoService" ref="accountService"/>
</bean>

     
</beans>

App.java

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lilin.entity.Userinfo;
import com.lilin.test.SimpleObject;
 
public class App {

@org.junit.Test
public void test(){
 
ApplicationContext context =new  ClassPathXmlApplicationContext("applicationContext.xml");
SimpleObject object =  (SimpleObject) context.getBean("test");
 
try {
Userinfo userinfo = new Userinfo();
userinfo.setName("=>李林");
object.add(userinfo);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
}
     
}

Burlap 客户端

  上述都不变 remoting-servlet.xml 的ServiceExporter改为org.springframework.remoting.caucho.BurlapServiceExporter
服务端修改 

applicationContext.xml 的ProxyFactoryBean 为org.springframework.remoting.caucho.BurlapProxyFactoryBean

启动 ok
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring JAVA