隨筆-314  評論-209  文章-0  trackbacks-0

          雖然對 Spring 不熟悉,又不懂 iBatis ,而且對模式的概念還沒有弄清楚,但也硬著頭皮去讀 Spring 包自帶的 Jpetstore 經典 J2EE 例子。

          可以肯定, Jpetstore 是按照 MVC 模式設計的。持久化層用 iBatis (這個我不懂,我希望是用 Hibernate ), web 層控制器的 servlet 有兩個選擇,一個是用 Struts ,另一個是 Spring MVC

          以下是自己的閱讀體會,也許分析不當或描述不清,但也算初步嘗試,所以記下來了。

          一,分層結構

          Jpetstore 使用了門面模式、單例模式, DAO 模式。

          ?

          1. 門面模式

          門面接口的實現類: PetStoreImpl

          public class PetStoreImpl implements PetStoreFacade, OrderService

          {

          ??? private AccountDao accountDao;

          ??? private CategoryDao categoryDao;

          ??? private ProductDao productDao;

          ??? private ItemDao itemDao;

          ??? private OrderDao orderDao;

          ?

          ??? // ----------------------------------------------------------------

          ??? // Setter methods for dependency injection

          ??? // ----------------------------------------------------------------

          ?

          ??? public void setAccountDao(AccountDao accountDao)

          ??? {

          ??????? this.accountDao = accountDao;

          ??? }

          ??? // 省略余下的四個setter

          ??? // -------------------------------------------------------------------------

          ??? // Operation methods, implementing the PetStoreFacade interface

          ??? // -------------------------------------------------------------------------

          ?

          ??? public Account getAccount(String username)

          ??? {

          ??????? return this.accountDao.getAccount(username);

          ??? }

          ??? public Account getAccount(String username, String password)

          ??? {

          ??????? return this.accountDao.getAccount(username, password);

          ??? }

          ??? public void insertAccount(Account account)

          ??? {

          ??????? this.accountDao.insertAccount(account);

          ??? }

          ??? public void updateAccount(Account account)

          ??? {

          ??????? this.accountDao.updateAccount(account);

          ??? }

          ??? // 省略其它的crud方法

          }

          ?

          暫時先不管 OrderService 這個接口。

          PetStoreImpl 的那些 setter 方法正是 spring 的注入方法。

          在配置文件中:

          < bean id ="petStore" class ="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">

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

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

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

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

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

          </ bean >

          ?

          2. 單例模式

          單例模式中,我們一般把類的構造方法設置為 private ,提供靜態工廠方法給外界返回唯一的實例,但在這里,它不是這樣做的,因為有了 Spring 。有了 Spring BeanFactory 管理,可以輕易配置實現單例。看看作者的注釋。

          ?

          There is one instance of this class in the JPetStore application. In Spring terminology, it is a "singleton". This means a per-Application Context singleton. The factory creates a single instance; there is no need for a private constructor, static factory method etc as in the traditional implementation of the Singleton Design Pattern.

          ?

          單例的 PetStoreImpl

          Struts 當控制器時,它這樣做:為整個應用程序編寫一個繼承自 Action BaseAction 基礎類。

          ?

          public abstract class BaseAction extends Action

          {

          ??? private PetStoreFacade petStore;

          ?

          ??? public void setServlet(ActionServlet actionServlet)

          ??? {

          ??????? super.setServlet(actionServlet);

          ??????? if (actionServlet != null)

          ??????? {

          ??????????? ServletContext servletContext = actionServlet.getServletContext();

          ??????????? WebApplicationContext wac = WebApplicationContextUtils

          ??????????????? .getRequiredWebApplicationContext(servletContext);

          ??????????? this.petStore = (PetStoreFacade) wac.getBean("petStore");

          ??????? }

          ??? }

          ?

          ??? protected PetStoreFacade getPetStore()

          ??? {

          ??????? return petStore;

          ??? }

          }

          ?

          3.DAO 模式

          ORM 工具用 iBatis ,在領域模式層使用了粗粒度對象。下面是 AccountDao 的配置。

          ?

          < select id ="getAccountByUsername" resultMap ="result">

          ??? select

          ????????? signon.username as userid,

          ????????? account.email,

          ????????? account.firstname,

          ????????? account.lastname,

          ????????? account.status,

          ????????? account.addr1,

          ????????? account.addr2,

          ????????? account.city,

          ????????? account.state,

          ????????? account.zip,

          ????????? account.country,

          ????????? account.phone,

          ????????? profile.langpref,

          ????????? profile.favcategory,

          ????????? profile.mylistopt,

          ????????? profile.banneropt,

          ? ???????? bannerdata.bannername

          ??? from account, profile, signon, bannerdata

          ??? where account.userid = #value#

          ????? and signon.username = account.userid

          ????? and profile.userid = account.userid

          ????? and profile.favcategory = bannerdata.favcategory

          ? </ select >

          posted on 2006-11-02 09:41 xzc 閱讀(313) 評論(0)  編輯  收藏 所屬分類: Design
          主站蜘蛛池模板: 庆城县| 郎溪县| 酒泉市| 读书| 正安县| 伊金霍洛旗| 和静县| 高州市| 航空| 正蓝旗| 闸北区| 吉安县| 乳山市| 广饶县| 安庆市| 临泉县| 台安县| 琼中| 同德县| 祁阳县| 彭泽县| 循化| 临城县| 巴马| 临颍县| 柞水县| 永泰县| 温宿县| 常山县| 桑日县| 社会| 武平县| 六盘水市| 土默特右旗| 济源市| 开阳县| 宁陕县| 陆良县| 陇西县| 堆龙德庆县| 通道|