Remoting HttpInvoker测试

本文介绍了一个使用Spring框架实现的远程服务调用示例,包括实体类定义、接口及其实现类、测试代码和配置文件等内容。示例中详细展示了如何通过HttpInvoker进行远程服务的发布与调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
1.实体POJO
package org.redwolf.server;

import java.io.Serializable;

public class Person implements Serializable {
       
        private static final long serialVersionUID = 1L;
        private String name;
        private String address;
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public String getAddress() {
                return address;
        }
        public void setAddress(String address) {
                this.address = address;
        }       

}
2.接口
package org.redwolf.server;

public interface PersonService {
        public void insert(String name,String address);
        public Person getPerson(String name,String address);
}
3.实现类
package org.redwolf.server;

public class PersonServiceImpl implements PersonService {
   
   
        public void insert(String message,String senderName) {
                System.out.println("-------------数据插入成功-------------");

        }
        public Person getPerson(String name, String address) {
                Person person=new Person();
                person.setName(name);
                person.setAddress(address);
                System.out.println("--------查看数据成功--------");
                return person;
        }
       
       
}
4.测试类
package org.redwolf.server;
import java.sql.Time;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
        public static void main(String[] args) {
                ApplicationContext ctx=new ClassPathXmlApplicationContext("org/redwolf/server/bean-1.xml");
                PersonService ms=(PersonService)ctx.getBean("remoteMb");
                //插入数据
                ms.insert("123", "www.org.redwolf.cn");               
                //查看数据
                Person mb=ms.getPerson("redwolf", "www.org.redwolf.cn");
                System.out.println("    姓名:"+mb.getName()+"------地址:"+mb.getAddress());
                DateFormat df = DateFormat.getDateInstance();
                System.out.println(df.format(new Date()));
        }

}
5.同一包下的配置文件bean-1.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.0.xsd">
<!-- 通过Spring HttpInvoker机制代理远程访问服务 -->
    <bean id="remoteMb" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
       <property name="serviceUrl" value="http://192.168.1.98:8080/spHttp/remote/remoteMb"/>
       <property name="serviceInterface" value="org.redwolf.server.PersonService"/>
    </bean>
</beans>
6.web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app>
  <!-- 加载服务配置文件注册servlet名为remote,此名字要和服务配置文件的名字的第一部分相同,即servlet的名字必须为remote-servlet中的remote -->  

<context-param>  
  
       <param-name>contextConfigLocation</param-name>  
  
       <param-value>/WEB-INF/remote-servlet.xml
      
       </param-value>  
  
    </context-param>  
  
  
<!-- 配置DispatcherServlet -->  
  
    <servlet>  
  
        <servlet-name>remote</servlet-name>  
  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  
        <!-- 配置该Servlet随应用启动时候启动 -->  
  
        <load-on-startup>1</load-on-startup>  
  
    </servlet>  
  
<!-- 配置DispatcherServlet映射的url -->  
  
    <servlet-mapping>  
  
       <servlet-name>remote</servlet-name>  
  
       <url-pattern>/remote/*</url-pattern>  
  
    </servlet-mapping>
</web-app>
7.remote-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="msi"  class="org.redwolf.server.PersonServiceImpl" />  
     <bean name="/remoteMb" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
       <property name="service" ref="msi" />  
       <property name="serviceInterface"  value="org.redwolf.server.PersonService" />  
    </bean>
</beans>
8.运行后控制台出现:
INFO (AbstractApplicationContext.java:378) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@12d7a10: display name [org.springframework.context.support.ClassPathXmlApplicationContext@12d7a10]; startup date [Fri Dec 18 22:16:31 CST 2009]; root of context hierarchy
INFO (XmlBeanDefinitionReader.java:303) - Loading XML bean definitions from class path resource [org/redwolf/server/bean-1.xml]
INFO (AbstractApplicationContext.java:393) - Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@12d7a10]: org.springframework.beans.factory.support.DefaultListableBeanFactory@bc8e1e
INFO (DefaultListableBeanFactory.java:276) - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@bc8e1e: defining beans [remoteMb]; root of factory hierarchy
    姓名:redwolf------地址:
www.org.redwolf.cn
2009-12-18
9. 后台出现:
2009-12-18 22:15:30 org.apache.catalina.core.AprLifecycleListener init
信息: The APR based Apache Tomcat Native library which allows optimal performanc
e in production environments was not found on the java.library.path: D:\mytool\J
ava\jdk6\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\mytool\
Java\jdk6\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program
Files\Common Files\Thunder Network\KanKan\Codecs;D:\mytool\MySQL\MySQL Server 6
.0\bin
2009-12-18 22:15:30 org.apache.coyote.http11.Http11Protocol init
信息: Initializing Coyote HTTP/1.1 on http-8080
2009-12-18 22:15:30 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 1018 ms
2009-12-18 22:15:30 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2009-12-18 22:15:30 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.16
INFO (FrameworkServlet.java:231) - FrameworkServlet 'remote': initialization st
arted
INFO (AbstractApplicationContext.java:378) - Refreshing org.springframework.web
.context.support.XmlWebApplicationContext@14384c2: display name [WebApplicationC
ontext for namespace 'remote-servlet']; startup date [Fri Dec 18 22:15:31 CST 20
09]; root of context hierarchy
INFO (XmlBeanDefinitionReader.java:303) - Loading XML bean definitions from Ser
vletContext resource [/WEB-INF/remote-servlet.xml]
INFO (AbstractApplicationContext.java:393) - Bean factory for application conte
xt [org.springframework.web.context.support.XmlWebApplicationContext@14384c2]: o
rg.springframework.beans.factory.support.DefaultListableBeanFactory@589e56
INFO (DefaultListableBeanFactory.java:276) - Pre-instantiating singletons in or
g.springframework.beans.factory.support.DefaultListableBeanFactory@589e56: defin
ing beans [msi,/remoteMb]; root of factory hierarchy
INFO (FrameworkServlet.java:250) - FrameworkServlet 'remote': initialization co
mpleted in 657 ms
2009-12-18 22:15:32 org.apache.catalina.core.StandardContext addApplicationListe
ner
信息: The listener "listeners.ContextListener" is already configured for this co
ntext. The duplicate definition has been ignored.
2009-12-18 22:15:32 org.apache.catalina.core.StandardContext addApplicationListe
ner
信息: The listener "listeners.SessionListener" is already configured for this co
ntext. The duplicate definition has been ignored.
2009-12-18 22:15:33 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2009-12-18 22:15:33 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2009-12-18 22:15:33 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/31  config=null
2009-12-18 22:15:33 org.apache.catalina.startup.Catalina start
信息: Server startup in 2548 ms
-------------数据插入成功-------------
--------查看数据成功--------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值