Spring中HttpInvoker实例
2012-09-20 16:45
441 查看
Spring版本2.5.6.SEC01
1.服务端:
需要如下jar包:spring.jar spring-webmvc.jar
IPersonService.java
Java代码
public interface IPersonService {
public String queryPersonName();
}
PersonServiceImpl.java
C-sharp代码
public class PersonServiceImpl implements IPersonService {
public String queryPersonName() {
return "test";
}
}
service-config.xml
Java代码
<?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添加如下代码
Java代码
<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
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
Java代码
<?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
转自:http://up2pu.iteye.com/blog/988871
1.服务端:
需要如下jar包:spring.jar spring-webmvc.jar
IPersonService.java
Java代码
public interface IPersonService {
public String queryPersonName();
}
PersonServiceImpl.java
C-sharp代码
public class PersonServiceImpl implements IPersonService {
public String queryPersonName() {
return "test";
}
}
service-config.xml
Java代码
<?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添加如下代码
Java代码
<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
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
Java代码
<?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
转自:http://up2pu.iteye.com/blog/988871
相关文章推荐
- Spring中HttpInvoker实例
- Spring中HttpInvoker远程方法调用使用实例
- Spring源码分析总结——HttpInvoker
- Spring HttpInvoker(一)
- Spring HttpInvokerProxyFactoryBean 远程调用示例
- 关于spring http invoker中一个常见错误,dubbo 调用 hibernate的set map list
- Spring Http Invoker
- Spring Boot 运行原理 - 实例分析(HttpEncodingAutoConfiguration)
- Spring HTTP Invoker
- Spring HTTP invoker
- Spring HTTP invoker 简介
- 【JAVA】Spring HTTP Invoker 远程服务调用
- spring HTTP invoker:实现远程调用
- Spring HTTP invoker简介
- Spring远程调用技术<3>-Spring的HTTP Invoker
- 远程服务调用之RMI、Hessian、Burlap、Spring的HTTPinvoker
- Spring的Hessian,Burlap,HttpInvoker远程调用
- Spring HttpInvoker和Hessian在Spring MVC下出错
- Spring HttpInvoker 从实战到源码追溯
- spring mvc httpinvoker 报错invalid stream header: 0D0A0D0A 解决办法之一