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

Spring中HttpInvoker实例

2010-03-25 14:03 465 查看
Spring版本2.5.6.SEC01

1.服务端:

需要如下jar包:spring.jar spring-webmvc.jar

IPersonService.java

public interface IPersonService {
public String queryPersonName();
}


PersonServiceImpl.java

public class PersonServiceImpl implements IPersonService {
public String queryPersonName() {
return "test";
}
}


service-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

<bean id="personService"
class="com.travelsky.angel.service.impl.PersonServiceImpl" />

<bean id="serviceExporter"
class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="service" ref="personService" />
<property name="serviceInterface"
value="com.travelsky.angel.service.IPersonService" />
</bean>
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/person.service">serviceExporter</prop>
</props>
</property>
</bean>
</beans>


web.xml添加如下代码

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/service-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.service</url-pattern>
</servlet-mapping>


2.客户端:

客户端需要应用服务端的bean和接口,另外需要spring.jar

HttpInvokerClient.java

public class HttpInvokerClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("httpinvoker-client.xml");
IPersonService service = (IPersonService) context.getBean("personServiceProxy");
String result = service.queryPerson();
System.out.println(result);
}
}


httpinvoker-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="personServiceProxy"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://127.0.0.1:8080/ProjectContextRoot/person.service" />
<property name="serviceInterface"
value="com.travelsky.angel.service.IPersonService" />
</bean>
</beans>


注意:ProjectContextRoot对应.mymetadata中的context-root,如果修改,需要重新启动Eclipse
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: