---
title: "ONJava.com -- Web 服务实现中的概念分离 --> page 2"
author: "suli"
date: "2007-01-29T17:35:00Z"
tags: []
slug: "onjava-com-web-page-2"
draft: false
lastmod: "2007-01-29T17:35:00Z"
id: 52505
url: "https://suli.iteye.com/blog/52505"
---

第 [1](http://blog.csdn.net/pub/a/onjava/2006/09/06/separation-of-concerns-in-web-services.html?page=1), **2**, [3](http://blog.csdn.net/pub/a/onjava/2006/09/06/separation-of-concerns-in-web-services.html?page=3), [4](http://blog.csdn.net/pub/a/onjava/2006/09/06/separation-of-concerns-in-web-services.html?page=4) 页

 

### 业务逻辑的实现与配置

我们从解释业务逻辑类的实现与配置入手，因为它们是最简单的部分。 请从[可下载的示例代码](?page=4#resources)部分查看 `AccountMgr` 接口和 `AccountMgrImpl` 类的源代码。 从源代码中可以看出，实际上实现没有做任何事，因为我们要保证事务的简单性，毕竟本文的目的不在于如何编写资金转移的代码。

以下是一个 Spring 配置文件的片段(从“资源”部分可获取完整的配置文件)，它展示了我们是如何为业务逻辑配置 Spring Bean 以使用 Spring 的 AOP 功能。 第一个 Bean 条目只是单纯地为 `AccountMgrImpl` 类设置了一个 Bean。 第二个 Bean 条目展示了我们前面讨论的如何利用 AOP 代理。 我们将 Bean ID 设置为 `accountMgr`，将从 `ProxyFactoryBean` 获取该 ID。 当 `FundsTransferServiceSoapBindingImpl` 以该 Bean ID 向 Spring　提出请求时，`ProxyFactoryBean`　将返回一个　AOP　代理对象实例。 我们对它进行配置以实现　`AccountMgr`　接口，这样一来，客户端程序就会认为正在与一个业务逻辑对象通信。 对于第二个名为　`interceptorNames`　的属性，设置它是为了使一个称为　`securityInterceptor`　的　Bean 可以拦截对方法的调用以执行安全性检查。 如此，我们就在对业务逻辑代码不产生任何依赖性的情况下插入了安全性机制。 最后，我们将　target 属性设置为　`accountMgrTarget`　Bean，这样，方法调用会最终传播到实际的业务逻辑类　`AccountMgrImpl`　上。

```
<beans>  <bean id="accountMgrTarget"    class="com.mybank.bizlogic.AccountMgrImpl"/>  . . .  <bean id="accountMgr"    class="org.springframework.aop.framework.    ProxyFactoryBean">    <property name="proxyInterfaces">      <list>        <value>          com.mybank.bizlogic.AccountMgr        </value>      </list>    </property>    <property name="interceptorNames">      <list>        <value>          securityInterceptor        </value>      </list>    </property>    <property name="target">      <ref bean="accountMgrTarget"/>    </property>  </bean>  . . .</beans>
```

### Web 服务的实现和配置

`FundsTransferServiceSoapBindingImpl` 类就是　Web 服务的实现。 请从“可下载的示例代码”部分查看其源代码。 该类的骨架是由 Axis 生成的，只需填充用于实现的方法。 请注意，该类扩展自 `ServletEndpointSupport`。 这是 Spring 提供的一个方便的类，它可以用于 JAX-RPC Web服务实现以获取对 Spring 应用程序上下文的引用。 通过扩展该类，`FundsTransferServiceSoapBindingImpl` 可以访问 Spring 上下文来获取一个对 `accountMgr` Bean 的引用(上文中已经进行了讨论)。 由于 `FundsTransferServiceSoapBindingImpl` 由 Axis 管理，我们不能使用 Spring 的依赖性注入功能为该 Bean 自动获取引用。 因此，不得不在 `onInit()` 方法中明确地获取引用。 不尽人意的是，这在特定于 Spring 的类中添加了一些依赖性。 算了吧，这也是为了可以利用 Spring 和 Acegi 所付出的小小代价。 请注意在实际的 `transferFunds()` 方法中，该代码只传播给 `accountMgr` Bean。

在 Axis 配置文件(*deploy.wsdd* 和 *server-config.wsdd*) 中，我们需要保证用于实现服务的类是 `FundsTransferServiceSoapBindingImpl` 类，而不是 Axis 生成的类骨架(`FundsTransferServiceSoapBindingSkeleton`)。 要使 Spring 在 Axis 等 Web 应用程序中正常工作，我们需要在 *web.xml* 文件中添加下面的条目。 `context-param` 指出 Spring 配置文件的位置。 `listener` 条目的配置，以便 Spring 配置文件及其上下文可以正确地装载和启用。

```
<web-app>  <context-param>    <param-name>      contextConfigLocation    </param-name>    <param-value>      /WEB-INF/spring-config.xml    </param-value>  </context-param>  <listener>    <listener-class>      org.springframework.web.context.      ContextLoaderListener    </listener-class>  </listener>. . .</web-app>
```

第 [1](http://blog.csdn.net/suli2921/archive/2007/01/29/1497258.aspx), **2**, [3](http://blog.csdn.net/pub/a/onjava/2006/09/06/separation-of-concerns-in-web-services.html?page=3), [4](http://blog.csdn.net/pub/a/onjava/2006/09/06/separation-of-concerns-in-web-services.html?page=4) 页

[下一页![箭头](/images/arrows/arrows-66.gif)](http://blog.csdn.net/pub/a/onjava/2006/09/06/separation-of-concerns-in-web-services.html?page=3)