spring http invoker 实践

标签: spring http invoker | 发表时间:2014-01-12 05:17 | 作者:qiantujava
出处:http://blog.csdn.net

搞了半天,终于把一个 Spring http invoker 的小例子运行起来了。

服务器端:

UserServices接口:

package spring.services;

public interface UserServices {
	public void printUser();
}

UserServicesImpl实现类:

package spring.services;

public class UserServicesImpl implements UserServices {
	@Override
	public void printUser() {
		System.out.println("这里只是测试了一下。");
	}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<!-- 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>WEB-INF/applicationContext.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配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

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="httpService"
		class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
		<property name="service" ref="userServices" />
		<property name="serviceInterface" value="spring.services.UserServices">
		</property>
	</bean>
	
	<bean id="userServices" class="spring.services.UserServicesImpl" />
	
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">  
            <props>  
                  <prop key="/user.service">httpService</prop>  
            </props>  
        </property>  
	</bean>

</beans>

这样服务器端就搭建好了,等着客户端调用了。

地址为:http://服务器地址:端口/项目名/user.service

客户端:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	id="WebApp_ID" version="3.0">

	<!-- Spring配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>WEB-INF/applicationContext.xml</param-value>
	</context-param>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

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="httpService"
		class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
		<property name="serviceUrl">
			<value>http://10.6.176.253:8080/spring_client/user.service</value>
		</property>
		<property name="serviceInterface" value="spring.services.UserServices">
		</property>
	</bean>

</beans>

把地址改成你服务器的地址:http://服务器地址:端口/项目名/user.service

UserServlet:(随便一个servlet就可以了,action,jsp也可以)

package spring.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.support.WebApplicationContextUtils;

import spring.services.UserServices;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserServices userServices = (UserServices) WebApplicationContextUtils   
		        .getRequiredWebApplicationContext(   
		            request.getSession().getServletContext()).getBean("httpService");   
		  
		System.out.println("--------------------------");
		userServices.printUser();
		System.out.println("--------------------------");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
}

调用这个servlet的时候就会调用服务器端的
userServices.printUser();
了,当然,这方法是在服务器运行的,运行结果在服务器上面。

作者:qiantujava 发表于2014-1-11 21:17:02 原文链接
阅读:94 评论:0 查看评论