`

使用 AppFuse 的七个理由(中英文两版)

 
阅读更多

使用 AppFuse 的七个理由

学习 Java 开放源码工具 —— 并使用这些工具提高生产效率

developerWorks
文档选项
<noscript></noscript> <script language="JavaScript" type="text/javascript"> <!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="将此页作为电子邮件发送" /></td><td width="122"><p><a class="smallplainlink" href="javascript:document.email.submit();"><b>将此页作为电子邮件发送</b></a></p></td></tr>'); //--> </script>
将此页作为电子邮件发送

将此页作为电子邮件发送

<!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- 03/20/06 updated by gretchen -->
拓展 Tomcat 应用

下载 IBM 开源 J2EE 应用服务器 WAS CE 新版本 V1.1

<!--END RESERVED FOR FUTURE USE INCLUDE FILES-->

级别: 初级

Matt Raible (mraible@virtuas.com), 开放源码实践先驱, Virtuas Open Source Solutions

2006 年 8 月 31 日

开始学习在 Java™ 平台上使用诸如 Spring、Hibernate 或 MySQL 之类的开放源码工具时可能非常困难。再加上 Ant 或 Maven,以及与 DWR 一起的小 Ajax,还有 Web 框架 —— 即 JSF,我们必须睁大眼睛盯着如何配置应用程序。AppFuse 减少了集成开放源码项目的痛苦。它可以把测试变成一等公民,让我们可以从数据库表生成整个 UI,并使用 XFire 来支持 Web 服务。另外,AppFuse 的社区也非常健全,这是不同 Web 框架用户可以一起融洽相处的地方之一。
<!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->

AppFuse 是一个开放源码的项目和应用程序,它使用了在 Java 平台上构建的开放源码工具来帮助我们快速而高效地开发 Web 应用程序。我最初开发它是为了减少在为客户构建新 Web 应用程序时所花费的那些不必要的时间。从核心上来说,AppFuse 是一个项目骨架,类似于通过向导创建新 Web 项目时 IDE 所创建的东西。当我们使用 AppFuse 创建一个项目时,它会提示我们将使用开放源码框架,然后才创建项目。它使用 Ant 来驱动测试、代码生成、编译和部署。它提供了目录和包结构,以及开发基于 Java 语言的 Web 应用程序所需要的库。

与大部分 “new project” 向导不同,AppFuse 创建的项目从最开始就包含很多类和文件。这些文件用来实现特性,不过它们同时也会在您开发应用程序时被用作示例。通过使用 AppFuse 启动新项目,我们通常可以减少一到两周的开发时间。我们不用担心如何将开放源码框架配置在一起,因为这都已经完成了。我们的项目都已提前配置来与数据库进行交互,它会部署到应用服务器上,并对用户进行认证。我们不必实现安全特性,因为这都早已集成了。

当我最初开发 AppFuse 时,它只支持 Struts 和 Hibernate。经过几年的努力,我发现了比 Struts 更好的 Web 框架,因此我还添加了为这些 Web 框架使用的选项。现在,AppFuse 可以支持 Hibernate 或 iBATIS 作为持久性框架。对于 Web 框架来说,我们可以使用 JavaServer Faces(JSF)、Spring MVC、Struts、Tapestry 或 WebWork。

AppFuse 提供了很多应用程序需要的一些特性,包括:

  • 认证和授权
  • 用户管理
  • Remember Me(这会保存您的登录信息,这样就不用每次都再进行登录了)
  • 密码提醒
  • 登记和注册
  • SSL 转换
  • E-mail
  • URL 重写
  • 皮肤
  • 页面修饰
  • 模板化布局
  • 文件上载

这种 “开箱即用” 的功能是 AppFuse 与其他 CRUD 代 框架的区别之一(CRUD 取自创建、检索、更新删除 几个操作的英文首字母),包括 Ruby on Rails、Trails 和 Grails。上面提到的这些框架,以及 AppFuse,都让我们可以从数据库表或现有的模型对象中生成主页/细节页。

图 1 阐述了一个典型 AppFuse 应用程序的概念设计:


图 1. 典型的 AppFuse 应用程序
典型的 AppFuse 应用程序

清单 1 给出了我们在创建 devworks 项目时所使用的命令行交互操作,同时还给出了所生成的结果。这个项目使用了 WebWork 作为自己的 Web 框架(请参考下面 参考资料 一节给出的链接)。


清单 1. 使用 AppFuse 创建新项目
alotta:~/dev/appfuse mraible$ ant new
Buildfile: build.xml

clean:
     [echo] Cleaning build and distribution directories

init:

new:
     [echo] 
     [echo] +-------------------------------------------------------------+
     [echo] |    -- Welcome to the AppFuse New Application Wizard! --     |
     [echo] |                                                             |
     [echo] | To create a new application, please answer the following    |
     [echo] | questions.                                                  |
     [echo] +-------------------------------------------------------------+

    [input] What would you like to name your application [myapp]?
devworks
    [input] What would you like to name your database [mydb]?
devworks
    [input] What package name would you like to use [org.appfuse]?
com.ibm
    [input] What web framework would you like to use [webwork,tapestry,spring,js
f,struts]?
webwork
     [echo] Creating new application named 'devworks'...
     [copy] Copying 359 files to /Users/mraible/Work/devworks
     [copy] Copying 181 files to /Users/mraible/Work/devworks/extras
     [copy] Copying 1 file to /Users/mraible/Work/devworks
     [copy] Copying 1 file to /Users/mraible/Work/devworks

install:
     [echo] Copying WebWork JARs to ../../lib
     [copy] Copying 6 files to /Users/mraible/Work/devworks/lib
     [echo] Adding WebWork entries to ../../lib.properties
     [echo] Adding WebWork classpath entries
     [echo] Removing Struts-specific JARs
   [delete] Deleting directory /Users/mraible/Work/devworks/lib/struts-1.2.9
   [delete] Deleting directory /Users/mraible/Work/devworks/lib/strutstest-2.1.3
     [echo] Deleting struts_form.xdt for XDoclet
   [delete] Deleting directory /Users/mraible/Work/devworks/metadata/templates
     [echo] Deleting Struts merge-files in metadata/web
   [delete] Deleting 7 files from /Users/mraible/Work/devworks/metadata/web
     [echo] Deleting unused Tag Libraries and Utilities
   [delete] Deleting 2 files from /Users/mraible/Work/devworks/src/web/org/appfu
se/webapp
     [echo] Modifying appgen for WebWork
     [copy] Copying 12 files to /Users/mraible/Work/devworks/extras/appgen
     [echo] Replacing source and test files
   [delete] Deleting directory /Users/mraible/Work/devworks/src/web/org/appfuse/
webapp/form
   [delete] Deleting directory /Users/mraible/Work/devworks/src/web/org/appfuse/
webapp/action
     [copy] Copying 13 files to /Users/mraible/Work/devworks/src
   [delete] Deleting directory /Users/mraible/Work/devworks/test/web/org/appfuse
/webapp/form
   [delete] Deleting directory /Users/mraible/Work/devworks/test/web/org/appfuse
/webapp/action
     [copy] Copying 5 files to /Users/mraible/Work/devworks/test
     [echo] Replacing web files (images, scripts, JSPs, etc.)
   [delete] Deleting 1 files from /Users/mraible/Work/devworks/web/scripts
     [copy] Copying 34 files to /Users/mraible/Work/devworks/web
   [delete] Deleting: /Users/mraible/Work/devworks/web/WEB-INF/validator-rules-c
ustom.xml
     [echo] Modifying Eclipse .classpath file
     [echo] Refactoring build.xml
     [echo] ----------------------------------------------
     [echo] NOTE: It's recommended you delete extras/webwork as you shouldn't ne
ed it anymore.
     [echo] ----------------------------------------------
     [echo] Repackaging info written to rename.log
     [echo] 
     [echo] +-------------------------------------------------------------+
     [echo] |           -- Application created successfully! --           |
     [echo] |                                                             |
     [echo] | Now you should be able to cd to your application and run:   |
     [echo] | > ant setup test-all                                        |
     [echo] +-------------------------------------------------------------+

BUILD SUCCESSFUL
Total time: 15 seconds

为什么使用 WebWork?
Struts 社区最近在热情地拥抱 WebWork,这种结合导致为 Java 平台提供了一个非常优秀的新 Web 框架:Struts 2。当然,Spring MVC 是一个非常优秀的基于请求的框架,但是它不能像 Struts 2 一样支持 JSF。基于内容的框架(例如 JSF 和 Tapestry)也都很好,但是我发现 WebWork 更为直观,更容易使用(更多有关 Structs 2 和 JSF 的内容请参看 参考资料)。

在创建一个新项目之后,我们就得到了一个类似于图 2 所示的目录结构。Eclipse 和 Intellij IDEA 项目文件都是作为这个过程的一部分创建的。


图 2. 项目的目录结构
项目的目录结构

这个目录结构与 Sun 为 Java 2 Platform Enterprise Edition(J2EE)Web 应用程序推荐的目录结构非常类似。在 2.0 版本的 AppFuse 中,这个结构会变化成适合 Apache Maven 项目的标准目录结构(有关这两个目录介绍的内容,请参看 参考资料 中的链接)。AppFuse 还会从 Ant 迁移到 Maven 2 上,从而获得相关下载的能力和对生成 IDE 项目文件的支持。目前基于 Ant 的系统要求提交者维护项目文件,而 Maven 2 可以通过简单地使用项目的 pom.xml 文件生成 IDEA、Eclipse 和 NetBeans 项目文件。(这个文件位于您项目的根目录中,是使用 Maven 构建应用程序所需要的主要组件)。它与利用 Ant 所使用的 build.xml 文件非常类似。)

现在我们对 AppFuse 是什么已经有一点概念了,在本文剩下的部分中,我们将介绍使用 AppFuse 的 7 点理由。即使您选择不使用 AppFuse 来开始自己的项目,也会看到 AppFuse 可以为您提供很多样板代码,这些代码可以在基于 Java 语言的 Web 应用程序中使用。由于它是基于 Apache 许可证的,因此非常欢迎您在自己的应用程序中重用这些代码。

理由 1:测试

测试是在软件开发项目中很少被给予足够信任的一个环节。注意我并不是说在软件开发的一些刊物中没有得到足够的信任!很多文章和案例研究都给出了测试优先的开发方式和足够的测试覆盖面以提高软件的质量。然而,测试通常都被看作是一件只会延长项目开发时间的事情。实际上,如果我们使用测试优先的方法在编写代码之前就开始撰写测试用例,我相信我们可以发现这实际上会加速 开发速度。另外,测试优先也可以使维护和重用更加 容易。如果我们不编写代码来测试自己的代码,那么就需要手工对应用程序进行测试 —— 这通常效率都不高。自动化才是关键。

当我们首次开始使用 AppFuse 时,我们可能需要阅读这个项目 Web 站点上提供的快速入门指南和教程(请参看 参考资料 中的链接)。这些教程的编写就是为了您可以首先编写测试用例;它们直到编写接口和/或实现之后才能编译。如果您有些方面与我一样,就会在开始编写代码之前就已经编写好测试用例了;这是真正可以加速编写代码的惟一方式。如果您首先编写了代码的实现,通过某种方式验证它可以工作,那么您可能会对自己说,“哦,看起来不错 —— 谁需要测试呢?我还有更多的代码需要编写!”这种情况不幸的一面是您通常都会做一些事情 来测试自己的代码;您简单地跳过了可以自动化进行测试的地方。

AppFuse 的文档展示了如何测试应用程序的所有 层次。它从数据库层开始入手,使用了 DbUnit(请参看 参考资料)在运行测试之前提前使用数据来填充自己的数据库。在数据访问(DAO)层,它使用了 Spring 的 AbstractTransactionalDataSourceSpringContextTests 类(是的,这的确是一个类的名字!)来允许简单地加载 Spring 上下文文件。另外,这个类对每个 testXXX() 方法封装了一个事务,并当测试方法存在时进行回滚。这种特性使得测试 DAO 逻辑变得非常简单,并且不会对数据库中的数据造成影响。

在服务层,jMock (请参看 参考资料)用来编写那些可以消除 DAO 依赖的真正 单元测试。这允许进行验证业务逻辑正确的快速测试;我们不用担心底层的持久性逻辑。

HtmlUnit 支持
HtmlUnit 团队在 1.8 发行版中已经完成了相当多的工作来确保包可以与流行的 Ajax 框架(Prototype 和 Scriptaculous)很好地工作。

在 Web 层,测试会验证操作(Struts/WebWork)、控件(Spring MVC)、页面(Tapestry)和管理 bean(JSF)如我们所期望的一样进行工作。Spring 的 spring-mock.jar 可以非常有用地用来测试所有这些框架,因为它包含了一个 Servlet API 的仿真实现。如果没有这个有用的库,那么测试 AppFuse 的 Web 框架就会变得非常困难。

UI 通常是开发 Web 应用程序过程中最为困难的一部分。它也是顾客最经常抱怨的地方 —— 这既是由于它并不是非常完美,也是由于它的工作方式与我们期望的并不一样。另外,没有什么会比在客户面前作演示的过程中看到看到异常堆栈更糟糕的了!您的应用程序可能会非常可怕,但是客户可能会要求您做到十分完美。永远不要让这种事情发生。Canoo WebTest 可以对 UI 进行测试。它使用了 HtmlUnit 来遍历测试 UI,验证所有的元素都存在,并可以填充表单的域,甚至可以验证一个假想的启用 Ajax 的 UI 与我们预期的工作方式一样。(有关 WebTest 和 HTMLUnit 的链接请参看 参考资料。)

为了进一步简化 Web 的测试,Cargo(请参看 参考资料)对 Tomcat 的启动和停止(分别在运行 WebTest 测试之前和之后)进行了自动化。





回页首


理由 2:集成

正如我在本文简介中提到的一样,很多开放源码库都已经预先集成到 AppFuse 中了。它们可以分为以下几类:

  • 编译、报告和代码生成:Ant、Ant Contrib Tasks、Checkstyle、EMMA、Java2Html、PMD 和 Rename Packages
  • 测试框架:DbUnit、Dumbster、jMock、JUnit 和 Canoo WebTest
  • 数据库驱动程序:MySQL 和 PostgreSQL
  • 持久性框架:Hibernate 和 iBATIS
  • IoC 框架:Spring
  • Web 框架:JSF、Spring MVC、Struts、Tapestry 和 WebWork
  • Web 服务:XFire
  • Web 工具:Clickstream、Display Tag、DWR、JSTL、SiteMesh、Struts Menu 和 URL Rewrite Filter
  • Security:Acegi Security
  • JavaScript 和 CSS:Scriptaculous、Prototype 和 Mike Stenhouse 的 CSS Framework

除了这些库之外,AppFuse 还使用 Log4j 来记录日志,使用 Velocity 来构建 e-mail 和菜单模板。Tomcat 可以支持最新的开发,我们可以使用 1.4 或 5 版本的 Java 平台来编译或构建程序。我们应该可以将 AppFuse 部署到任何 J2EE 1.3 兼容的应用服务器上;这已经经过了测试,我们知道它在所有主要版本的 J2EE 服务器和所有主要的 servlet 容器上都可以很好地工作。

图 3 给出了上面创建的 devworks 项目的 lib 目录。这个目录中的 lib.properties 文件控制了每个依赖性的版本号,这意味着我们可以简单地通过把这些包的新版本放到这个目录中并执行诸如 ant test-all -Dspring.version=2.0 之类的命令来测试这些包的新版本。


图 3. 项目依赖性
AppFuse 项目依赖性

预先集成这些开放源码库可以在项目之初极大地提高生产效率。尽管我们可以找到很多文档介绍如何集成这些库,但是定制工作示例并简单地使用它来开发应用程序要更加简单。

除了可以简化 Web 应用程序的开发之外,AppFuse 让我们还可以将 Web 服务简单地集成到自己的项目中。尽管 XFire 也在 AppFuse 下载中一起提供了,不过如果我们希望,也可以自己集成 Apache Axis(请参看 参考资料 中有关 Axis 集成的教程)。另外,Spring 框架和 XFire 可以一起将服务层作为 Web 服务非常简单地呈现出来,这就为我们提供了开发面向服务架构的能力。

另外,AppFuse 并不会将我们限定到任何特定的 API 上。它只是简单地对可用的最佳开放源码解决方案重新进行打包和预先集成。AppFuse 中的代码可以处理这种集成,并实现了 AppFuse 的基本安全性和可用性特性。只要可能,就会减少代码,以便向 AppFuse 的依赖框架添加一个特性。例如,AppFuse 自带的 Remember Me 和 SSL 切换特性最近就因为类似的特性而从 Acegi Security 中删除了。





回页首


理由 3:自动化

Ant 使得简化了从编译到构建再到部署的自动化过程。Ant 是 AppFuse 中的一等公民,这主要是因为我发现在命令行中执行操作比从 IDE 中更加简单。我们可以使用 Ant 实现编译、测试、部署和执行任何代码生成的任务。

尽管这种能力对于有些人来说非常重要,但是它并不适用于所有的人。很多 AppFuse 用户目前都使用 Eclipse 或 Intellij IDEA 来构建和测试自己的项目。在这些 IDE 中运行 Ant 的确可以工作,但是这样做的效率通常都不如使用 IDE 内置的 JUnit 支持来运行测试效率高。

幸运的是,AppFuse 支持在 IDE 中运行测试,不过管理这种特性对于 AppFuse 开发人员来说就变得非常困难了。最大的痛苦在于 XDoclet 用来生成 Hibernate 映射文件和 Web 框架所使用的一些工件(例如 ActionForms 和 Struts 使用的 struts-config.xml)。IDE 并不知道需要生成的代码,除非我们配置使用 Ant 来编译它们,或者安装了一些可以认识 XDoclet 的插件。

这种对知识的缺乏是 AppFuse 2.0 切换到 JDK 5 和 Maven 2 上的主要原因。JDK 5、注释和 Struts 2 将让我们可以摆脱 XDoclet。Maven 2 将使用这些生成的文件和动态类路径来生成 IDE 项目文件,这样对项目的管理就可以进行简化。目前基于 Ant 的编译系统已经为不同的层次生成了一些工件(包括 dao.jar、service.jar 和 webapp.war),因此切换到 Maven 的模型上应该是一个非常自然的调整。

除了 Ant 之外(它对于编译、测试、部署和报告具有丰富的支持),对于 CruiseControl 的支持也构建到了 AppFuse 中。CruiseControl 是一个 Continuous Integration 应用程序,让我们可以在源代码仓库中代码发生变化时自动运行所有的测试。extras/cruisecontrol 目录包含了我们为基于 AppFuse 的项目快速、简单地设置 Continuous Integration 时所需要的文件。

设置 Continuous Integration 是软件开发周期中我们首先要做的事情之一。它不但激发程序员去编写测试用例,而且还通过 “You broke the build!” 游戏促进了团队之间的合作和融合。





回页首


理由 4:安全特性和可扩展性

AppFuse 最初是作为我为 Apress 编写的书籍 Pro JSP 中示例应用程序的一部分开发的。这个示例应用程序展示了很多安全特性和用于简化 Struts 开发的特性。这个应用程序中的很多安全特性在 J2EE 的安全框图中都不存在。使用容器管理认证(CMA)的认证方法非常简单,但是 Remember Me、密码提示、SSL 切换、登记和用户管理等功能却都不存在。另外,基于角色的保护方法功能在非 EJB 环境中也是不可能的。

最初,AppFuse 使用自己的代码和用于 CMA 的解决方案完全实现了这些特性。我在 2004 年年初开始学习 Spring 时就听说过有关 Acegi Security 的知识。我对 Acegi 所需要的 XML 的行数(175)与 web.xml 中所需要的 CMA 的行数(20)进行了比较,很快就决定丢弃 Acegi 了,因为它太过复杂了。

一年半之后 —— 在为另外一本书 Spring Live 中编写了一章有关使用 Acegi Security 的内容之后 —— 我就改变了自己的想法。Acegi 的确(目前仍然)需要很多 XML,但是一旦我们理解了这一点,它实际上是相当简单的。当我们最终作出改变,使用 Acegi Security 的特性来全部取代 AppFuse 的特性之后,我们最终删除了大量的代码。类之上的类都已经没有了,“Acegi handles that now” 中消失的部分现在全部进入了 CVS 的 Attic 中了。

Acegi Security 是 J2EE 安全模型中曾经出现过的最好模型。它让我们可以实现很多有用的特性,这些特性在 Servlet API 的安全模型中都不存在:认证、授权、角色保护方法、Remember Me、密码加密、SSL 切换、用户切换和注销。它让我们还可以将用户证书存储到 XML 文件、数据库、LDAP 或单点登录系统(例如 Yale 的 Central Authentication Service (CAS) 或者 SiteMinder)中。

AppFuse 对很多与安全性相关的特性的实现从一开始都是非常优秀的。现在 AppFuse 使用了 Acegi Security,这些特性 —— 以及更多特性 —— 都非常容易实现。Acegi 有很多地方都可以进行扩充:这是它使用巨大的 XML 配置文件的原因。正如我们已经通过去年的课程对 Acegi 进行集成一样,我们已经发现对很多 bean 的定义进行定制可以更加紧密地与 AppFuse 建立联系。

Spring IoC 容器和 Acegi Security 所提供的简单开发、容易测试的代码和松耦合特性的组合是 AppFuse 是这么好的一种开发平台的主要原因。这些框架都是不可插入的,允许生成干净的可测试代码。AppFuse 集成了很多开放源码项目,依赖注入允许对应用程序层进行简单的集成。





回页首


理由 5:使用 AppGen 生成代码

有些人会将代码生成称为代码气味的散播(code smell)。在他们的观点中,如果我们需要生成代码,那么很可能就会做一些错事。我倾向于这种确定自己代码使用的模式和自动化生成代码的能力应该称为代码香味的弥漫(code perfume)。如果我们正在编写类似的 DAO、管理器、操作或控件,并且不想为它们生成代码,那么这就需要根据代码的气味来生成代码。当然,当语言可以为我们提供可以简化任务的特性时,一切都是那么美好;不过代码生成通常都是一个必需 —— 通常其生产率也非常高 —— 的任务。

AppFuse 中提供了一个基于 Ant 和 XDoclet 的代码生成工具,名叫 AppGen。默认情况下,常见的 DAO 和管理器都可以允许我们对任何普通老式 Java 对象(POJO)进行 CRUD 操作,但是在 Web 层上这样做有些困难。AppGen 有几个特性可以用来执行以下任务:

  • (使用 Middlegen 和 Hibernate 工具)从数据库表中生成 POJO
  • 从 POJO 生成 UI
  • 为 DAO、管理器、操作/控制器和 UI 生成测试

在运行 AppGen 时,您会看到提示说 AppGen 要从数据库表或 POJO 中生成代码。如果在命令行中执行 ant install-detailed,AppGen 就会安装 POJO 特定的 DAO、管理器以及它们的测试。运行 ant install 会导致 Web 层的类重用通用的 DAO 和默认存在的管理器。

为了阐述 AppGen 是如何工作的,我们在 devworks MySQL 数据库中创建了如清单 2 所示的表:


清单 2. 创建一个名为 cat 的数据库表
    create table cat (
      cat_id int(8) auto_increment,
      color varchar(20) not null,
      name varchar(20) not null,
      created_date datetime not null,
      primary key (cat_id)
    ) type=InnoDB;

在 extras/appgen 目录中,运行 ant install-detailed。这个命令的输出结果对于本文来说实在太长了,不过我们在清单 3 中给出了第一部分的内容:


清单 3. 运行 AppGen 的 install-detailed 目标
$ ant install-detailed
Buildfile: build.xml

init:
    [mkdir] Created dir: /Users/mraible/Work/devworks/extras/appgen/build
     [echo] 
     [echo] +-------------------------------------------------------+
     [echo] |             -- Welcome to the AppGen! --              |
     [echo] |                                                       |
     [echo] | Use the "install" target to use the generic DAO and   |
     [echo] | Manager, or use "install-detailed" to general a DAO   |
     [echo] | and Manager specifically for your model object.       |
     [echo] +-------------------------------------------------------+

    [input] Would you like to generate code from a table or POJO? (table,pojo)
table
    [input] What is the name of your table (i.e. person)?
cat
    [input] What is the name, if any, of the module for your table (i.e. organization)?

     [echo] Running Middlegen to generate POJO...

要对 cat 表使用这个新生成的代码,我们需要修改 src/dao/com/ibm/dao/hibernate/applicationContext-hibernate.xml,来为 Hibernate 添加 Cat.hbm.xml 映射文件。清单 3 给出了我们修改后的 sessionFactory bean 的样子:


清单 4. 将 Cat.hbm.xml 添加到 sessionFactory bean 中
    <bean id="sessionFactory" class="...">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>com/ibm/model/Role.hbm.xml</value>
            <value>com/ibm/model/User.hbm.xml</value>
            <value>com/ibm/model/Cat.hbm.xml</value>
        </list>
    </property>
    ...
</bean>

在运行 ant setup deploy 之后,我们就应该可以在部署的应用程序中对 cat 表执行 CRUD 操作了:


图 4. Cat 列表
所生成的主屏幕

图 5. Cat 表单
所生成的详细屏幕

我们在上面的屏幕快照中看到的记录都是作为代码生成的一部分创建的,因此现在就有测试数据了。





回页首


理由 6:文档

我们可以找到 AppFuse 各个风味的教程,并且它们都以 6 种不同的语言给出了:中文、德语、英语、韩语、葡萄牙语和西班牙语。使用风味(flavor) 一词,我的意思是不同框架的组合,例如 Spring MVC 加上 iBATIS、Spring MVC 加上 Hibernate 或 JSF 加上 Hibernate。使用这 5 种 Web 框架和两种持久框架,可以有好几种组合。有关它们的翻译,AppFuse 为自己的默认特性提供了 8 种翻译。可用语言包括中文、荷兰语、德语、英语、法语、意大利语、葡萄牙语和西班牙语。

除了核心教程之外,还添加了很多教程(请参看 参考资料) 来介绍与各种数据库、应用服务器和其他开放源码技术(包括 JasperReports、Lucene、Eclipse、Drools、Axis 和 DWR)的集成。





回页首


理由 7:社区

Apache 软件基金会对于开放源码有一个有趣的看法。它对围绕开放源码项目开发一个开放源码社区最感兴趣。它的成员相信如果社区非常强大,那么产生高质量的代码就是一个自然的过程。下面的内容引自 Apache 主页:

“我们认为自己不仅仅是一组共享服务器的项目,而且是一个开发人员和用户的社区。”

AppFuse 社区从 2003 年作为 SourceForge 上的一个项目(是 struts.sf.net 的一部分)启动以来,已经获得了极大的增长。通过在 2004 年 3 月转换到 java.net 上之后,它已经成为这里一个非常流行的项目,从 2005 年 1 月到 3 月成为访问量最多的一个项目。目前它仍然是一个非常流行的项目(有关 java.net 项目统计信息的链接,请参看 参考资料),不过在这个站点上它正在让位于 Sun 赞助的很多项目。

在 2004 年年末,Nathan Anderson 成为继我之后第一个提交者。此后有很多人都加入了进来,包括 Ben Gill、David Carter、Mika G?ckel、Sanjiv Jivan 和 Thomas Gaudin。很多现有的提交者都已经通过各种方式作出了自己的贡献,他们都帮助 AppFuse 社区成为一个迅速变化并且非常有趣的地方。

邮件列表非常友好,我们试图维护这样一条承诺 “没有问题是没有人理会的问题”。我们的邮件列表归档文件中惟一一条 “RTFM” 是从用户那里发出的,而不是从开发者那里发出的。我们绝对信奉 Apache 开放源码社区的哲学。引用我最好的朋友 Bruce Snyder 的一句话,“我们为代码而来,为人们而留下”。目前,大部分开发者都是用户,我们通常都喜欢有一段美妙的时间。另外,大部分文档都是由社区编写的;因此,这个社区的知识是非常渊博的。





回页首


结束语

我们应该尝试使用 AppFuse 进行开发,这是因为它允许我们简单地进行测试、集成、自动化,并可以安全地生成 Web 应用程序。其文档非常丰富,社区也非常友好。随着其支撑框架越来越好,AppFuse 也将不断改进。

从 AppFuse 2.0 开始,我们计划迁移到 JDK 5(仍然支持部署到 1.4)和 Maven 2 上去。这些工具可以简化使用 AppFuse 的开发、安装和升级。我们计划充分利用 Maven 2 的功能来处理相关依赖性。我们将碰到诸如 appfuse-hibernate-2.0.jar 和 appfuse-jsf-2.0.jar 之类的工件。这些工件都可以在 pom.xml 文件中进行引用,它们负责提取其他相关依赖性。除了在自己的项目中使用 AppFuse 基类之外,我们还可以像普通的框架一样在 JAR 中对这些类简单地进行扩展,这应该会大大简化它的升级过程,并鼓励更多用户将自己希望的改进提交到这个项目中。

如果没有其他问题,使用 AppFuse 可以让您始终处于 Java Web 开发的技术前沿上 —— 就像我们一样!



参考资料

学习

获得产品和技术
  • AppFuse on java.net:下载不同风味的 AppFuse。

  • WebWork:了解这个易于使用的 Web 框架。

  • DbUnit:查看更多有关 JUnit 扩展的内容。

  • jMock:创建动态仿真对象来简化真正的单元测试。

  • Canoo WebTest:自动化 Web 应用程序的 UI 测试。

  • HtmlUnit:WebTest 的优秀 JavaScript 支持背后的基础。

  • Cargo:自动启动和停止容器。

  • Greenbox:一种代码生成框架。


讨论


关于作者

Matt Raible 居住在美国科罗拉多州的丹佛,他在那里是 Spring 和 Web 框架对 Virtuas Open Source Solutions 的实践先驱。他在开放源码领域具有丰富的经验,是这个领域的专家。他在这个领域中既是用户,又是一名开发人员。Matt 是 SourceBeat PublishingSpring Live 的作者。他还为 Apress 的书籍 Pro JSP Third Edition 作出了很大的贡献。他是很多开放源码会议的积极倡导者,包括 ApacheCon、MySQL User's Conference 和 OSCON,同时他还是 http://raibledesigns.com 上一名非常活跃的博客。Raible 的大部分生活都被计算机所包围了,尽管他是在连电都没有的 Montana 长大的。当不工作的时候时,他总是试图让妻子 Julie 成为世界上最幸福的女人,或者与他们的孩子 Abbie 和 Jack 一起玩耍。

Seven simple reasons to use AppFuse

Learn about -- and be more productive with -- Java open source tools

developerWorks
Document options
<noscript></noscript> <noscript></noscript> <script language="JavaScript" type="text/javascript"> <!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img alt="Set printer orientation to landscape mode" height="16" src="//www.ibm.com/i/v14/icons/printer.gif" width="16" vspace="3" /></td><td width="122"><p><b><a class="smallplainlink" href="javascript:print()">Print this page</a></b></p></td></tr>'); //--> </script> <script language="JavaScript" type="text/javascript"> <!-- document.write('<tr valign="top"><td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt=""/></td><td width="16"><img src="//www.ibm.com/i/v14/icons/em.gif" height="16" width="16" vspace="3" alt="Email this page" /></td><td width="122"><p><a class="smallplainlink" href="javascript:void newWindow()"><b>E-mail this page</b></a></p></td></tr>'); //--> </script>
Set printer orientation to landscape mode

Print this page

Email this page

E-mail this page

<!--START RESERVED FOR FUTURE USE INCLUDE FILES--><!-- 11/29/06 commented out by Jill--><!--<br /> <table border="0" cellpadding="0" cellspacing="0" width="150"> <tr> <td class="v14-header-2-small">Using XML, but need to do more?</td> </tr> </table> <table border="0" cellpadding="0" cellspacing="0" class="v14-gray-table-border"> <tr> <td width="150" class="no-padding"> <table border="0" cellpadding="0" cellspacing="0" width="143"> <tr valign="top"> <td width="8"><img src="//www.ibm.com/i/c.gif" width="8" height="1" alt="" /></td> <td><img src="//www.ibm.com/i/v14/icons/fw_bold.gif" height="16" width="16" border="0" vspace="3" alt="" /></td> <td width="125"> <p><a href="http://www.ibm.com/developerworks/kickstart/database.html?S_TACT=105AGX01&amp;S_CMP=SIMPLEART" class="smallplainlink">Download DB2 Express-C 9</a> </p> </td> </tr> </table> </td> </tr> </table> --><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->
Rate this page

Help us improve this content


Level: Introductory

Matt Raible (mraible@virtuas.com), Open Source Practice Leader, Virtuas Open Source Solutions

08 Aug 2006

Getting started with open source tools for the Java™ platform such as Spring, Hibernate, or MySQL can be difficult. Throw in Ant or Maven, a little Ajax with DWR, and a Web framework -- say, JSF -- and you're up to your eyeballs just trying to configure your application. AppFuse removes the pain of integrating open source projects. It also makes testing a first-class citizen, allows you to generate your entire UI from database tables, and supports Web services with XFire. Furthermore, AppFuse's community is healthy and happy -- and one of the few places where users of different Web frameworks actually get along.
<!--START RESERVED FOR FUTURE USE INCLUDE FILES--> <script language="JavaScript" type="text/javascript"> <!-- if (document.referrer&&document.referrer!="") { // document.write(document.referrer); var q = document.referrer; var engine = q; var isG = engine.search(/google/.com/i); var searchTerms; //var searchTermsForDisplay; if (isG != -1) { var i = q.search(/q=/); var q2 = q.substring(i+2); var j = q2.search(/&/); j = (j == -1)?q2.length:j; searchTerms = q.substring(i+2,i+2+j); if (searchTerms.length != 0) { searchQuery(searchTerms); document.write("<div id=/"contents/"></div>"); } } } //--> </script><!--END RESERVED FOR FUTURE USE INCLUDE FILES-->

AppFuse is an open source project and application that uses open source tools built on the Java platform to help you develop Web applications quickly and efficiently. I originally developed it to eliminate the ramp-up time I often found when building new Web applications for customers. At its core, AppFuse is a project skeleton, similar to the one that's created by your IDE when you click through a wizard to create a new Web project. When you create a project with AppFuse, it prompts you for the open source frameworks you'd like to use and then creates your project. It uses Ant to drive testing, code generation, compilation, and deployment. It provides your directory and package structure, as well as the libraries you'll need to develop a Java language-based Web application.

Unlike the products of most "new project" wizards, AppFuse-created projects contain a number of classes and files from the very beginning. These files are used to implement features, but they also serve as examples for you when you're developing your application. By using AppFuse to start new projects, it's possible to eliminate the usual first week or two of development time. You don't have to worry about configuring open source frameworks together because that's already done for you. Your project is preconfigured to talk to a database, deploy in an application server, and authenticate users. There's no need for you to implement security features because they're already integrated.

When I first developed AppFuse, it only supported Struts and Hibernate. Over the years, I've found better Web frameworks than Struts, so I added options for them as well. Today, AppFuse supports Hibernate or iBATIS as persistence frameworks. For the Web framework, you can use JavaServer Faces (JSF), Spring MVC, Struts, Tapestry, or WebWork.

AppFuse comes out of the box with features that many applications need, including:

  • Authentication and authorization
  • User management
  • Remember Me (which saves your login information so you don't have to log in every time)
  • Password reminder
  • Signup and registration
  • SSL switching
  • E-mail
  • URL rewriting
  • Skinability
  • Page decoration
  • Templated layout
  • File upload

This out-of-the-box functionality is one of the main things that separate AppFuse from the other CRUD generation frameworks (from create, retrieve, update and delete), including Ruby on Rails, Trails, and Grails. The aforementioned frameworks, as well as AppFuse, allow you to generate master/detail pages from database tables or existing model objects.

Figure 1 illustrates the conceptual design of a typical AppFuse application:


Figure 1. Typical AppFuse application
Typical AppFuse application

Listing 1 illustrates the command-line interaction you'd go through to create a project named devworks, along with the resulting output. This project uses WebWork as its Web framework (see the Resources section below for a link).


Listing 1. Creating a new project with AppFuse
alotta:~/dev/appfuse mraible$ ant new
Buildfile: build.xml

clean:
     [echo] Cleaning build and distribution directories

init:

new:
     [echo] 
     [echo] +-------------------------------------------------------------+
     [echo] |    -- Welcome to the AppFuse New Application Wizard! --     |
     [echo] |                                                             |
     [echo] | To create a new application, please answer the following    |
     [echo] | questions.                                                  |
     [echo] +-------------------------------------------------------------+

    [input] What would you like to name your application [myapp]?
devworks
    [input] What would you like to name your database [mydb]?
devworks
    [input] What package name would you like to use [org.appfuse]?
com.ibm
    [input] What web framework would you like to use [webwork,tapestry,spring,js
f,struts]?
webwork
     [echo] Creating new application named 'devworks'...
     [copy] Copying 359 files to /Users/mraible/Work/devworks
     [copy] Copying 181 files to /Users/mraible/Work/devworks/extras
     [copy] Copying 1 file to /Users/mraible/Work/devworks
     [copy] Copying 1 file to /Users/mraible/Work/devworks

install:
     [echo] Copying WebWork JARs to ../../lib
     [copy] Copying 6 files to /Users/mraible/Work/devworks/lib
     [echo] Adding WebWork entries to ../../lib.properties
     [echo] Adding WebWork classpath entries
     [echo] Removing Struts-specific JARs
   [delete] Deleting directory /Users/mraible/Work/devworks/lib/struts-1.2.9
   [delete] Deleting directory /Users/mraible/Work/devworks/lib/strutstest-2.1.3
     [echo] Deleting struts_form.xdt for XDoclet
   [delete] Deleting directory /Users/mraible/Work/devworks/metadata/templates
     [echo] Deleting Struts merge-files in metadata/web
   [delete] Deleting 7 files from /Users/mraible/Work/devworks/metadata/web
     [echo] Deleting unused Tag Libraries and Utilities
   [delete] Deleting 2 files from /Users/mraible/Work/devworks/src/web/org/appfu
se/webapp
     [echo] Modifying appgen for WebWork
     [copy] Copying 12 files to /Users/mraible/Work/devworks/extras/appgen
     [echo] Replacing source and test files
   [delete] Deleting directory /Users/mraible/Work/devworks/src/web/org/appfuse/
webapp/form
   [delete] Deleting directory /Users/mraible/Work/devworks/src/web/org/appfuse/
webapp/action
     [copy] Copying 13 files to /Users/mraible/Work/devworks/src
   [delete] Deleting directory /Users/mraible/Work/devworks/test/web/org/appfuse
/webapp/form
   [delete] Deleting directory /Users/mraible/Work/devworks/test/web/org/appfuse
/webapp/action
     [copy] Copying 5 files to /Users/mraible/Work/devworks/test
     [echo] Replacing web files (images, scripts, JSPs, etc.)
   [delete] Deleting 1 files from /Users/mraible/Work/devworks/web/scripts
     [copy] Copying 34 files to /Users/mraible/Work/devworks/web
   [delete] Deleting: /Users/mraible/Work/devworks/web/WEB-INF/validator-rules-c
ustom.xml
     [echo] Modifying Eclipse .classpath file
     [echo] Refactoring build.xml
     [echo] ----------------------------------------------
     [echo] NOTE: It's recommended you delete extras/webwork as you shouldn't ne
ed it anymore.
     [echo] ----------------------------------------------
     [echo] Repackaging info written to rename.log
     [echo] 
     [echo] +-------------------------------------------------------------+
     [echo] |           -- Application created successfully! --           |
     [echo] |                                                             |
     [echo] | Now you should be able to cd to your application and run:   |
     [echo] | > ant setup test-all                                        |
     [echo] +-------------------------------------------------------------+

BUILD SUCCESSFUL
Total time: 15 seconds

Why WebWork?
The Struts community has recently embraced WebWork and the combination has resulted in a great new Web framework for the Java platform: Struts 2. Sure, Spring MVC is a nice request-based framework, but it does not support JSF the way Struts 2 does. Component-based frameworks such as JSF and Tapestry are nice, but I've found WebWork to be more intuitive and easier to work with. (See Resources for more on Struts 2 and JSF.)

After you create a project, you wind up with a directory structure similar to the one illustrated in Figure 2. Eclipse and Intellij IDEA project files are created as part of this process.


Figure 2. Project directory structure
Project directory structure

This directory structure is very close to what Sun recommends for Java 2 Platform, Enterprise Edition (J2EE) Web applications. In version 2.0 of AppFuse, this structure will change to match the standard directory layout for the Apache Maven project (see Resources for links to both directory guidelines). AppFuse will also move from Ant to Maven 2 to obtain capabilities for transitive dependency downloading and support for generating IDE project files. The current Ant-based system requires that committers maintain project files, whereas Maven 2 can generate IDEA, Eclipse, and NetBeans project files simply by using the project's pom.xml file. (This file, located in your project's root directory, is the main component you need to build an application with Maven. It's very similar to the build.xml file you use with Ant.)

Now that you have a sense of what AppFuse is, in the remainder of this article, I'll cover seven simple reasons to use it. Even if you choose not to use it to start your projects, you will see that it provides much of the boilerplate code that can be used in Java language-based Web applications. Because it's Apache licensed, you're more than welcome to reuse any of its code in your own applications.

Reason #1: Testing

Testing is something that's rarely given enough credibility in software development projects. Notice that I didn't say that it doesn't get credit in software development publications! Many articles and case studies illustrate instances when test-first development and high test coverage can increase the quality of software. Regardless, testing is often viewed as something that simply lengthens a project's time to completion. In reality, if you use test-first methodologies to write your tests before your code, I believe you'll find that you'll actually speed up your development time. Furthermore, testing first makes maintenance and refactoring much easier. If you don't write code to test your code, you'll need to manually test your application by clicking through it -- and that's simply not very productive. Automation is key.

When you first start using AppFuse, you'll probably read the QuickStart Guide and Tutorials available on the project Web site (see Resources for links). The tutorials are written so that you write your tests first; they won't compile until you write the interface and/or implementation. If you're anything like me, you really should write the tests before you start writing code; it's the only way you'll be truly motivated to write them. If you write the implementation first and somehow verify that it works, you'll probably say to yourself, "Heck, it seems to be OK -- who needs a test? I've got more code to write!" The unfortunate aspect of this scenario is that you most likely did something to test that code; you simply skipped the part where you automated that test.

AppFuse's documentation shows you how to test all of the layers of your application. It starts at the database layer and uses DbUnit (see Resources) to pre-populate your database with test data before running tests. At the data access (DAO) layer, it uses Spring's AbstractTransactionalDataSourceSpringContextTests class (yes, that's a real class name!) to allow easy loading of Spring context files. Furthermore, this class wraps a transaction around each testXXX() method and rolls it back when the test method exits. This feature makes testing your DAO logic easy and does not affect the data in your database.

In the service layer, jMock (see Resources) is used to write true unit tests that mock their DAO dependencies. This allows for quick and fast tests that verify that your business logic is correct; you don't need to worry about about the underlying persistence logic.

HtmlUnit support
The HtmlUnit team has done a fair amount of work in the 1.8 release to make sure the package works with popular Ajax frameworks like Prototype and Scriptaculous.

In the Web tier, tests verify that actions (Struts/WebWork), controllers (Spring MVC), pages (Tapestry), and managed beans (JSF) work as you'd expect them to. Spring's spring-mock.jar is useful for testing all of these frameworks because it contains a mock implementation of the Servlet API. Testing AppFuse's Web frameworks would be difficult without this useful library.

The UI is usually the most difficult part to develop in a Web application. It's the piece that customers most often complain about -- either because it's not pretty or because it doesn't work the way they expect it to work. Plus, nothing is worse than doing a demo in front of a customer and seeing a stacktrace in the middle of it! Your application may be awesome, but your customer will have questions about your thoroughness. Don't let it happen. Canoo WebTest tests the UI. It employs HtmlUnit under the covers to walk through your UI, verify that elements are present and fill in form fields, and even validate that a fancy Ajax-enabled UI is working as expected. (See Resources for links to both WebTest and HtmlUnit.)

To make Web testing even easier, Cargo (see Resources) automates the starting and stopping of Tomcat before and after WebTest tests are run.



Back to top


Reason #2: Integration

As I mentioned in this article's introduction, many open source libraries are pre-integrated in AppFuse. They fall into the following categories:

  • Building, reporting, and code generation: Ant, Ant Contrib Tasks, Checkstyle, EMMA, Java2Html, PMD, and Rename Packages
  • Testing frameworks: DbUnit, Dumbster, jMock, JUnit, and Canoo WebTest
  • Database drivers: MySQL and PostgreSQL
  • Persistence frameworks: Hibernate and iBATIS
  • IoC frameworks: Spring
  • Web frameworks: JSF, Spring MVC, Struts, Tapestry, and WebWork
  • Web services: XFire
  • Web utilities: Clickstream, Display Tag, DWR, JSTL, SiteMesh, Struts Menu, and URL Rewrite Filter
  • Security: Acegi Security
  • JavaScript and CSS: Scriptaculous, Prototype, and Mike Stenhouse's CSS Framework

In addition to these libraries, AppFuse uses Log4j for logging and Velocity to construct e-mail and menu templates. Tomcat is supported out of the box for development, and you can use versions 1.4 or 5 of the Java platform to compile and build. You should be able to deploy AppFuse on any J2EE 1.3-compliant application server; it has been tested and is known to work on all the major J2EE servers and all of the major servlet containers.

Figure 3 shows the lib directory from the previously created devworks project. The lib.properties file in this directory controls the version numbers for each dependency, which means that you can easily test a new version of any of these packages by dropping it in this directory and issuing a command line like ant test-all -Dspring.version=2.0.


Figure 3. Project dependencies
AppFuse project dependencies

Having all of these open source libraries pre-integrated can supply a huge boost in productivity at the beginning of a project. Although you can find a fair amount of documentation for integrating these libraries, customizing a working example and simply using it to develop your application is much easier.

Besides simplifying the development of Web applications, AppFuse allows you to integrate Web services into your projects easily. XFire is included with the AppFuse download, but you can also integrate Apache Axis if you'd prefer (see Resources for the Axis integration tutorial). Together, the Spring framework and XFire make exposing your service layer as Web services very easy, giving you the ability to develop a service-oriented architecture.

In addition, AppFuse does not lock you into any particular API. It's simply repackaging and pre-integrating the best open source solutions available. The code that exists in AppFuse handles this integration and implements AppFuse's basic security and usability features. When possible, code is removed in favor of adding a feature to one of AppFuse's dependent frameworks. For example, AppFuse's home-grown Remember Me and SSL switching features were recently removed in favor of similar features from Acegi Security.



Back to top


Reason #3: Automation

Ant makes automation easy, from compiling to building to deploying. Ant is a first-class citizen in AppFuse, primarily because I've found it easier to run things from the command line than from an IDE. You can build, test, deploy, and perform any code-generation tasks using Ant.

Although this capability is great for some people, it doesn't work for everyone. Many AppFuse users currently use Eclipse or Intellij IDEA to build and test their projects. Running Ant from inside these IDEs works, but it isn't nearly as productive as running tests using the IDEs' built-in JUnit support.

Luckily, AppFuse supports running tests from the IDEs themselves, but maintaining this feature has been difficult for AppFuse developers. The biggest pain point is the fact that XDoclet is used to generate Hibernate mapping files and some artifacts for the Web frameworks (such as ActionForms and struts-config.xml for Struts). IDEs are unaware that code needs to be generated unless you configure them to build with Ant or install some sort of XDoclet-aware plug-in.

This lack of awareness is one of the primary reasons that AppFuse 2.0 will move to JDK 5 and Maven 2. JDK 5, annotations, and Struts 2 will allow us to get rid of XDoclet. Maven 2 will generate IDE project files, and with these generated files and dynamic classpaths, managing your project will be easier. The current Ant-based build system already produces artifacts for the different layers (including dao.jar, service.jar, and webapp.war), so moving to Maven's model should be a natural fit.

In addition to Ant (and a rich set of targets for building, testing, deploying, and reporting), support for CruiseControl is built into AppFuse. CruiseControl is a Continuous Integration application that allows you to automatically run all of your tests whenever code changes in your source code repository. The extras/cruisecontrol directory contains the files you need to quickly and easily set up Continuous Integration for your AppFuse-based project.

Setting up Continuous Integration is one of the first things you should do in a software development cycle. It not only motivates programmers to write tests, it also promotes teamwork and bonding through "You broke the build!" games.



Back to top


Reason #4: Security features and extensibility

AppFuse was originally developed as part of a sample application for a book I wrote for Apress, Pro JSP. This sample application demonstrated many security features and features for simplifying Struts development. Many of the security features in this application did not exist in J2EE's security paradigm. Authentication using container-managed authentication (CMA) was easy, but Remember Me, password hints, SSL switching, signup, and user management were nonexistent. Furthermore, the ability to protect methods based on roles was not possible in a non-EJB environment.

At first, AppFuse implemented all of these features with its own code and workarounds for CMA. I'd heard about Acegi Security when I first started learning Spring in early 2004. I compared the number of lines of XML required by Acegi (175) with the number that CMA required in web.xml (20) and quickly dismissed Acegi as too complicated.

A year and a half later -- and after writing a chapter about using Acegi Security for another book, Spring Live -- I had changed my mind. Acegi did (and still does) require a fair amount of XML, but it really is quite simple once you understand it. When we finally took the plunge and replaced all AppFuse's home-grown features with Acegi Security's features, we ended up deleting a lot of code. Classes upon classes went away, disappearing into the "Acegi handles that now" pile in CVS's Attic.

Acegi Security is simply the best thing that's ever happened to J2EE's security model. It allows you to implement many useful features that aren't part of the Servlet API's security model: authentication, authorization, role-protected methods, Remember Me, password encryption, SSL switching, user switching, and logout. It also allows you to store your user's credentials in an XML file, in a database, in LDAP, or in a single sign-on system such as Yale's Central Authentication Service (CAS) or SiteMinder.

AppFuse's implementation of many security-related features was nice in the beginning. Now that AppFuse uses Acegi Security, these features -- and many more -- are easy to implement. Acegi has many points for extension: that is the reason for its large XML configuration file. As we've integrated Acegi over the course of the last year, we've found that we've customized many bean definitions to hook into AppFuse more closely.

The combined ease of development, easily testable code, and loose coupling provided by the Spring IoC container and Acegi Security are the primary reasons that AppFuse is such a pleasure to develop with. These frameworks are nonintrusive and allow clean, testable code. AppFuse integrates many open source projects, and dependency injection allows easy integration of your application's layers.



Back to top


Reason #5: Code generation with AppGen

Some people call code generation code smell. In their opinion, if you need to generate code, you must be doing something wrong. I would argue that the ability to identify the patterns your code uses and automate their generation is code perfume. If you're writing similar DAOs, managers, and actions or controllers and you're not generating the code for them, that's a code smell. Sure, it's nice when the language gives you features like generics to make things easier, but often code generation is a necessary -- and extremely productive -- task.

AppFuse ships with an Ant- and XDoclet-based code-generation tool named AppGen. By default, generic DAOs and managers allow you to CRUD any plain old Java object (POJO), but doing that at the Web tier is difficult. AppGen has several features for performing the following tasks:

  • Generating POJOs from database tables (using the Middlegen and Hibernate tools)
  • Generating the UI from POJOs
  • Generating tests for DAOs, managers, actions/controllers, and the UI

When you run AppGen, you're prompted to generate from a database table or a POJO, and AppGen generates the code. If you issue ant install-detailed at the command line, AppGen installs POJO-specific DAOs and managers with their tests. Running ant install causes your Web tier classes to reuse the generic DAO and manager that exist by default.

To illustrate how AppGen works, I've created the table illustrated in Listing 2 in the devworks MySQL database.:


Listing 2. Create a database table named cat
    create table cat (
      cat_id int(8) auto_increment,
      color varchar(20) not null,
      name varchar(20) not null,
      created_date datetime not null,
      primary key (cat_id)
    ) type=InnoDB;

From the extras/appgen directory, run ant install-detailed. The full output from this command is too long to list for this article, but I've supplied the first part in Listing 3:


Listing 3. Running AppGen's install-detailed target
$ ant install-detailed
Buildfile: build.xml

init:
    [mkdir] Created dir: /Users/mraible/Work/devworks/extras/appgen/build
     [echo] 
     [echo] +-------------------------------------------------------+
     [echo] |             -- Welcome to the AppGen! --              |
     [echo] |                                                       |
     [echo] | Use the "install" target to use the generic DAO and   |
     [echo] | Manager, or use "install-detailed" to general a DAO   |
     [echo] | and Manager specifically for your model object.       |
     [echo] +-------------------------------------------------------+

    [input] Would you like to generate code from a table or POJO? (table,pojo)
table
    [input] What is the name of your table (i.e. person)?
cat
    [input] What is the name, if any, of the module for your table (i.e. organization)?

     [echo] Running Middlegen to generate POJO...

To use the newly generated code for the cat table, you need to modify src/dao/com/ibm/dao/hibernate/applicationContext-hibernate.xml to add the Cat.hbm.xml mapping file for Hibernate. Listing 4 illustrates what your modified sessionFactory bean should look like:


Listing 4. Adding Cat.hbm.xml to your sessionFactory bean
    <bean id="sessionFactory" class="...">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>com/ibm/model/Role.hbm.xml</value>
            <value>com/ibm/model/User.hbm.xml</value>
            <value>com/ibm/model/Cat.hbm.xml</value>
        </list>
    </property>
    ...
</bean>

After running ant setup deploy, you should be able to CRUD the cat table from the deployed application:


Figure 4. Cat list
Generated master screen

Figure 5. Cat form
Generated detail screen

The records you see in the above screenshots are created as part of the code-generation process, so there's data to test against.



Back to top


Reason #6: Documentation

You can find tutorials for each of the flavors of AppFuse, and you can find them in six different languages: Chinese, German, English, Korean, Portuguese, and Spanish. By flavors, I mean the different framework combinations, such as Spring MVC plus iBATIS, Spring MVC plus Hibernate, or JSF plus Hibernate. With five Web frameworks and two persistence frameworks, several combinations are possible. Related to these translations is the fact that AppFuse ships with eight translations for its default features. The available languages include Chinese, Dutch, German, English, French, Italian, Portuguese, and Spanish.

In addition to the core tutorials, many supplemental tutorials (see Resources) have been added to show integration with various databases, application servers, and other open source technologies (including JasperReports, Lucene, Eclipse, Drools, Axis.and DWR).



Back to top


Reason #7: Community

The Apache Software Foundation has an interesting perspective on open source. It's most interested in developing a community around its open source projects. Its members believe that if a community is strong, high-quality code will be a natural progression. To quote the text on the Apache home page:

"We consider ourselves not simply a group of projects sharing a server, but rather a community of developers and users."

The AppFuse community has grown tremendously since it started as a SourceForge project (part of struts.sf.net) in 2003. With the move to java.net in March of 2004, it became a popular project there and was the most accessed project from January through March of 2005. Today it remains a popular project (see Resources for links to java.net's project statistics) but is losing some ground to many of the Sun-sponsored projects on the site.

In late 2004, Nathan Anderson was brought on board as the first committer besides me. Many others have been added since, including Ben Gill, David Carter, Mika Göckel, Sanjiv Jivan, and Thomas Gaudin. All of the existing committers have contributed one way or another, and all have helped to make the AppFuse community a vibrant and fun place to be.

The mailing list is friendly, and we try to maintain the mantra that "no question is a dumb question." The only "RTFM" in our mailing list archives was from a user, not a developer. We definitely believe in the Apache open source philosophy. To quote my good friend Bruce Snyder, "We come for the code and stay for the people." Currently, most developers are users, and we generally like to have a good time. In addition, most of the documentation has been written by the community; therefore, the community is very knowledgeable.



Back to top


Conclusion

You should try developing with AppFuse because it allows you to easily test, integrate, automate, secure, and generate your Web applications. Its documentation is plentiful and its community is friendly. As the frameworks that power AppFuse get better, it will only continue to improve.

With AppFuse 2.0, we plan to move to JDK 5 (while still supporting deploying to 1.4) and Maven 2. These tools will simplify developing, installing, and upgrading with AppFuse. We plan to leverage Maven 2's ability to handle transitive dependencies. You'll encounter artifacts such as appfuse-hibernate-2.0.jar and appfuse-jsf-2.0.jar. These artifacts will be referenced in your pom.xml file and will be responsible for grabbing the rest of the associated dependencies. Rather than having AppFuse's base classes in your project, you'll simply extend the classes in the JARs, as with a normal framework, which should make it a lot easier to upgrade and encourage more users to submit their desired tweaks to the project.

If nothing else, using AppFuse will keep you at the forefront of Java Web development -- like we are!



Resources

Learn

Get products and technologies
  • AppFuse on java.net: Download different flavors of AppFuse.

  • WebWork: Check out this easy-to-use Web framework.

  • DbUnit: Find out more about this JUnit extension.

  • jMock: Create dynamic mock objects to simplify true unit testing.

  • Canoo WebTest: Automate the UI testing of your Web applications.

  • HtmlUnit: The muscle behind WebTest's excellent JavaScript support.

  • Cargo: Automate starting and stopping your container.

  • Greenbox: A code-generation framework.


Discuss


About the author

Matt Raible lives in Denver, Colorado, where he is a Spring and Web frameworks practice leader for Virtuas Open Source Solutions. He has extensive experience and expertise in open source, as both a user and a developer. Matt is the author of Spring Live from SourceBeat Publishing. He has also contributed to the Apress book Pro JSP Third Edition. He is a frequent presenter at open source conferences, including ApacheCon, MySQL User's Conference, and OSCON, and he is an active blogger on http://raibledesigns.com. Raible has been surrounded by computers for most of his life, even though he grew up in the backwoods of Montana without electricity. When he's not working, he's trying to make his wife, Julie, the happiest woman in the world or playing with their children, Abbie and Jack.

分享到:
评论

相关推荐

    appfuse

    使用appfuse2.0,下载过来的实例源码,没有jar包

    使用 AppFuse 快速构建 java

    使用 AppFuse 快速构建 java 使用 AppFuse 快速构建 java

    可直接使用的appfuse项目

    AppFuse是一个集成了众多当前最流行开源框架与工具(包括Hibernate、ibatis、Struts、Spring、DBUnit、Maven、Log4J、Struts Menu、Xdoclet、SiteMesh、OSCache、JUnit、JSTL等(现在还有lucene的,无敌了))于一身的...

    使用AppFuse快速构建J2EE应用

    关于AppFuse的特性、架构以及为什么要使用AppFuse,AppFuse的创始人MattRaible在《使用AppFuse的七个理由》一文中已经做了很详尽的阐述,这里就不再赘言。本文将着力于实践,即如何运用AppFuse开发J2EE应用。使用

    AppFuse

    本文以一个 J2EE 开发者的角度,借助一个简单的应用示例,在融合了个人经验的基础上介绍了如何用 AppFuse 一步步地构建 J2EE 项目。通过阅读本文,读者不仅能够学会用 AppFuse 进行开发,而且能够充分体会到 AppFuse...

    appfuse使用手册

    代码自动生成工具appfuse的中文使用手册

    AppFuse入门文档(AppFuse与SpringMVC+mybatis整合)

    本文档详细描述了AppFuse与SpringMVC+mybatis整合的过程,只要你懂一些基本的eclipse操作和基本的maven命令,就可以在三分钟之内迅速的搭建出一个AppFuse的架构

    APPFUSE工具研究.doc

    Appfuse是一个开源的工程应用,它集成了现在最流行的开发框架到该应用中,使用Maven可以很方便的开发和部署因为。也可以集成到现在流行的开源开发工具如eclipse,idea等。现在让我们简单的看看APPFUSE开发应用的简单...

    appfuse新手上路

    新手使用appfuse2.0,启动步骤和配置过程

    玩转appfuse--使用appfuse建设MVC网站

    使用appfuse进行网站开发,appfuse是关于Spring,Struts2,MVC3,Hibernate等技术的案例,可以加快建设网站。 1.文档说明。 2.可以执行的案例,亲测无误。

    使用appfuse2建立项目原型骨架的步骤

    使用appfuse2建立项目原型骨架的步骤 appfuse2 项目原型 骨架

    appfuse1.4-architecture

    06年时的appfuse,学习SSH架构的经典入门框架。相对比较老的资料,可以欣赏一下当时的架构,向牛人致敬

    建立项目原型骨架的步骤(最新版本appfuse)appfuse2.1.0-M2

    建立项目原型骨架的步骤(最新版本appfuse)appfuse2.1.0-M2 spring3.0 hibernte3.3 struts2.1.8

    appfuse 学习笔记

    Appfuse 一个开放源码的项目和应用程序,帮助我们快速而高效的地开发。

    appfuse开发框架(myapp)使用说明文档

    当使用JDK1.4时,由于JDK1.4没有自带xml解析器,Tomcat启动调用这两个包来解析XML文档,这时没有冲突,但JDK1.5有自xml解析器,Tomcat启动时再来调用这两个包,那将产生冲突,所以,用JDK1.4是要加这两个包,用JDK...

    Appfuse教程Appfuse开发.pdf

    用 户管理、根据不同的用户可以展现不同的菜单,可以自动生成40%-60%左右的代码,自带了默认的一些在CSS中设定的样式,使用这些样式能很快的改变整 个系统的外观,还有自动化测试的功能。  它最大的价值就是为我们...

    Appfuse1.9至2.0.2

    主要是自己从网络上搜集的一些关于appfuse1.8.2-2.0.2的一些相关资料,间或有点自己试验的记录,还有点maven和quartz的东东,之前我主要是用1.8.2构建项目,感觉还不错,希望对想学习appfuse的人有些帮助.

    AppFuse2.1所需包

    AppFuse2.1所需包,主要包括Spring MVC+Hibernate4 还有一些其它包!

Global site tag (gtag.js) - Google Analytics