關(guān)于spring+hibernate+struts架構(gòu)service層的作
系統(tǒng)
2221 0
07年11月15日
????????? 我目前做的項(xiàng)目是采用struts+hibernate+spring框架來做的,這個(gè)架構(gòu)是支持hibernate延遲的,即lazy="true";
????????? 一般采用這個(gè)架構(gòu)都要配service層,就不再嘮叨了.關(guān)鍵是看一個(gè)配置文件能說明service層的作用.看看配置文件
?
<?
xml?version="1.0"?encoding="UTF-8"
?>
<!
DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd"
>
<
beans
>
????
<
bean?
id
="channleManagerServiceTarget"
?class
="com.cms.channle.service.impl.ChannleManager"
?singleton
="true"
>
????????
<
property?
name
="tchannleDAO"
>
????????????
<
ref?
local
="tchannleDAO"
?
/>
????????
</
property
>
????
</
bean
>
????
<
bean?
id
="channleManageService"
?class
="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
>
????????
<
property?
name
="transactionManager"
>
????????????
<
ref?
bean
="transactionManager"
?
/>
????????
</
property
>
????????
<
property?
name
="target"
>
????????????
<
ref?
local
="channleManagerServiceTarget"
?
/>
????????
</
property
>
????????
<
property?
name
="transactionAttributes"
>
????????????
<
props
>
????????????????
<
prop?
key
="list*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
????????????????
<
prop?
key
="add*"
>
PROPAGATION_REQUIRED
</
prop
>
????????????????
<
prop?
key
="delete*"
>
PROPAGATION_REQUIRED
</
prop
>
????????????????
<
prop?
key
="del*"
>
PROPAGATION_REQUIRED
</
prop
>
????????????????
<
prop?
key
="save*"
>
PROPAGATION_REQUIRED
</
prop
>
????????????????
<
prop?
key
="get*"
>
PROPAGATION_REQUIRED,readOnly
</
prop
>
????????????????
<
prop?
key
="update*"
>
PROPAGATION_REQUIRED
</
prop
>
????????????????
<
prop?
key
="edit*"
>
PROPAGATION_REQUIRED
</
prop
>
????????????
</
props
>
????????
</
property
>
????????
<
property?
name
="postInterceptors"
>
????????????
<
list
>
????????????????
<
ref?
bean
="hibernateInterceptor"
></
ref
>
????????????
</
list
>
????????
</
property
>
????
</
bean
>
????
<
bean?
id
="tchannleDAO"
?class
="com.cms.channle.dao.impl.TchannleDAO"
>
????????
<
property?
name
="sessionFactory"
>
????????????
<
ref?
bean
="sessionFactory"
?
/>
????????
</
property
>
????
</
bean
>
????
</
beans
>
上面代碼中<property name="transactionAttributes"></property >之間是對service層配置了事務(wù),由spring管理.struts的action里調(diào)用的時(shí)候,一般都調(diào)用service層封裝的方法.因?yàn)閟ervice層的方法可能會調(diào)用多個(gè)DAO層的方法.
?
舉個(gè)例子說明一下,假入說我有一個(gè)需求,就是銀行的轉(zhuǎn)帳需求吧,當(dāng)A給B匯款1000元這個(gè)過程,A的帳戶里應(yīng)該減少1000元,B的帳戶應(yīng)該增加1000元,而這個(gè)過程是由2個(gè)DAO方法實(shí)現(xiàn)的,即A減少的方法,B增加的方法.
而service層的轉(zhuǎn)帳方法(起名叫g(shù)etResult方法)里面先調(diào)用了DAO層A減少的方法,再調(diào)用B增加的方法.配置文件的事務(wù)配置的是管理service層的事務(wù),只有service層的轉(zhuǎn)帳方法完成了DAO層的兩個(gè)方法,才算這個(gè)事務(wù)結(jié)束.
如果直接調(diào)用DAO層的方法,那么spring不會管理事務(wù),每次調(diào)用DAO的方法就算單獨(dú)的一個(gè)事務(wù),如果程序運(yùn)行不穩(wěn)定,A減少1000元的方法成功,B增加1000元的方法失敗的情況下,由于此時(shí)的事務(wù)不是同一個(gè)事務(wù).那么A的事務(wù)不會回滾.....結(jié)果就是數(shù)據(jù)庫里面A少了1000塊錢,B沒有增加1000塊錢........錢那里去了........出大錯(cuò)了吧,這個(gè)錯(cuò)誤夠嚴(yán)重了吧!
關(guān)于spring+hibernate+struts架構(gòu)service層的作用
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元