2008-07-11
让Spring架构减化事务配置(2)
(2) 使用TransactionProxyFactoryBean
<!—定义业务对象--> <bean id="com.prs.application.ehld.sample.biz.service.sampleService.target" class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl"> <property name="userInfoDAO" ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO"> </property> </bean>
<!—定义业务对象的事务代理对象--> <bean id="com.prs.application.ehld.sample.biz.service.sampleService"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="target" ref="com.prs.application.ehld.sample.biz.service.sampleService.target" /> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop> </props> </property> </bean>
使用TransactionProxyFactoryBean需要为每一个代理对象都去定义自己的事务属性。
3) 使用TransactionProxyFactoryBean及abstract属性来简化配置
这种方工也是目前使用得最多的一种声明式事务配置方法
<!--事务控制代理抽象定义 --> <bean id="baseTransactionProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> <prop key="update*">PROPAGATION_REQUIRED</prop> <prop key="save*">PROPAGATION_REQUIRED</prop> <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop> <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop> </props> </property> </bean>
<!—定义业务对象--> <bean id="com.prs.application.ehld.sample.biz.service.sampleService.target" class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl"> <property name="userInfoDAO" ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO"> </property> </bean>
<!—定义业务对象的事务代理对象--> <bean id="com.prs.application.ehld.sample.biz.service.sampleService"parent="baseTransactionProxy"> <property name="target" ref="com.prs.application.ehld.sample.biz.service.sampleService.target"> </property> </bean>
使用abstract属性,可以让代理对象可以共享一个定义好的事务属性,使配置简化。







评论排行榜