spirng中使用Http invoker 来远程调用

domain:

 

public class Account implements Serializable{
	private static final long serialVersionUID = 9020521114209538117L;
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

 

 

AccountService

 

public interface AccountService {
	public void insertAccount(Account account);
	public List<Account> getAccounts(String name);

}

 

AccountServiceImpl

 

public class AccountServiceImpl implements AccountService {

	public List<Account> getAccounts(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	public void insertAccount(Account account) {
		System.out.println("insert Account......");

	}

}

 

Exposing the service object:

 

accountService-servlet.xml

放在WEB-INF下

<bean id="httpservice" class="org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter">
	<property name="service" ref="accountService"></property>
	<property name="serviceInterface" value="com.lmning.AccountService"></property>
	</bean>
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
	<property name="mappings">
	<props>
	<prop key="/account.service">httpservice</prop>
	</props>
	</property>
	</bean>

 

 

In addition, define a corresponding servlet for this exporter in 'web.xml', with the servlet name matching the bean name of the target exporter:

 

web.xml

 

<servlet>
	<servlet-name>accountService</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
	<servlet-name>accountService</servlet-name>
	<url-pattern>*.service</url-pattern>
	</servlet-mapping>

 

客户端xml

 

bean.xml

 

<bean id="serverAccountService" class="org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean">
    <property name="serviceUrl" value="http://localhost:8081/spring-rmi-webservice/account.service"/>
    <property name="serviceInterface" value="com.lmning.AccountService"/>

 

client应用程序:

 

 */
	public static void main(String[] args) {
				ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
		AccountService service = (AccountService)ctx.getBean("serverAccountService");
		service.insertAccount(null);
	}

 

 

运行,ok!

 

你可能感兴趣的:(bean,xml,Web,webservice,servlet)