虽然对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> |