`

Spring实现远程调用

阅读更多
       前段时间为了完成项目中的远程调用功能,查了很多资料,学习了Webservice远程调用和Spring远程调用,现在将Spring远程调用的方法总结如下,希望对有需要的人能够提供一点点帮助。

       远程调用分为两个工程:客户端和服务器端,每个工程都必须添加Spring 的Core,AOP,Remoting,Web  4个jar包。
服务器端如下:
src下有一个包,包下有一个接口和一个实现了该接口的类
接口和实现类内容:
public interface HelloService {
String getPerson(String name);
}

public class HelloServiceImpl implements HelloService {

public String getPerson(String name) {
// TODO Auto-generated method stub

return "Hello "+name;
}

}

包结构图如下:




WebRoot下的WEB-INF下有两个配置文件 web.xml和server_config.xml
web.xml的内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <servlet>
        <servlet-name>remoteservice</servlet-name>
        <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/server_config.xml
</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
   
    <servlet-mapping>
        <servlet-name>remoteservice</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


server_config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!--<bean id="service" class="hello.HelloService"></bean>
--><bean id="serviceImpl" class="hello.HelloServiceImpl"></bean>

<bean name="/service.do"
  class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
<property name="serviceInterface" value="hello.HelloService"></property>
<property name="service" ref="serviceImpl"></property>
</bean>

</beans>


客户端如下:
src下有一个包和一个配置文件client.xml,包内有一个和服务器端一致的接口和一个测试类
包结构图如下:




测试类的内容如下:
public class TestService {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("client_config.xml");
HelloService service = (HelloService)context.getBean("serviceProxy");
String str = service.getPerson("zhangrongrong");
System.out.println(str);
}

}

client.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<bean id="serviceProxy"
class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
<property name="serviceUrl"
value="http://127.0.0.1:8080/spring_server/service.do">
</property>
<property name="serviceInterface"
value="chello.HelloService">
</property>
</bean>

</beans>
  • 大小: 8.9 KB
  • 大小: 8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics