隨筆-109  評論-187  文章-25  trackbacks-0

          1

          Package

          net.loocky.rmi.spring

          |-------BusinessService.java

          |-------Service.java

          |-------ServiceClient.java

          |-------ServiceImpl.java

          |-------ServiceServer.java

          |-------rmi-config.xml

          ?

          (1)??? Service.java? is interface

          package net.loocky.rmi.spring;

          ?

          import java.rmi.Remote;

          import java.rmi.RemoteException;

          ?

          ?

          public interface Service? extends Remote{

          ?

          ?????? public String getName()throws RemoteException ;

          ?

          ?????? public void setName(String name)throws RemoteException ;

          ?

          }

          (2)??? BusinessService.java is business interface too ,not extends remote

          package net.loocky.rmi.spring;

          public interface BusinessService {

          ?????? public String getName() ;

          ?

          ?????? public void setName(String name);

          }

          (3)??? ServiceImpl

          package net.loocky.rmi.spring;

          ?

          public class ServiceImpl implements Service, BusinessService {

          ?????? private String name = "111";

          ?

          ?????? public String getName() {

          ????????????? System.out.println(name);

          ????????????? return name;

          ?????? }

          ?

          ?????? public void setName(String name) {

          ????????????? this.name = name;

          ?

          ?????? }

          ?

          }

          (4)??? ServiceServer

          package net.loocky.rmi.spring;

          ?

          import org.springframework.context.ApplicationContext;

          import org.springframework.context.support.ClassPathXmlApplicationContext;

          ?

          public class ServiceServer {

          ?????? public static void main(String[] args) throws Exception {

          ????????????? //load the config file ,and spring will deploy the service automatically

          ????????????? ApplicationContext ctx = new ClassPathXmlApplicationContext(

          ??????????????????????????? "rmi-config.xml");

          ?????? }

          }

          (5)??? ServiceClient

          package net.loocky.rmi.spring;

          ?

          import org.springframework.context.ApplicationContext;

          import org.springframework.context.support.ClassPathXmlApplicationContext;

          ?

          public class ServiceClient {

          ?????? public static void main(String[] args) throws Exception {

          ????????????? // HessianProxyFactory proxyFactory = new HessianProxyFactory();

          ????????????? ApplicationContext ctx = new ClassPathXmlApplicationContext(

          ??????????????????????????? "rmi-config.xml");

          ????????????? BusinessService service = (BusinessService) ctx

          ??????????????????????????? .getBean("myServiceClient");

          ?????? // 下面的方式是不可行的,因為 SPRING 的, CGLIB enhance 了對象

          // BusinessService service =

          ????????????? // (BusinessService)Naming.lookup("rmi://localhost:1099/aaaa");

          ????????????? // service.setName("sssssssssssssss");

          ????????????? System.out.println(service.getName());

          ?????? }

          ?

          }

          (6)??? rmi-config.xml

          <? xml version = "1.0" encoding = "UTF-8" ?>

          <! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

          ??? "http://www.springframework.org/dtd/spring-beans.dtd" >

          ??? < beans >

          ??? < bean id = "myService" class = "net.loocky.rmi.spring.ServiceImpl" />

          ??? < bean id = "serviceExporter" class = "org.springframework.remoting.rmi.RmiServiceExporter" >

          ??????? < property name = "serviceName" >

          ??????????? < value > aaaa </ value >

          ??????? </ property >

          ??????? < property name = "service" >

          ??????? ???? < ref bean = "myService" />

          ??????? </ property >

          ??????? < property name = "serviceInterface" >

          ??????????? < value > net.loocky.rmi.spring.BusinessService </ value >

          ??????? </ property >

          ??????? < property name = "registryPort" >

          ??????????? < value > 1099 </ value >

          ??????? </ property >

          ??? </ bean >

          ??? < bean id = "myServiceClient" class = "org.springframework.remoting.rmi.RmiProxyFactoryBean" >

          ?????? < property name = "serviceUrl" >

          ?????????? < value > rmi://127.0.0.1:1099/aaaa </ value >

          ?????? </ property >

          ?????? < property name = "serviceInterface" >

          ?????????? < value > net.loocky.rmi.spring.BusinessService </ value >

          ?????? </ property >

          ?

          ??? </ bean >

          </ beans >

          ?

          ?

          posted on 2006-05-29 14:45 小小程序程序員混口飯吃 閱讀(693) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 电白县| 都江堰市| 吉首市| 乐山市| 革吉县| 北京市| 同仁县| 丰台区| 雷州市| 内乡县| 双鸭山市| 镇赉县| 丹寨县| 宜黄县| 临城县| 东安县| 滦南县| 浦县| 巴彦淖尔市| 孝义市| 阜阳市| 克拉玛依市| 宝丰县| 武清区| 平阳县| 中山市| 南木林县| 积石山| 库尔勒市| 河西区| 通州区| 都兰县| 互助| 莫力| 普安县| 汕头市| 都江堰市| 湄潭县| 九江县| 屯门区| 双流县|