1简介
Spring HTTP Invoker是spring框架中的一个远程调用模型,执行基于HTTP的远程调用,也就是说,可以通过防火墙,并使用java的序列化机制在网络间传递对象。客户端可以很轻松的像调用本地对象一样调用远程服务器上的对象,要注意的一点是,服务端、客户端都是使用Spring框架。
2示例
下面为自己简单布置的一个示例,用于移动端使用管理后台数据库
准备:注意要引入spring-web的jar包,其他jar包就不介绍了…
2.1服务端
接口
package com.dcXt.mobile.service.ba;
import java.util.List; import com.dcXt.business.entity.Tb01;
public interface BaMobileService{
boolean save(Tb01 obj);
} |
实现类
采用了注解配置bean(提醒一下,别忘了配置bean)
package com.dcXt.mobile.service.ba;
import javax.annotation.Resource; import org.springframework.orm.hibernate3.HibernateTemplate; import org.springframework.stereotype.Component;
import com.dcXt.business.entity.Tb01;
@Component("baMobileServiceImpl") public class BaMobileServiceImpl implements BaMobileService{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } @Resource public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } @Override public boolean save(Tb01 obj) { hibernateTemplate.saveOrUpdate(obj); System.out.println(obj.toString()); return true; } } |
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- DispatcherServlet --> <servlet> <servlet-name>service</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-service.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>service</servlet-name> <url-pattern>*.service</url-pattern> </servlet-mapping>
<!-- spring入口 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:applicationContext.xml </param-value> </context-param>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list>
</web-app>
|
classpath:applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="baMobileService" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter"> <property name="service" ref="baMobileServiceImpl" /> <property name="serviceInterface" value="com.dcXt.mobile.service.ba.BaMobileService"> </property> </bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/ba.service">baMobileService</prop> </props> </property> </bean>
</beans> |
说明:客户端访问地址/ba.service;从而调用这个baMobileService,客户端接口和服务器端一致,相当于可以直接用这个baMobileServiceImpl
2.2客户端
接口(与服务器端完全一致,包过包路径)
package com.dcXt.mobile.service.ba;
import com.dcXt.business.entity.Tb01;
public interface BaMobileService{
boolean save(Tb01 obj);
} |
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="baMobileService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"> <property name="serviceUrl"> <value>http://127.0.0.1:8081/dcXt/ba.service</value> </property> <property name="serviceInterface" value="com.dcXt.mobile.service.ba.BaMobileService"> </property> </bean>
<bean id="baAction" class="com.dcXt.business.action.BaAction" scope="prototype"> <property name="baMobileService" ref="baMobileService"></property> </bean> </beans> |
说明:Action直接使用baMobileService即可
3总结
注意好服务器端和客户端写法,客户端和服务器端同样的接口,然后客户端就通过接口直接调用服务器端的实现类