您的位置:首页 > 理论基础 > 计算机网络

Spring HTTP Service

2014-01-07 22:59 429 查看
简述:

基于Spring MVC, 使用Http Service Invoke远程调用方法

(参考: http://blog.csdn.net/hanqunfeng/article/details/4303127)
步骤:

1. 本地定义接口,并在配置文件中说明

PersonService.java

package com.anialy.httpservice.service;

import com.anialy.httpservice.entity.Person;

public interface PersonService {
public Person getPersonByName(String name);
}


PersonService.java

package com.anialy.httpservice.service.impl;

import com.anialy.httpservice.entity.Person;
import com.anialy.httpservice.service.PersonService;

public class PersonServiceImpl implements PersonService{

public Person getPersonByName(String name) {
if("anialy".equals(name))
return new Person("anialy", 100);
return null;
}

}


applicationContext-server-http-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
<bean id="httpService"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service">
<ref bean="personService" />
</property>
<property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">
</property>
</bean>

<bean id="personService" class="com.anialy.httpservice.service.impl.PersonServiceImpl"/>

</beans>


2. mvc配置服务uri与对应的service

applicationContext-mvc.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 
<mvc:view-controller path="/" view-name="redirect:/home" />
<mvc:view-controller path="/home" view-name="home" />

<!-- Spring Service Invoke -->
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/service/httpService">httpService</prop>
</props>
</property>
</bean>

<!-- Spring MVC -->
<bean
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/test=testController
</value>
</property>
<property name="order" value="1" />
</bean>

<bean id="testController" class="com.anialy.webproj.controller.TestController">
<property name="methodNameResolver" ref="paramResolver" />
</bean>

<!-- 定义JSP -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="paramResolver"
class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action" />
<property name="defaultMethodName" value="test" />
</bean>
</beans>


3. 客户端配置uri的invoke

applicationContext-client-http-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 
<bean id="personService"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean"
depends-on="propertyConfigurer">
<property name="serviceUrl" >
<value> http://${host}:${port}/${contextPath}/service/httpService </value>
</property>
<property name="serviceInterface" value="com.anialy.httpservice.service.PersonService">
</property>
</bean>

</beans>


设置属性文件

system.properties

serviceName=localhost
host=127.0.0.1
port=8080
contextPath=WebProj


加载配置文件的xml

applicationContext-constants.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
<value>classpath:system.properties</value>
</list>
</property>
</bean>
</beans>


4. 编写测试方法

TestHttpServiceInvoke.java

package httpservice;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.anialy.httpservice.entity.Person;
import com.anialy.httpservice.service.PersonService;

public class TestHttpServiceInvoke {
private static final Logger logger
= LoggerFactory.getLogger(TestHttpServiceInvoke.class);

private ApplicationContext ctx;
private PersonService personService;

@Before
public void init() throws IOException{
ctx = new ClassPathXmlApplicationContext(new String[]{
"classpath*:/applicationContext-client-http-service.xml",
"classpath*:/applicationContext-constants.xml"
});

personService = (PersonService)  ctx.getBean("personService");
System.out.println("");
}

@Test
public void test() {
Person person = personService.getPersonByName("anialy");
logger.info("姓名:" + person.getName());
logger.info("年龄:" + person.getAge());
}
}


输出:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: