0
点赞
收藏
分享

微信扫一扫

关于Spring的HttpInvoker

狐沐说 02-22 15:00 阅读 6

在使用Spring的HttpInvoker实现远程服务时,服务器端可以和Hessian很类似:

Hessian是这样:

  @Bean(name = "/helloService")
  public HessianServiceExporter hessianServiceExporter(HelloService helloService) {
    HessianServiceExporter exporter = new HessianServiceExporter();
    exporter.setService(helloService);
    exporter.setServiceInterface(HelloService.class);
    return exporter;
  }

HttpInvoker是这样:

@Bean(name = "/helloServiceHttpInvoker")
  public HttpInvokerServiceExporter serviceExporter(HelloService helloService) {
    HttpInvokerServiceExporter exporter = new HttpInvokerServiceExporter();
    exporter.setService(helloService);
    exporter.setServiceInterface(HelloService.class);
    return exporter;
  }

但在客户端调用时相差较大:

Hessian可以这样:

package com.example.client;

import com.caucho.hessian.client.HessianProxyFactory;
import com.example.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

@Configuration
public class HessianClient {
    public static void main(String[] args) throws Exception {
        String url = "http://localhost:8080/Gradle___com_example___deepseek_hessian_springmvc_web_1_0_SNAPSHOT_war__exploded_/helloService";
        HessianProxyFactory factory = new HessianProxyFactory();
        HelloService helloService = (HelloService) factory.create(HelloService.class, url);
        System.out.println(helloService.sayHello("World"));
    }
}

也可以这样:

package com.example.client;

import com.caucho.hessian.client.HessianProxyFactory;
import com.example.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.remoting.caucho.HessianProxyFactoryBean;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

@Configuration
public class HessianClient {
    static private String url = "http://localhost:8080/Gradle___com_example___deepseek_hessian_springmvc_web_1_0_SNAPSHOT_war__exploded_/helloService";
    @Bean
    public HessianProxyFactoryBean hs () {
        HessianProxyFactoryBean proxy = new HessianProxyFactoryBean();
        proxy.setServiceUrl(url);
        proxy.setServiceInterface(HelloService.class);
        return proxy;
    }
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(HessianClient.class);
        HelloService hello = context.getBean(HelloService.class);
        System.out.println(hello.sayHello("WWWWorld"));
    }
}

而HttpInvoker似乎只能用第二种方式,即先将Bean放入Context中:

@Bean
    public HttpInvokerProxyFactoryBean helloService() {
        HttpInvokerProxyFactoryBean factoryBean = new HttpInvokerProxyFactoryBean();
        factoryBean.setServiceUrl("http://localhost:8080/Gradle___spittr_war__exploded_/helloServiceHttpInvoker");
        factoryBean.setServiceInterface(HelloService.class);
        return factoryBean;
    }

然后在main中从上下文中取出Bean再访问:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(HessianClient.class);
        HelloService hello = context.getBean(HelloService.class);
        System.out.println(hello.sayHello("WWWWorld"));

举报

相关推荐

0 条评论