??Spring JPetStore總體架構(gòu)屬于三層結(jié)構(gòu). 分為業(yè)務(wù)服務(wù)層? 表現(xiàn)層? 數(shù)據(jù)訪問(wèn)層
??
?????? 業(yè)務(wù)服務(wù)層使用由POJO(java類)實(shí)現(xiàn), 它們運(yùn)行在輕量級(jí)的容器Spring中. 輕量級(jí)容器的功能在于,?一 管理業(yè)務(wù)對(duì)象的生命周期. 二 借助于Spring?的IOC(Inversion of Control, 控制反轉(zhuǎn))功能來(lái)完成對(duì)象之間的依賴關(guān)系, 而不用編程一顯示完成. 三 借助于Spring的AOP(Aspect-Oriented Programming, 面向方面的程序設(shè)計(jì))?為運(yùn)行在容器中的對(duì)象提供一些企業(yè)服務(wù), 比如聲明式的事務(wù)管理. 業(yè)務(wù)層放棄了EJB而使用運(yùn)行在輕量級(jí)容器的服務(wù)層, 會(huì)有以下好處:
1.? 能夠在Servlet引擎中運(yùn)行. 不用EJB容器的服務(wù)器, tomcat就能搞定, 軟件的費(fèi)用低, 容易管理, 負(fù)載小.
2?. 容易在不同的應(yīng)用服務(wù)器或serverlet引擎之間移植. 要想達(dá)到高可移植性, 對(duì)于EJB容器來(lái)說(shuō)要比web容器困難, 比如, 就需要確保不同的EJB容器在啟動(dòng)時(shí)都會(huì)運(yùn)行某一些代碼.
3.??實(shí)現(xiàn)更為簡(jiǎn)單.
4. 不需要那么累贅的部署文件.
不好的地方:
1.? 缺乏對(duì)遠(yuǎn)程調(diào)用的內(nèi)置支持.
2.? 缺乏一個(gè)標(biāo)準(zhǔn)的環(huán)境, 用于容納, 管理業(yè)務(wù)對(duì)象.
3.? 沒(méi)有清晰的業(yè)務(wù)層.
4.? 在不同的應(yīng)用系統(tǒng)之間缺乏一致性, 第個(gè)系統(tǒng)都可能會(huì)有自己的一套做法: 怎么訪問(wèn)業(yè)務(wù)對(duì)象?怎么解決事務(wù)管理 怎么訪問(wèn)數(shù)據(jù)等.
雖然如此但現(xiàn)在我們有了spring一切就有了解決之道了.
????? 表現(xiàn)層中, Spring JPetStore提供了兩種不同的web層實(shí)現(xiàn), 二者都要依靠同一個(gè)中間層, 一個(gè)是基于Structs的,另一個(gè)則是Spring的MVC框架. 二者都是基于JSTL的JSP視圖.
????? 數(shù)據(jù)訪問(wèn)層,使用了J2EE模式中的"數(shù)據(jù)訪問(wèn)對(duì)象"(Data Access Object, DAO),?他用一個(gè)DAO接口隱藏了持久化操作的細(xì)節(jié), 這樣使用這個(gè)模式的業(yè)務(wù)對(duì)象無(wú)需知道底層的持久化技術(shù)的細(xì)節(jié). Spring JPetStore中使用了iBATIS框架.
下面讓我們來(lái)看一下應(yīng)用中的細(xì)節(jié)內(nèi)容吧, 先在Eclipse下把應(yīng)用加進(jìn)來(lái)方便調(diào)試, 運(yùn)行.
Eclipse可到(http://www.eclipse.org/downloads/上下載)他是壓縮包解壓后就可用(可以再去下MyEclipe http://www.myeclipse.com是Eclipese的開(kāi)發(fā)插件方便開(kāi)發(fā), 不過(guò)要付費(fèi)可以下載它的破解文件http://jinxinxin.bokee.com/inc/myeclipse_keygens.rar?MyEclipse的安裝如果不會(huì)去Google一下吧). 好了開(kāi)發(fā)工具安裝后新建一工程jpetstore, 然后用..\spring-jpetstore\samples\jpetstore下的src目錄復(fù)蓋你的eclipse工作空間下的\jpetstore下的src目錄,然后再把..\spring-jpetstore\samples\jpetstore下的war目錄下的全部?jī)?nèi)容拷到你的eclipse工作空間下的\jpetstore下的WebRoot目錄下復(fù)蓋WEB-INF.回到eclipse下刷新工程你可看到如下:
先從數(shù)據(jù)訪問(wèn)層說(shuō)起吧, 他使用iBATIS框架來(lái)訪問(wèn)數(shù)據(jù)庫(kù),在..\spring-jpetstore\samples\jpetstore\db目錄下有它的各種數(shù)據(jù)庫(kù)的schema有hsql, mysql, oracle, postges你使用拿一種數(shù)據(jù)庫(kù)是通過(guò)..\spring-jpetstore\samples\jpetstore\war\WEB-INF下的jdbc.properties來(lái)配置的.默認(rèn)的是hsql數(shù)據(jù)庫(kù):
# Properties file with JDBC-related settings.
# Applied by PropertyPlaceholderConfigurer from "dataAccessContext-local.xml".
# Targeted at system administrators, to avoid touching the context XML files.
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://localhost:9002
jdbc.username=sa
jdbc.password=
要改為用mysql只要改為:
#jdbc.driverClassName=org.hsqldb.jdbcDriver
#jdbc.url=jdbc:hsqldb:hsql://localhost:9002
#jdbc.username=sa
#jdbc.password=
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/jpetstore
jdbc.username=root
jdbc.password=
即可.業(yè)務(wù)層能過(guò)...jpetstore.dao包下的接口來(lái)訪問(wèn)....jpetstore.dao.ibatis包從而通過(guò)iBATIS框架訪問(wèn)數(shù)據(jù)庫(kù)中的數(shù)據(jù)的部份代碼如下:
package org.springframework.samples.jpetstore.dao;
public interface AccountDao {
? Account getAccount(String username, String password) throws DataAccessException;
? ...........
}
package org.springframework.samples.jpetstore.dao.ibatis;
public class SqlMapAccountDao extends SqlMapDaoSupport implements AccountDao {
??? public Account getAccount(String username, String password) throws DataAccessException {
??? Account account = new Account();
??? account.setUsername(username);
??? account.setPassword(password);
??? return (Account) getSqlMapTemplate().executeQueryForObject("getAccountByUsernameAndPassword", account);
? }
..............
}
Account.xml
<mapped-statement name="getAccountByUsernameAndPassword" result-map="result">
??? select
????? SIGNON.USERNAME as USERID,
????? ACCOUNT.EMAIL,
????? ACCOUNT.FIRSTNAME,
????? ACCOUNT.LASTNAME,
????? ACCOUNT.STATUS,
????? ACCOUNT.ADDR1,
????? ACCOUNT.ADDR2,
????? ACCOUNT.CITY,
??????..???
??? from ACCOUNT, PROFILE, SIGNON, BANNERDATA
??? where ACCOUNT.USERID = #username#
????? and SIGNON.PASSWORD = #password#
????? and SIGNON.USERNAME = ACCOUNT.USERID
????? and PROFILE.USERID = ACCOUNT.USERID
????? and PROFILE.FAVCATEGORY = BANNERDATA.FAVCATEGORY
? </mapped-statement>
然后到了業(yè)務(wù)層, 業(yè)務(wù)層為表達(dá)層提供服務(wù),操縱數(shù)據(jù)層來(lái)完成業(yè)務(wù)邏輯比如從數(shù)據(jù)庫(kù)中讀出客啟信息傳給表達(dá)層 向數(shù)據(jù)庫(kù)中插入訂單等.業(yè)務(wù)層還要完成對(duì)數(shù)據(jù)庫(kù)操作的完整性,正確性即事務(wù)管理. 此應(yīng)用是通過(guò)Spring的AOP來(lái)完成無(wú)需編程實(shí)現(xiàn)如下:
dataAccessContex-local.xml
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
??<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
??<property name="url"><value>${jdbc.url}</value></property>
??<property name="username"><value>${jdbc.username}</value></property>
??<property name="password"><value>${jdbc.password}</value></property>
?</bean>
?<!-- Transaction manager for a single JDBC DataSource -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
? <property name="dataSource">
?? <ref local="dataSource" />
? </property>
? <property name="mappingResources">
?? <list>
??? <value>org/springframework/samples/jpetstore/Hibernate/Account.hbm.xml</value>
?? </list>
? </property>
? <property name="hibernateProperties">
?? <props>
??? <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
??? <prop key="hibernate.show_sql">true</prop>
?? </props>
? </property>
?</bean>
?
?<!-- (see dataAccessContext-jta.xml for an alternative) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
? <property name="sessionFactory">
?? <ref local="sessionFactory" />
? </property>
?</bean>
?<!-- SqlMap setup for iBATIS Database Layer -->
?<bean id="sqlMap" class="org.springframework.orm.ibatis.SqlMapFactoryBean">
??<property name="configLocation"><value>WEB-INF/sql-map-config.xml</value></property>
?</bean>
?<!-- ========================= DAO DEFINITIONS: IBATIS IMPLEMENTATIONS ========================= -->
?<bean id="accountDao" class="org.springframework.samples.jpetstore.dao.hibdaoimp.AccountDaoImp">
? <property name="sessionFactory">
?? <ref local="sessionFactory" />
? </property>
? ?
?</bean>
................
</beans>
applicationContex.xml
<bean id="petStoreTarget" class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl">
??<property name="accountDao"><ref bean="accountDao"/></property>
??<property name="categoryDao"><ref bean="categoryDao"/></property>
??<property name="productDao"><ref bean="productDao"/></property>
??<property name="itemDao"><ref bean="itemDao"/></property>
??<property name="orderDao"><ref bean="orderDao"/></property>
?</bean>
?<!-- Transactional proxy for the JPetStore primary business object -->
?<bean id="petStore" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
??<property name="transactionManager"><ref bean="transactionManager"/></property>
??<property name="target"><ref local="petStoreTarget"/></property>
??<property name="transactionAttributes">
???<props>
????<prop key="insert*">PROPAGATION_REQUIRED</prop>
????<prop key="update*">PROPAGATION_REQUIRED</prop>
????<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
???</props>
??</property>
?
從上面的配置代碼我們可看到通過(guò)Spring的IOC完成了org.springframework.samples.jpetstore.domain.logic.PetStoreImpl等類的注入. 通過(guò)AOP用org.springframework.transaction.interceptor.TransactionProxyFactoryBean來(lái)完成事務(wù)代理
業(yè)務(wù)層通過(guò)一個(gè)門(mén)面(facada)PetStoreFacade.java接口來(lái)為表達(dá)層提供服務(wù):
package org.springframework.samples.jpetstore.domain.logic;
public interface PetStoreFacade {
?Account getAccount(String username);
?Account getAccount(String username, String password);
?void insertAccount(Account account);
?void updateAccount(Account account);
..........
}
用PetStoreImp.java來(lái)實(shí)現(xiàn):
package org.springframework.samples.jpetstore.domain.logic;
public class PetStoreImpl implements PetStoreFacade, OrderService {
? private AccountDao accountDao;
?.........
?public void setAccountDao(AccountDao accountDao) {
??this.accountDao = accountDao;
?}
?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);
? }
? .......
}
表達(dá)層這里介紹Structs,Structs中通過(guò)PetStoreFacade接口來(lái)訪問(wèn)業(yè)務(wù)層:
package org.springframework.samples.jpetstore.web.struts;
public abstract class BaseAction extends Action {
? private
PetStoreFacade petStore;
?public void setServlet(ActionServlet actionServlet) {
??super.setServlet(actionServlet);
??ServletContext servletContext = actionServlet.getServletContext();
??WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
??this.petStore = (PetStoreFacade) wac.getBean("petStore");
?}
?protected PetStoreFacade getPetStore() {
??return petStore;
?}
}
public class SignonAction extends BaseAction {
? public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
???????AccountActionForm acctForm = (AccountActionForm) form;
????? String username = acctForm.getUsername();
????? String password = acctForm.getPassword();
????? Account account = getPetStore().getAccount(username, password);
.....
??????????????? return mapping.findForward("success");
????}
Structs框架通過(guò)struct-config.xml文來(lái)控制相關(guān)映射轉(zhuǎn)發(fā)的:????
struct-config.xml???????
<action path="/signon" type="org.springframework.samples.jpetstore.web.struts.SignonAction"
???name="accountForm" scope="request"
???validate="false">
???<forward name="success" path="/index.jsp"/>
??</action>
關(guān)于jsp就不在多說(shuō)了下面再來(lái)看一下它的web.xml文件:
??<context-param>
??<param-name>contextConfigLocation</param-name>
??<param-value>
???/WEB-INF/dataAccessContext-local.xml? /WEB-INF/applicationContext.xml
??</param-value>
???</context-param>
<servlet>
??<servlet-name>context</servlet-name>
??<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
??<load-on-startup>1</load-on-startup>
?</servlet>
?<servlet>
??<servlet-name>action</servlet-name>
??<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
??<load-on-startup>3</load-on-startup>
?</servlet>
??
??<servlet-mapping>
???<servlet-name>action</servlet-name>
??
??<url-pattern>*.do</url-pattern>
?</servlet-mapping>
?
還有關(guān)于遠(yuǎn)程機(jī)制有Caucho的Hessian(一個(gè)借助HTTP的二進(jìn)制協(xié)議) Burlap(一個(gè)基于XML的借助HTTP的傳輸協(xié)議), Apache Axis提供的JAX-RPC(基于SOAP的借助HTTP傳輸?shù)膚eb serverice), 還有基于RMI的遠(yuǎn)程調(diào)用.
想了解更多還是自已細(xì)看里面的代碼吧!
請(qǐng)繼續(xù)關(guān)注Spring JPetStore(三) 在其上實(shí)現(xiàn)自已的Sunlight Netstore