熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring配置Hessian

2022-06-13   來源: Java開源技術 

  創建web工程並加載springhessian框架

  創建service:

  [java]

  public interface BasicService {

  public void setServiceName(String serverName)

  public String getServiceName()

  public User createUser()

  }

  創建service實現

  public class BasicServiceImpl implements BasicService {

  private String serviceName;

  @Override

  public void setServiceName(String serverName) {

  thisserviceName = serverName;

  }

  @Override

  public String getServiceName() {

  return thisserviceName;

  }

  @Override

  public User createUser() {

  return new User(zhangsan

  }

  }

  創建需要傳遞的對象

  [java]

  public class User implements Serializable {

  private static final long serialVersionUID = L;

  private String username;

  private String password;

  public User(String username String password) {

  thisusername = username;

  thispassword = password;

  }

  public String getUsername() {

  return username;

  }

  public void setUsername(String username) {

  thisusername = username;

  }

  public String getPassword() {

  return password;

  }

  public void setPassword(String password) {

  thispassword = password;

  }

  }

  配置webxml利用dispatchServlet處理請求

  [html]

  <?xml version= encoding=UTF?>

  <webapp xmlns:xsi=//XMLSchemainstance xmlns=/xml/ns/javaee xmlns:web=/xml/ns/javaee/webapp__xsd xsi:schemaLocation=/xml/ns/javaee /xml/ns/javaee/webapp__xsd id=WebApp_ID version=>

  <displayname>HessianSpringServer</displayname>

  <servlet>

  <servletname>remote</servletname>

  <servletclass>orgspringframeworkwebservletDispatcherServlet</servletclass>

  <initparam>

  <paramname>contextConfigLocation</paramname>

  <paramvalue>classpath:com/loujinhe/config/remoteservletxml</paramvalue>

  </initparam>

  <loadonstartup></loadonstartup>

  </servlet>

  <servletmapping>

  <servletname>remote</servletname>

  <urlpattern>/remote/*</urlpattern>

  </servletmapping>

  </webapp>

  配置remoteservletxml:

  [html]

  <?xml version= encoding=UTF?>

  <beans xmlns=/schema/beans

  xmlns:xsi=//XMLSchemainstance

  xsi:schemaLocation=/schema/beans

  /schema/beans/springbeansxsd>

  <bean id=basicService class=comloujinheserviceimplBasicServiceImpl/>

  <bean id=hessianRemote name=/HessianRemote class=orgspringframeworkremotingcauchoHessianServiceExporter>

  <property name=serviceInterface value=comloujinheserviceBasicService/>

  <property name=service ref=basicService/>

  </bean>

  </beans>

  創建客戶端調用工程並加載springhessian框架

  創建service和普通需要傳遞的對象

  配置remoteclientxml

  [html]

  <?xml version= encoding=UTF?>

  <beans xmlns=/schema/beans

  xmlns:xsi=//XMLSchemainstance

  xsi:schemaLocation=/schema/beans

  /schema/beans/springbeansxsd>

  <bean id=basicService class=comloujinheserviceimplBasicServiceImpl/>

  <bean id=hessianRemote name=/HessianRemote class=orgspringframeworkremotingcauchoHessianServiceExporter>

  <property name=serviceInterface value=comloujinheserviceBasicService/>

  <property name=service ref=basicService/>

  </bean>

  </beans>

  創建客戶端測試程序

  [java]

  public class RemoteTest {

  public static void main(String[] args) {

  ApplicationContext context = new ClassPathXmlApplicationContext(com/loujinhe/config/remoteclientxml

  BasicService basicService = (BasicService) contextgetBean(hessianRemoteCall

  basicServicesetServiceName(hello service

  Systemoutprintln(basicServicegetServiceName())

  Systemoutprintln(basicServicecreateUser()getUsername())

  Systemoutprintln(basicServicecreateUser()getPassword())

  }

  }

  啟動服務器執行客戶端測試程序結果如下

  hello service

  zhangsan

  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28147.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.