Spring HTTP Invoker

Source: Internet
Author: User

1 Overview

Spring HTTP Invoker is a remote call model in the spring framework. It executes HTTP-based remote calls, that is, objects can be transmitted between networks through the firewall and java serialization mechanism. The client can easily call objects on the remote server just like calling local objects. Note that both the server and client use the Spring framework.

2 Examples

The following is a simple example for mobile terminals to use the management background database.

Preparation: Be sure to introduce the jar package of spring-web. Other jar packages will not be introduced...

2.1 Server

Interface

Package com. dcXt. mobile. service. ba;

 

Import java. util. List;

Import com. dcXt. business. entity. Tb01;

 

Public interface BaMobileService {

Boolean save (Tb01 obj );

 

}

 

Implementation class

Bean configuration using annotation (remind me not to forget to configure bean)

Package com. dcXt. mobile. service. ba;

 

Import javax. annotation. Resource;

Import org. springframework. orm. hibernate3.HibernateTemplate;

Import org. springframework. stereotype. Component;

 

Import com. dcXt. business. entity. Tb01;

 

@ Component ("baMobileServiceImpl ")

Public class BaMobileServiceImpl implements BaMobileService {

Private HibernateTemplate hibernateTemplate;

Public HibernateTemplate getHibernateTemplate (){

Return hibernateTemplate;

}

@ Resource

Public void setHibernateTemplate (HibernateTemplate hibernateTemplate ){

This. hibernateTemplate = hibernateTemplate;

}

@ Override

Public boolean save (Tb01 obj ){

HibernateTemplate. saveOrUpdate (obj );

System. out. println (obj. toString ());

Return true;

}

}

 

 

Web. xml

<? Xml version ="1.0"Encoding =UTF-8"?>

<Web-app version ="3.0"

Xmlns =Http://java.sun.com/xml/ns/javaee"

Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemaLocation =Http://java.sun.com/xml/ns/javaee

Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<! -- DispatcherServlet -->

<Servlet>

<Servlet-name> service </servlet-name>

<Servlet-class>

Org. springframework. web. servlet. DispatcherServlet

</Servlet-class>

<Init-param>

<Param-name> contextConfigLocation </param-name>

<Param-value> classpath: applicationContext-service.xml </param-value>

</Init-param>

<Load-on-startup> 1 </load-on-startup>

</Servlet>

<Servlet-mapping>

<Servlet-name> service </servlet-name>

<Url-pattern> *. service </url-pattern>

</Servlet-mapping>

<! -- Spring entry -->

<Context-param>

<Param-name> contextConfigLocation </param-name>

<Param-value>

Classpath: applicationContext. xml

</Param-value>

</Context-param>

<Listener> <listener-class> org. springframework. web. context. ContextLoaderListener </listener-class>

</Listener>

<Welcome-file-list>

<Welcome-file> login. jsp </welcome-file>

</Welcome-file-list>

 

</Web-app>

 

 

Classpath: applicationContext-service.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"Xmlns: context =Http://www.springframework.org/schema/context"

Xsi: schemaLocation =Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

 

<Bean id ="BaMobileService"Class ="Org. springframework. remoting. httpinvoker. HttpInvokerServiceExporter">

<Property name ="Service"Ref ="BaMobileServiceImpl"/>

<Property name ="ServiceInterface"Value ="Com. dcXt. mobile. service. ba. BaMobileService">

</Property>

</Bean>

<Bean id ="UrlMapping"Class ="Org. springframework. web. servlet. handler. SimpleUrlHandlerMapping">

<Property name ="Mappings">

<Props>

<Prop key ="/Ba. service"> BaMobileService </prop>

</Props>

</Property>

</Bean>

 

</Beans>

Description: client access address./Ba. serviceTo call thisBaMobileService, the client interface is consistent with the server, which is equivalent to directly using thisBaMobileServiceImpl

 

2.2 Client

Interface (completely consistent with the server side, package path)

Package com. dcXt. mobile. service. ba;

 

Import com. dcXt. business. entity. Tb01;

 

Public interface BaMobileService {

Boolean save (Tb01 obj );

 

}

ApplicationContext. 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"Xmlns: context =Http://www.springframework.org/schema/context"

Xsi: schemaLocation =Http://www.springframework.org/schema/beans

Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

Http://www.springframework.org/schema/context

Http://www.springframework.org/schema/context/spring-context-3.0.xsd">

 

<Bean id ="BaMobileService"

Class ="Org. springframework. remoting. httpinvoker. HttpInvokerProxyFactoryBean">

<Property name ="ServiceUrl">

<Value> http: // 127.0.0.1: 8081/dcXt/ba. service </value>

</Property>

<Property name ="ServiceInterface"Value ="Com. dcXt. mobile. service. ba. BaMobileService">

</Property>

</Bean>

<Bean id ="BaAction"Class ="Com. dcXt. business. action. BaAction"Scope ="Prototype">

<Property name ="BaMobileService"Ref ="BaMobileService"> </Property>

</Bean>

</Beans>

Note: direct use of ActionBaMobileServiceYou can.

 

3. Conclusion

Pay attention to the server and client writing methods. The client and the server have the same interface, and then the client directly calls the server implementation class through the interface.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.