问:
原文链接 http://blog.csdn.net/cpp_lzth/article/details/6551703
看AOP的时候发现spring中有个org.springframework.transaction.interceptor.TransactionProxyFactoryBean.还有这里为什么要配置这个类呢???
答:
[xhtml] view plain copy
- <!--设置数据源 -->
- <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>
- <!--设置ibatis -->
- <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>
- <!--设置DAO -->
- <bean id= "VssDao " class= "com.wehave.hyerp.productmanage.persistence.sqlmapdao.VssSqlMapDao ">
- <property name= "sqlMapClient ">
- <ref bean= "sqlMapClient "/>
- </property>
- </bean>
- <!--业务层交给spring进行事务管理 -->
- <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>
问:
这个跟直接把DAO交给控制层有什么区别???
为什么还要用TransactionProxyFactoryBean来包住DAO呢???
TransactionProxyFactoryBean又不是自己写的Manager类,又有何功能实现事务控制呢???
答:
配置这个类,是为了现宽广地实现事务的管理,也可以说是为了要和hibenate结合起来,TransactionProxyFactoryBean这个类一般有三个构造参数要注入,一个是hibernate的sessionFactory,这是为了spring可以和hibernate结合,第二个是用户的DAO类,而这个DAO类本身也需要把hibernate的sessionFactory作为构造参数注入到它的父类的构造函数中。第三个注入的,其实是事务管理的策略,通过AOP的方式实现的,例如DAO类中有有insert(String sql);update(String sql);delelte(String sql);三个方法,我们可以设定当insert执行时如果抛出异常,就执行rollback等操作。楼主对照rickhunterchen(千山鸟飞绝)的回贴,可以体现出我上面所说的东西。我明白这个原理,可是不知能否表达清楚。
时间: 2024-10-13 01:11:52