Spring教程-使用Spring的HTTP Invoker实例
使用Spring的HTTP Invoker实例
Spring提供了自己的远程服务实现,称为HttpInvoker。它可以用于HTTP请求,而不是RMI,并且在防火墙之间工作得很好。
通过HttpInvokerServiceExporter和HttpInvokerProxyFactoryBean类,我们可以实现Spring的HTTP Invoker提供的远程服务。
Http Invoker和其他远程技术
您可以使用许多远程技术,让我们看看哪种对您来说最好。
Http Invoker与RMI
RMI使用JRMP协议,而Http Invoker使用HTTP协议。由于企业应用程序主要使用http协议,因此最好使用HTTP Invoker。RMI也比HTTP Invoker有一些安全问题。HTTP Invoker在防火墙之间工作得很好。
Http Invoker与Hessian和Burlap
HTTP Invoker是Spring框架的一部分,但Hessian和Burlap是专有的。所有这些在防火墙之间都可以很好地工作。Hessian和Burlap可以在其他语言(如.Net和PHP)中集成,但HTTP Invoker不能。
Spring HTTP Invoker示例
要创建一个简单的Spring HTTP Invoker应用程序,您需要创建以下文件。
- Calculation.java
- CalculationImpl.java
- web.xml
- httpInvoker-servlet.xml
- client-beans.xml
- Client.java
1) Calculation.java
这是一个简单的接口,包含一个cube方法。
package cn.javatiku;
public interface Calculation {
int cube(int number);
}
2) CalculationImpl.java
这个类提供了Calculation接口的实现。
package cn.javatiku;
public class CalculationImpl implements Calculation{
public int cube(int number) {
return number*number*number;
}
}
3) web.xml
在这个xml文件中,我们将DispatcherServlet定义为前端控制器。如果任何请求后面跟着.http扩展名,它将被转发到DispatcherServlet。
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd">
<servlet>
<servlet-name>httpInvoker</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>httpInvoker</servlet-name>
<url-pattern>*.http</url-pattern>
</servlet-mapping>
</web-app>
4) httpInvoker-servlet.xml
这个文件必须创建在WEB-INF文件夹内。它的名称必须是servletname-servlet.xml。它为CalculationImpl和HttpInvokerServiceExporter定义了bean。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean" class="cn.javatiku.CalculationImpl"></bean>
<bean name="/Calculation.http"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="calculationBean"></property>
<property name="serviceInterface" value="cn.javatiku.Calculation"></property>
</bean>
</beans>
5) client-beans.xml
在这个xml文件中,我们为HttpInvokerProxyFactoryBean定义了bean。您需要定义该类的两个属性。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="calculationBean"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://localhost:8888/httpinvoker/Calculation.http"></property>
<property name="serviceInterface" value="cn.javatiku.Calculation"></property>
</bean>
</beans>
6) Client.java
这个类获取Calculation的实例并调用方法。
package cn.javatiku;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
Calculation calculation = (Calculation)context.getBean("calculationBean");
System.out.println(calculation.cube(5));
}
}
输出
Output: 125
如何运行此示例
启动和部署项目,这里我们假设服务器在8888端口号上运行。如果端口号不同,请在client-beans.xml中更改serviceURL。
然后,编译并运行Client.java文件。
基于Web的客户端
在上面的示例中,我们使用了基于控制台的客户端。我们还可以使用基于Web的客户端。您需要创建3个额外的文件。在这里,我们使用以下文件:
- ClientInvoker.java
- index.jsp
- process.jsp
ClientInvoker.java
它只定义了一个方法getCube(),返回给定数字的立方。
package cn.javatiku;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ClientInvoker {
public static int getCube(int number){
ApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
Calculation calculation = (Calculation)context.getBean("calculationBean");
return calculation.cube(number);
}
}
index.jsp
它创建一个表单来获取数字。
<form action="process.jsp">
Enter Number:<input type="text" name="number"/>
<input type="submit" value="cube" />
</form>
process.jsp
它创建一个表单来获取数字。
<jsp:include page="index.jsp"></jsp:include>
<hr/>
<%@page import="cn.javatiku.ClientInvoker"%>
<%
int number=Integer.parseInt(request.getParameter("number"));
out.print("cube of "+number+" is: "+ClientInvoker.getCube(number));
%>
输出: