向你推荐---AppFuse(E文章转载)

类别:Java 点击:0 评论:0 推荐:
AppFuse: Start Your J2EE Web Appsby Matt Raible
07/15/2004

Contents The Birth of AppFuse What Is AppFuse? What Does the Future Hold? AppFuse Light How Do I Start Using It? Resources

One of the hardest parts about J2EE development is getting started. There is an immense amount of open source tools for web app development. Making a decision on which technologies to use can be tough -- actually beginning to use them can be even more difficult. Once you've decided to use Struts and Hibernate, how do you go about implementing them? If you look on the Hibernate site or the Struts site, you'll probably have a hard time finding any information on integrating the two. What if you want to throw Spring into the mix? As a developer, the best way for me to learn is by viewing sample apps and tutorials that explain how to extend those applications. In order for me to learn (and remember) how to integrate open source technologies such as Hibernate, Spring, Struts, and Ant/XDoclet, I created AppFuse. The beauty of AppFuse is you can actually get started with Hibernate, Spring, and Struts without even knowing much about them. Using test-driven development, AppFuse and its tutorials will show you how to develop a J2EE web application quickly and efficiently.

The Birth of AppFuse

In early 2002, I managed to land a contract where I was the lone developer. I was responsible for everything, from gathering requirements to database creation to DHTML on the web front end. While I'd done a lot of front-end work (Struts, HTML, etc.) over the years, the business layer and persistence layer were mostly new to me. So I searched and searched for J2EE patterns to do things the "right way" for my new client. I ended up implementing Business Delegates, DAOs, and ServiceLocators -- many of them modeled after J2EE Blueprints. Towards the end of that contract, I was contracted to help write a book on JSP 2.0. The publisher was Wrox Press and the book was Professional JSP 2.0. I volunteered to write two chapters -- one on Struts and one on web security. Note: in March 2003, Wrox went out of business and this book eventually became Pro JSP, Third Edition, published by Apress.

When you write a technical book, it's helpful to use a sample app to demonstrate concepts. I was tired of reading books that had a throwaway sample app, so I wanted to develop something meaningful; something I could use after I was done writing. My initial thoughts were to write an application that would help you get started with Struts, and would use XDoclet to do a lot of the heavy lifting. It also had to have a lot of security features so I could use it for my chapter on web application security. I came up with the name struts-xdoclet and began developing my application.

After a couple of weeks, I decided that "struts-xdoclet" was too hard to say and renamed the project AppFuse. I developed the sample app for my Struts chapter using AppFuse and released it as Struts Resume. After publishing Struts Resume, I needed to extract the resume-specific stuff out of it to revert back to the generic "jumpstart" application I wanted AppFuse to be. It took me almost three months, but I finally released the first official version of AppFuse in April 2003.

Since its initial release, AppFuse has gone through many changes and improvements. Most of the technology decisions were made from experience. It was a pain to keep my ValueObjects (VOs) and ActionForms in sync, so ActionsForms are generated using XDoclet. I found it tedious to write JDBC and more tedious to update SQL and VOs when adding new table columns. To solve this, I chose to use Hibernate for persistence and use Ant/XDoclet to dynamically create my database tables. Rather than lose my tables' data each time, I integrated DBUnit, which also came in handy when running unit tests. I decided to use Tomcat/MySQL as the default server/database setup because I was most familiar with them. I integrated a plethora of unit/integration tests so I could make sweeping changes and verify that everything still worked (including the JSPs) by running ant test-all.

In March of 2004, I moved the AppFuse project from the Struts project on SourceForge to java.net. I had a couple of reasons for doing this. First of all, it was a sub-project on SourceForge, and its user base was growing quickly. It needed dedicated mailing lists and forums. Secondly, java.net seemed to do a better job of marketing projects and I'd heard their CVS system was much more stable. My experience at java.net has been quite nice: it's a very stable system, and the administrators are very supportive of the project and have even helped with marketing it.

What is AppFuse?

So after all that history, what is AppFuse? At its very core, AppFuse is a web application that you can package into a .war and deploy to a J2EE 1.3-compliant app server. It's designed to help you create new web applications using a new target in its build.xml file. The new target allows you to specify a name for your project and a name for the database it will talk to. Once you've created a project, you can instantly create a MySQL database and deploy it to Tomcat using ant setup. Furthermore, you can verify that the basic functionality of your new application works by running ant test-all. At this point, you might sneer and say, "What's the big deal? Anyone can create a .war file and deploy it to Tomcat." I agree, but do you have a setup-tomcat target that will configure Tomcat with JNDI resources for connections pooling and mail services? Most of what AppFuse does is not rocket science. In reality, it's nothing more than a directory structure, a build file, and a bunch of base classes -- with a few features thrown in. However, it has vastly accelerated my ability to start projects and develop high-quality, well-tested web applications.

AppFuse tries to make it as simple as possible to build, test, and deploy your application. It virtually eliminates setup and configuration, which are often the hard parts. Tools like Ant, JUnit, XDoclet, Hibernate, and Spring can be difficult to get started with. Furthermore, features like authentication, password hints, "remember me," user registration, and user management are things that most web apps need. AppFuse ships with tutorials for developing DAOs, business delegates, Struts actions (or Spring controllers), integrating tiles and validation, and uses an Ant-based XDoclet task (written by Erik Hatcher) to generate master/detail JSPs from model objects. It uses slick open source tag libraries like Struts Menu (for navigation) and the Display Tag (for paging and sorting lists).

One of the best parts, in my opinion, is that it embraces the Java community's ideas and suggestions. The directory structure and build file are largely based on Erik Hatcher and Steve Loughran's excellent Java Development with Ant book. In this book, Erik built a sample application that inspired me to learn more about Ant and XDoclet -- and use it in my Struts development.

When I first started learning and using Hibernate in AppFuse, I made many mistakes -- and the community let me know. At first, I opened and closed its Session object for each DAO method. When Gavin told me this was a bad idea, I made modifications to use an OpenSessionInView pattern, with my own ServletFilter to do the work. I passed the session object into each method signature, for which the community repeatedly questioned my logic. My answer was, "I wanted to get it working more than anything -- do you have a better idea?" The better idea turned out to be using the Session as a constructor argument, which worked pretty well.

Then, late last year, I discovered the Spring framework and found the beautiful solution I'd been looking for. Using its ORM support, I was able to eliminate any Session handling in AppFuse; now Spring elegantly handles it all. AppFuse now uses Spring's OpenSessionInViewFilter. All I needed to do was configure it in web.xml and it manages opening and closing the session for me. When I integrated Spring in AppFuse's persistence layer, I deleted two or three classes and reduced my LOC count by around 75 percent. All of the Hibernate issues I'd had before disappeared! In addition, I was quickly able to add a DAO implementation using iBATIS, which I worked with on a project last year. On that project, I discovered that iBATIS was easy to use and worked very well for interacting with complex database schemas.

AppFuse is not only a jumpstart kit for your web apps; it's also a showcase for integrating technologies like Hibernate, Spring, and Struts. Tutorials exist for integrating these different open source components, but rarely do they give you an application you can walk away with and use to develop your next application. In a sense, AppFuse is a glue that binds open source projects together. When I found Spring, it was a perfect fit, since it was the glue to configure components and loosely couple the different layers of an application. Erik's book might have been the match that lit AppFuse, but Spring is the gasoline that really got it roaring. Spring has vastly simplified how I develop with AppFuse and forced me to follow best practices in J2EE. In short, it's the best tool I've ever used with J2EE. I realize Spring is not the be-all-end-all for J2EE applications -- AppFuse worked fine before I integrated it. However, it helped answer all of my "How should I do ..." questions -- which was a nice relief.

What Does the Future Hold?

AppFuse 1.5 was released at the end of May. Its major feature is the option to use Spring MVC instead of Struts. All of the tutorials have been updated to educate users how to use Spring (or Struts) for their web layers. For AppFuse 1.6, I plan to add support for using WebWork and SiteMesh, as well as make it easier to run unit tests from Eclipse and IDEA. By the end of the year, I hope to add Tapestry and JSF as MVC options. The middle tier and back end (Spring plus Hibernate) probably won't change much. It's a well-oiled machine at this point. This isn't to say that I think AppFuse is perfect. I hope I continue to receive valuable feedback and suggestions. After all, this is the driving force behind improving its quality.

When I find innovative and easier ways to develop web apps, it ends up in AppFuse. People ask me why AppFuse doesn't support Middlegen. The reason is simple: I've never used or needed Middlegen on a project, so I don't see any reason to add it. If I end up on a project where I use Middlegen, you can bet I'll end up adding support for it. Recently, I started using Spring's MVC framework instead of Struts. At first, I found it a bit difficult to understand, so I implemented the web layer with Spring MVC. Since the web layer contains many features that I use in web apps (i.e., templating, validation, file upload, and exception handling), I learned the ins and outs of Spring MVC by integrating it.

Using and learning AppFuse is a good way to keep up to date with the latest releases of its dependent projects. I try to keep as up to date as possible with new releases -- and due to AppFuse's unit test coverage, I can easily test new releases by dropping them in and running ant test-all. This even works for dependent resources such as Tomcat and MySQL, in which I've found bugs via AppFuse. For me, it acts as a testing tool to verify the functionality of new open source releases.

AppFuse Light

Equinox is a lightweight version of AppFuse; hence its pseudonym "AppFuse Light." I was inspired to create it when looking at the struts-blank and webapp-minimal applications that ship with Struts and Spring, respectively. These "starter" apps were not robust enough for me, and I wanted something like AppFuse, only simpler -- with no build-time dependencies.

It's designed to show web app developers how to start a bare-bones Struts/Spring/Hibernate app using the QuickStart Chapter in Spring Live. More functionality may be added in the future, but the main goal is to provide a better implementation of a bare-bones web app.

Equinox should mostly be used for writing tutorials and rapid prototyping. I don't recommend using it for starting a full-fledged production application. That's what AppFuse is for.

How Do I Start Using It?

The best way to get started using AppFuse is to download it and read the QuickStart Guide. AppFuse is a part of the Java Enterprise Community on java.net. Its project homepage is at appfuse.dev.java.net.

If you're interested in Equinox, you can read the QuickStart Chapter from Spring Live to build a quick and easy application using Struts, Spring, and Hibernate. If you like, you can view a demo of the MyUsers application that this chapter helps you build. Equinox is a sub-project of AppFuse on java.net. Its project homepage is at equinox.dev.java.net.

Resources AppFuse Project: appfuse.dev.java.net, Wiki Page and Demo Support: FAQ and Mailing List How-Tos: Tutorials, Using Resin, Using OracleAS, Using DB2, and Using Velocity instead of JSPs. Similar Projects: Java Application Generator and Megg

Matt Raible is currently authoring Spring Live and is a member of the J2EE 1.5 Expert Group. He blogs about J2EE and his experiences on both jroller.com and raibledesigns.com.

本文地址:http://com.8s8s.com/it/it12613.htm