Spring HttpInvoker -- 通过MVC暴露服务
Spring 通过JDK的动态代理提供远程服务,轻量级,结构简单;但只对接口提供远程服务
如要对类提供动态代理,需要使用cglib。
参考文章:http://blog.csdn.net/java2000_wl/article/details/7441212
一: 暴露服务(Spring mvc暴露服务),controller提供服务(使用注解)
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 <!-- DispatcherServlet --> <servlet> <servlet-name>Spring-DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/applicationContext-server.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>Spring-DispatcherServlet</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping>
applicationContext-server.xml <bean id="httpServiceExporter" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter" > <property name="service" ref="httpService" /> <property name="serviceInterface" value="com.feng.service.HttpService" /> </bean> <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="urlMap"> <map> <entry key="/httpService.service" value-ref="httpServiceExporter" /> </map> </property> </bean>
3.Client端调用(需要将接口和bean打包发送到客户端)---eclipse export jar 选择bean and interface
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 { private static final DefaultListableBeanFactory beanFactory; static { beanFactory = new DefaultListableBeanFactory(); BeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory); Resource resource = new ClassPathResource("applicationContext-client.xml"); beanDefinitionReader.loadBeanDefinitions(resource); } public static void main(String[] args) { HttpService httpService = beanFactory.getBean("httpServicei", HttpService.class); System.out.println(httpService.SayHello("王五")); } }
或者使用注解
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(Locations="../applicationContext.xml") public class HttpClientTest(){ @Resource private HttpService httpService; public void testClient(){ System.out.println(httpService.drinkTea("red tea")); } }
作者:fenglie
专注于JAVAEE开发,热爱开源项目
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

浙公网安备 33010602011771号