關于spring+hibernate+struts架構service層的作
系統
1737 0
07年11月15日
????????? 我目前做的項目是采用struts+hibernate+spring框架來做的,這個架構是支持hibernate延遲的,即lazy="true";
????????? 一般采用這個架構都要配service層,就不再嘮叨了.關鍵是看一個配置文件能說明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層配置了事務,由spring管理.struts的action里調用的時候,一般都調用service層封裝的方法.因為service層的方法可能會調用多個DAO層的方法.
?
舉個例子說明一下,假入說我有一個需求,就是銀行的轉帳需求吧,當A給B匯款1000元這個過程,A的帳戶里應該減少1000元,B的帳戶應該增加1000元,而這個過程是由2個DAO方法實現的,即A減少的方法,B增加的方法.
而service層的轉帳方法(起名叫getResult方法)里面先調用了DAO層A減少的方法,再調用B增加的方法.配置文件的事務配置的是管理service層的事務,只有service層的轉帳方法完成了DAO層的兩個方法,才算這個事務結束.
如果直接調用DAO層的方法,那么spring不會管理事務,每次調用DAO的方法就算單獨的一個事務,如果程序運行不穩定,A減少1000元的方法成功,B增加1000元的方法失敗的情況下,由于此時的事務不是同一個事務.那么A的事務不會回滾.....結果就是數據庫里面A少了1000塊錢,B沒有增加1000塊錢........錢那里去了........出大錯了吧,這個錯誤夠嚴重了吧!
關于spring+hibernate+struts架構service層的作用
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元