Author: Jiangnan Baiyi
This article is from springside wiki. Pay attention to the latest version on Wiki. (Wiki was updated in 11.27 ).
1. Overview
Xfire is the option that many cool people around the world will vote after comparing with the axis series. My favorite features are:
- Integration with spring, without generating a pile of files, without packaging an Independent War, and exporting pojo in the application as a service directly.
- Aegis-a simple default Java XML binding mechanism, and other binding mechanisms can be plug-in.
- Jsr181 -- pojo WebService configuration of the annotatiton driver.
- Stax-based high-performance framework.
- Disconnects from the unit test capability of the Web server.
Documents and examples on the Internet are always not new. Let's put aside all documents and all axis habits. Just read this document, which represents xfire1.2.2's simplest practice.
2. Fire guide 2.1 modify web. xml and add the xfire entry to Web Applications
Xfire entry. Note that xfire has its own Servlet and does not depend on Spring MVC servlet any more. It is also far away from spring mvc url Mapping that is not familiar to everyone, achieving perfect integration with spring.
The path/service/* is specified here, that is, the WebService URL is generated as http://www.springside.org.cn/bookstore/service/bookservice, and the bookservice=is the interface name in 2.2.
<Servlet>
<Servlet-Name> xfire </servlet-Name>
<Servlet-class> org. codehaus. xfire. Spring. xfirespringservlet </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> xfire </servlet-Name>
<URL-pattern>/service/* </url-pattern>
</Servlet-mapping>
2.2 write a narrow interface to extract the services to be exported from pojo
Extract A narrow interface from the existing bookmanager. Java interface and only expose the method that needs to be exported as web service. Bookmanger. Java is a pojo and does not require any WebService-related code.
On the one hand, the narrow interface meets the security requirements and does not need to export all bookmanager methods as Web services. On the other hand, xfire currently only supports interface-based proxy.
Public interface bookservice {
List <book> findbooksbycategory (string cateoryid );
}
2.3 configure Java-XML binding
The default Aegis binding Syntax of xfire is very simple. In the springside example, almost one line of configuration does not need to be written. It is the simplest binding definition I have ever seen, it is much better than other solutions that take design complexity as the ultimate goal.
If the attributes, function parameters, and return values of an object are int, String, date, and other common types, and complex objects composed of common types, no need to be defined. There are only two situations that need to be defined:
- If you cannot use generics to define the type of elements in a collection, for example, list findbooks (). If you can write a list <book> findbooks (), you do not need.
- You need to define different names for attributes, or define certain attributes of complex objects and do not output them.
Xfire should replace the configuration with conventions. If Aegis configuration is required, the binding file of all services and entity beans must be named XXX. aegis. XML, and must be in the same directory as the original sit together class.
<Mapping>
<! -- Configure the return value of the findbooksbyname service, and the object in the list is book -->
<Method name = "findbooksbyname">
<Return-type componenttype = "org. springside. Bookstore. domain. Book"/>
</Method>
<! -- Configure the category class and ignore the embedded products attribute. Do not output XML -->
<Property name = "Products" ignore = "true"/>
</Mapping>
For other syntaxes, see the Aegis reference.
2.4 configure Spring Export
To save code, configure a base class. Note that the exported service cannot be lazy-init:
<! -- Import the basic xfire configuration file -->
<Import resource = "classpath: ORG/codehaus/xfire/spring/xfire. xml"/>
<Bean id = "basewebservice" class = "org. codehaus. xfire. Spring. remoting. xfireexporter" lazy-init = "false"
Abstract = "true">
<Property name = "servicefactory" ref = "xfire. servicefactory"/>
<Property name = "xfire" ref = "xfire"/>
</Bean>
Definition of each Web Service: parent is the base class, serviceclass Property sets the Web service interface, and servicebean Property sets the Web service implementation class.
<bean id="bookService" parent="baseWebService">
<property name="serviceBean" ref="bookManager"/>
<property name="serviceClass" value="org.springside.bookstore.components.xfire.server.simple.BookService"/>
</bean>
After the Web Service is exported, you can go to http: // localhost/service/bookservice? View the automatically generated WSDL.
For details about jsr181 and client API and test, see the xfire fire Guide (below)