Spring HttpInvoker -- HttpRequestHandlerServlet 暴露服务

Spring HttpInvoker使用JDK动态代理,仅支持接口服务

参考博客:http://blog.csdn.net/java2000_wl/article/details/7441255

一: 暴露服务(HttpRequestHandlerServlet 暴露服务)

1.Server接口与实现

package com.feng.service;

public interface HttpService {

    /**
     * @param name
     * @return
     */
     public String sayHello(String name);

     public String drinkTea(String teaType);  
}
package com.feng.service.impl;

import com.feng.service.HttpService;
@Service("httpService")
public class HttpServiceImpl implements HttpService {

    public String sayHello(String name) {
        return "hello:  " + name;
    }

        public String drinkTea(String teaType){
                return teaType + "is perfect!";
        }
}

2.Server配置文件

web.xml
    <!-- ContextLoaderListener  -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/applicationContext-server.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- HttpRequestHandlerServlet -->
    <!-- servlet-name 必须和暴露的seviceProxy bean id相同    -->
    <servlet>
        <servlet-name>httpServiceExporter</servlet-name>
        <servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>httpServiceExporter</servlet-name>
        <url-pattern>/httpService.service</url-pattern>
    </servlet-mapping>
applicationContext-server.xml
    <!--  和web.xml servlet-name 一致 -->    
    <bean id="httpServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" >
        <property name="service"             ref="httpService" />
        <property name="serviceInterface"     value="com.feng.service.HttpService" />
    </bean>
        

3.客户端

package com.feng.service;

public interface HttpService {

    /**
     * @param name
     * @return
     */
     public String sayHello(String name);

     public String drinkTea(String teaType);  
}

据测试接口的包名和类名可以不一致,但方法必须一致;javabean包名类名属性方法必须一致。

applicationContext-client.xml
    <bean id="httpServiceI"  class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
        <property name="serviceUrl"         value="http://localhost:8080/httpInvoker/httpService.service" />
        <property name="serviceInterface"     value="com.feng.service.HttpService"/>
    </bean>
public class ClientMain {
    
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-client.xml");
        ISayHelloService httpService = applicationContext.getBean("httpServiceI", HttpService.class);
        System.out.println(httpService.doSayHello("王五"));
    }
}

或者使用注解

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(Locations="../applicationContext.xml") 
public class HttpClientTest(){

    @Resource
    private HttpService httpService;

@Test
public void testClient(){ System.out.println(httpService.drinkTea("red tea")); } }

 

posted @ 2014-06-13 14:41  fenglie  阅读(412)  评论(0)    收藏  举报
版权所有,转载声明