熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Jpetstore閱讀心得之分層結構

2013-11-23 20:24:23  來源: Java開源技術 

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

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

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

  分層結構

  Jpetstore使用了門面模式單例模式DAO模式

   

  門面模式

  門面接口的實現類 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)

  {

  thisaccountDao = accountDao;

  }

  //省略余下的四個setter

  //

  // Operation methods implementing the PetStoreFacade interface

  //

   

  public Account getAccount(String username)

  {

  return thisaccountDaogetAccount(username);

  }

  public Account getAccount(String username String password)

  {

  return thisaccountDaogetAccount(username password);

  }

  public void insertAccount(Account account)

  {

  thisaccountDaoinsertAccount(account);

  }

  public void updateAccount(Account account)

  {

  thisaccountDaoupdateAccount(account);

  }

  //省略其它的crud方法

  }

   

  暫時先不管 OrderService 這個接口

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

  在配置文件中

  <bean id=petStore class=orgspringframeworksamplesjpetstoredomainlogicPetStoreImpl>

  <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>

   

   單例模式

  單例模式中我們一般把類的構造方法設置為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 perApplication 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)

  {

  supersetServlet(actionServlet);

  if (actionServlet != null)

  {

  ServletContext servletContext = actionServletgetServletContext();

  WebApplicationContext wac = WebApplicationContextUtils

  getRequiredWebApplicationContext(servletContext);

  thispetStore = (PetStoreFacade) wacgetBean(petStore);

  }

  }

   

  protected PetStoreFacade getPetStore()

  {

  return petStore;

  }

  }

   

  DAO模式

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

   

  <select id=getAccountByUsername resultMap=result>

  select

  signonusername as userid

  accountemail

  accountfirstname

  accountlastname

  accountstatus

  accountaddr

  accountaddr

  accountcity

  accountstate

  accountzip

  untry

  accountphone

  profilelangpref

  profilefavcategory

  profilemylistopt

  profilebanneropt

  bannerdatabannername

  from account profile signon bannerdata

  where accountuserid = #value#

  and signonusername = accountuserid

  and profileuserid = accountuserid

  and profilefavcategory = bannerdatafavcategory

  </select>


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28442.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.