虽然对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>
闽公网安备 35060202000074号