开发者社区> 问答> 正文

Spring的rmi服务 可以不使用接口,直接代理类可以不

服务对象不想抽象出服务接口,我该怎么做啊

展开
收起
a123456678 2016-03-16 17:14:59 2229 0
1 条回答
写回答
取消 提交回答
  • Spring的rmi服务通过动态代理来做的,

    没有接口,就把动态代理那部分拿掉,如下SayHelloImpl为服务端的类,你拿过来,方法是say,参数类型String

    import java.lang.reflect.AccessibleObject;
    import java.lang.reflect.Method;
     
    import org.aopalliance.intercept.MethodInvocation;
    import org.bestvail.rmi.server.SayHelloImpl;
    import org.springframework.remoting.rmi.RmiClientInterceptor;
     
    public class Main {
        public static void main(String[] args) throws Throwable {
            RmiClientInterceptor itr = new RmiClientInterceptor();
            itr.setServiceUrl("rmi://localhost:1021/serverRmiTest");
            MethodInvocation mi = new ExMethodInvocation(SayHelloImpl.class, "say", new Class<?>[]{String.class}, new Object[]{"vidy"});
            System.out.println(itr.invoke(mi));
        }
        public static class ExMethodInvocation implements MethodInvocation{
            private Class<?> obj;
            private Class<?>[] parameterTypes;
            private String method;
            private Object[] parameter;
            public ExMethodInvocation(Class<?> obj,String method,Class<?>[] parameterTypes,Object[] parameter ) {
                this.obj = obj;
                this.method = method;
                this.parameterTypes = parameterTypes;
                this.parameter = parameter;
            }
            public Object[] getArguments() {
                return parameter;
            }
     
            public AccessibleObject getStaticPart() {
                return null;
            }
     
            public Object getThis() {
                return null;
            }
     
            public Object proceed() throws Throwable {
                return null;
            }
     
            public Method getMethod() {
                try {
                    return obj.getDeclaredMethod(method, parameterTypes);
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                }
                return null;
            }
             
        }
    };
    2019-07-17 19:04:08
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖社区特邀专家徐雷Java Spring Boot开发实战系列课程(第20讲):经典面试题与阿里等名企内部招聘求职面试技巧 立即下载
微服务架构模式与原理Spring Cloud开发实战 立即下载
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载

相关实验场景

更多