加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

[webservices开发]XFire在SpringSide中的应用

发布时间:2020-12-17 02:56:26 所属栏目:安全 来源:网络整理
导读:这一节,通过 SpringSide 来分析 XFire 的应用。 ? SpringSide 开源项目是国内的开发人员所做的一个以 Spring 为核心的开源项目 , 目的是 提供一个 Pragmatic 的企业应用开发基础和最佳实践展示。为使用 Spring 框架的开发者提供一个非 Demo 版的复杂、正式

这一节,通过SpringSide来分析XFire的应用。

?

SpringSide开源项目是国内的开发人员所做的一个以Spring为核心的开源项目,目的是提供一个Pragmatic的企业应用开发基础和最佳实践展示。为使用Spring框架的开发者提供一个非Demo版的复杂、正式而体现最佳使用实践的参照系统。为JavaEEer必须面对的所有问题提供合理的、合乎Pragmatic原则的解决方案。采用Plugins形式组织,使开发者可快速定位所需的参考方案并做加法到自己的系统。

?

SpringSide中关于Web服务的配置是在

WEB-IBF/classes文件下的applicationContext-webservice.xml中配置的:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

??? <import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>

??? <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

? ??????<property name="mappings">

??????????? <value>/BookService=bookService</value>

??????? </property>

??? </bean>

?

??? <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" abstract="true">

??????? <property name="serviceFactory" ref="xfire.serviceFactory"/>

??????? <property name="xfire" ref="xfire"/>

??? </bean>

???

??? <bean id="bookService" parent="baseWebService">

??????? <property name="serviceBean" ref="bookManager"/>

??????? <property name="serviceClass" value="org.springside.bookstore.service.webservice.BookService"/>

??? </bean>

</beans>

?

第一步,引入xfire.xml文件

<import resource="classpath:org/codehaus/xfire/spring/xfire.xml"/>。这样,我们就不必在web.xml中配置了,这种语法在Spring参考手册(2.0-M33.18节中有介绍,3.19节介绍了在web.xml配置bean定义文件的方法,就是上一节使用的方法。

?

第二步,处理映射,将/BookService URLbookService这个bean联系起来。当然,这里bookService又继承了baseWebService,这样做挺巧妙,这样如果有多个Web服务bean,就继承baseWebService就可以了,这种面向对象的概念还是值得我们提倡的,Spring参考手册3.5节介绍了相关的知识。

?

bookService的定义中,serviceBean也就是接口实现类为bookManager bean,这个bean实际是在WEB-INF/classes/ applicationContext-manager.xml文件中所定义,类名为org.springside.bookstore.service.logic.BookManager

package org.springside.bookstore.service.logic;

?

import … …

?

public class BookManager extends BaseHibernateDao implements BookService {

?

??? private CategoryManager categoryManager;

?

??? public void setCategoryManager(CategoryManager categoryManager) {

??????? this.categoryManager = categoryManager;

??? }

?

??? protected Class getEntityClass() {

??????? … …

??? }

?

??? public Book get(Integer id) {

??????? … …

??? }

?

??? public void save(Book book) {

?????? … …

??? }

?

??? public void remove(Integer id) {

?????? … …

??? }

?

??? public List getAllCategorys() {

??????? … …

??? }

?

??? public Category getCategory(Integer id) {

??????? … …

??? }

?

??? public List findBooksByName(String name) {

??????? … …

??? }

??

??? public List getNewBooks() {

??????? … …

??? }

?

??? public List findAllLowStock(Integer stockHint) {

?????? … …

??? }

?

??? public List findBooksByCategory(String category_id) {

?????? … …

??? }

?

??? protected void filterCriteria(Criteria criteria,Map filter) {

?????? … …

??? }

}

?

serviceClass也就是接口类为

org.springside.bookstore.service.webservice.BookService

package org.springside.bookstore.service.webservice;

?

import java.util.List;

?

public interface BookService {

??? List findBooksByName(String name);

??

??? List findBooksByCategory(String category);

?

??? List getAllCategorys();

?

}

?

事实上,SpringSide既是一个Web服务的提供者,又是一个Web服务的消费者。它在WEB-INF/classes/applicationContext-webservice-client.xml文件中定义了消费这个Web服务的bean

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

??? <!-- 一分钟刷新一次sciprt文件-->

??? <bean class="org.springframework.scripting.support.ScriptFactoryPostProcessor">

????????? <property name="refreshCheckDelay" value="60" />

??? </bean>

?

??? <bean id="BookServiceClient" class="org.springframework.scripting.groovy.GroovyScriptFactory">

???????? <constructor-arg value="classpath:org/springside/bookstore/service/webservice/BookServiceClient.groovy"/>

???????? <property name="serviceUrl" value="http://localhost:8080/springside/service/BookService" />

??? </bean>

</beans>

?

这个消费Web服务的bean定义为BookServiceClient,它是采用Groovy脚本语言定义了。在Spring参考手册(2.0-M3)中的第25章专门介绍在Spring中脚本语言的使用,脚本语言支持是Spring 2.0新增的内容,目前可以支持GroovyBeanShellJruby三种脚本语言。

这个BookServiceClient最终是在dwr中使用,可以

plugins/org.springside.ajax.dwr/webapp/WEB-INF/dwr.xml中的定义。

?

SpringSide,采用Aegisbinding方式,在

plugins/org.springside.webservice.xfire/src/org/springside/bookstore/service/webservice/BookService.aegis.xml中定义了返回值的类型:

<?xml version="1.0" encoding="UTF-8"?>

<mappings>

??? <mapping>

??????? <method name="findBooksByName">

??????????? <return-type componentType="org.springside.bookstore.domain.Book"/>

??????? </method>

???? ???<method name="findBooksByCategory">

??????????? <return-type componentType="org.springside.bookstore.domain.Book"/>

??????? </method>

??????? <method name="getAllCategorys">

??????????? <return-type componentType="org.springside.bookstore.domain.Category"/>

??????? </method>

??? </mapping>

</mappings>

?

?

XFireSpringSide中的应用就介绍到这里为止。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读