問:html
原文連接
http://blog.csdn.net/cpp_lzth/article/details/6551703
看AOP的時候發現spring中有個org.springframework.transaction.interceptor.TransactionProxyFactoryBean.還有這裏爲何要配置這個類呢???java
答:spring
- <bean id= "dataSource " class= "org.springframework.jndi.JndiObjectFactoryBean ">
- <property name= "jndiName ">
- <value> <a href="http://lib.csdn.net/base/javase" class='replace_word' title="Java SE知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>:comp/env/jdbc/ayoa </value>
- </property>
- </bean>
-
- <bean id= "sqlMapClient " class= "org.springframework.orm.ibatis.SqlMapClientFactoryBean ">
- <property name= "configLocation ">
- <value> classpath:com/ayoa/bean/sqlmapdao/sql-map-config.xml </value>
- </property>
- <property name= "dataSource ">
- <ref bean= "dataSource "/>
- </property>
- </bean>
-
- <bean id= "TransactionManager " class= "org.springframework.jdbc.datasource.DataSourceTransactionManager ">
- <property name= "dataSource ">
- <ref bean= "dataSource "/>
- </property>
- </bean>
-
- <bean id= "VssDao " class= "com.wehave.hyerp.productmanage.persistence.sqlmapdao.VssSqlMapDao ">
- <property name= "sqlMapClient ">
- <ref bean= "sqlMapClient "/>
- </property>
- </bean>
-
- <bean id= "VssService " class= "org.springframework.transaction.interceptor.TransactionProxyFactoryBean ">
- <property name= "transactionManager ">
- <ref bean= "TransactionManager "> </ref>
- </property>
- <property name= "target ">
- <bean class= "com.wehave.hyerp.productmanage.service.VssService ">
- <property name= "vssDao ">
- <ref bean= "VssDao "/>
- </property>
- </bean>
- </property>
-
- <property name= "transactionAttributes ">
- <props>
- <prop key= "insertVss "> PROPAGATION_REQUIRED </prop>
- </props>
- </property>
- </bean>
問:sql
這個跟直接把DAO交給控制層有什麼區別???
爲何還要用TransactionProxyFactoryBean來包住DAO呢???
TransactionProxyFactoryBean又不是本身寫的Manager類,又有何功能實現事務控制呢???session
答:函數
配置這個類,是爲了現寬廣地實現事務的管理,也能夠說是爲了要和hibenate結合起來,TransactionProxyFactoryBean這個類通常有三個構造參數要注入,一個是hibernate的sessionFactory,這是爲了spring能夠和hibernate結合,第二個是用戶的DAO類,而這個DAO類自己也須要把hibernate的sessionFactory做爲構造參數注入到它的父類的構造函數中。第三個注入的,實際上是事務管理的策略,經過AOP的方式實現的,例如DAO類中有有insert(String sql);update(String sql);delelte(String sql);三個方法,咱們能夠設定當insert執行時若是拋出異常,就執行rollback等操做。樓主對照rickhunterchen(千山鳥飛絕)的回貼,能夠體現出我上面所說的東西。我明白這個原理,但是不知可否表達清楚。spa