【转】XFire与Spring结合的几种方式
1、使用org.codehaus.xfire.spring.XFireSpringServlet与ServiceBean
1.1 web.xml的配置
<web-app><display-name>Spring Image Database</display-name><description>Spring Image Database sample application</description><!--These values are used by ContextLoaderListener, defined immediately below.The files listed below are used to initialize the business logic portion of the application.Each dispatcher servlet (defined further down) has their own configuration file,which may or may not depend on items in these files.--><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext-webservice.xml</param-value></context-param><!-- Log4j configuration listener--><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!-- Spring framework --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>XFireServlet</servlet-name><display-name>XFire Servlet</display-name><servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class></servlet><servlet-mapping><servlet-name>XFireServlet</servlet-name><url-pattern>/services/*</url-pattern></servlet-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
<beans><import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/><bean name="echoService" ref="echo"/><property name="serviceClass" value="org.codehaus.xfire.test.Echo"/><property name="inHandlers"><list><ref bean="addressingHandler"/></list></property></bean><bean id="echo" ref="bookServiceBean"/><property name="serviceClass" value="org.codehaus.xfire.demo.BookService"/></bean><bean id="bookServiceBean" name="code">public static void main(String args[]){ String serviceURL = "http://127.0.0.1:9001/xfire/services/BookService";Service serviceModel = new ObjectServiceFactory().create(BookService.class,null,"http://xfire.codehaus.org/BookService",null);XFireProxyFactory serviceFactory = new XFireProxyFactory();try{BookService service = (BookService) serviceFactory.create(serviceModel, serviceURL);Client client = Client.getInstance(service);client.addOutHandler(new OutHeaderHandler());Book[] books = service.getBooks();System.out.println("BOOKS:");for (int i = 0; i < books.length; i++){System.out.println(books[i].getTitle());}}catch (MalformedURLException e){e.printStackTrace();}}
public interface BookService{public Book[] getBooks();public Book findBook(String isbn);public Map getBooksMap();}public class BookServiceImpl implements BookService{private Book onlyBook;public BookServiceImpl(){onlyBook = new Book();onlyBook.setAuthor("Dan Diephouse");onlyBook.setTitle("Using XFire");onlyBook.setIsbn("0123456789");}public Book[] getBooks() {return new Book[] { onlyBook };}public Book findBook(String isbn){if (isbn.equals(onlyBook.getIsbn()))return onlyBook;return null;}public Map getBooksMap() {Map result = new HashMap();result.put(onlyBook.getIsbn(), onlyBook);return result;}}
<web-app><!-- START SNIPPET: xfire --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:org/codehaus/xfire/spring/xfire.xml</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>/WEB-INF/log4j.properties</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet><servlet-name>xfire</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class></servlet><servlet-mapping><servlet-name>xfire</servlet-name><url-pattern>/*</url-pattern></servlet-mapping><!-- END SNIPPET: xfire --></web-app>
<beans><!-- START SNIPPET: xfire --><bean name="code"><bean id="echoBean" class="org.codehaus.xfire.spring.remoting.XFireExporter"><property name="serviceFactory"><ref bean="xfire.serviceFactory"/></property><property name="xfire"><ref bean="xfire"/></property><property name="serviceBean"><ref bean="echoBean"/></property><property name="serviceClass"><value>org.codehaus.xfire.spring.example.Echo</value></property></bean><!-- END SNIPPET: xfire --></beans>