初步接触httpinvoker.HttpInvokerProxyFactoryBean,弄了个简单的小例子,记录一下。

先来个项目文件层次图

 

服务器端包括:domain、service、dao、service-config.xml,使用web容器启动,比如tomcat、resin等。

客户端包括:domain、service中的IxxxService(接口即可)、client、httpinvokeclient.xml。

下面贴源码

Person.java

package com.fei.domain;

public class Person implements  java.io.Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3972445346283975987L;
	
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
}

IPersonDao.java

package com.fei.dao;

import com.fei.domain.Person;

public interface IPersonDao {
	public Person findByName(String name);
}


 

PersonDaoImpl.java

package com.fei.dao;

import com.fei.domain.Person;

public class PersonDaoImpl implements IPersonDao{

	@Override
	public Person findByName(String name) {
		//模拟数据库查询
		Person person=new Person();
		person.setName("张三");
		person.setAge(20);
		return person;
	}

}

IPersonService.java

package com.fei.service;

import com.fei.domain.Person;

public interface IPersonService {

	public Person findByName(String name);
}


 

PersonService.java

package com.fei.service;

import com.fei.dao.IPersonDao;
import com.fei.dao.PersonDaoImpl;
import com.fei.domain.Person;

public class PersonService implements IPersonService{

	private IPersonDao personDao=new PersonDaoImpl();
	@Override
	public Person findByName(String name) {
		// TODO Auto-generated method stub
		System.out.println("aaaaaaaa");
		System.out.println(name);
		return personDao.findByName(name);
	}

}

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.fei.service.PersonService" />  
     <bean id="serviceExporter"  
         class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">  
         <property name="service" ref="personService" />  
         <property name="serviceInterface"  
             value="com.fei.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

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name></display-name>
  <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>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


至此,服务器看可以用web容器跑起来了,比如用tomcat



客户端

HttpInvokeClient.java

package com.fei.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.fei.domain.Person;
import com.fei.service.IPersonService;

public class HttpInvokeClient {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("httpinvokeclient.xml");
		IPersonService personService=(IPersonService)context.getBean("personServiceProxy");
		Person person=personService.findByName("张三");
		System.out.println(person.getName()+"   "+person.getAge());
	}
}

httpinvokeclient.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/httpinvoke/person.service" />  
         <property name="serviceInterface"  
             value="com.fei.service.IPersonService" />  
     </bean>  
</beans>  


运行客户端HttpInvokeClient.java

 

再看看服务器端的打印

 

 

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐