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

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

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

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

          一,分層結(jié)構(gòu)

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

          ?

          1. 門面模式

          門面接口的實現(xiàn)類: 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. 單例模式

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

          ?

          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 當控制器時,它這樣做:為整個應(yīng)用程序編寫一個繼承自 Action BaseAction 基礎(chǔ)類。

          ?

          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 ,在領(lǐng)域模式層使用了粗粒度對象。下面是 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
          主站蜘蛛池模板: 皋兰县| 河曲县| 梁河县| 乃东县| 乐陵市| 潼关县| 凌海市| 郎溪县| 阳西县| 岐山县| 庐江县| 含山县| 都昌县| 罗源县| 东明县| 安义县| 抚顺市| 黎城县| 云南省| 隆安县| 辽阳县| 岑巩县| 金华市| 秦安县| 兴宁市| 乌兰浩特市| 兴山县| 康保县| 汤阴县| 龙陵县| 望都县| 监利县| 措勤县| 阿尔山市| 武穴市| 邵阳市| 湖南省| 云南省| 思南县| 崇州市| 射洪县|