spring配置文件application.xml中的事务总结
2010-02-02 11:36
453 查看
spring配置文件中几种配置事务的方式:
第一种:通过aop管理用户的增删改 (aspectJ)
<aop:config>
<aop:advisor pointcut="execution(* *..OrderDao.*(..))"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
第二种:通过拦截器
Dao实现类实例
<bean name="dao" class="org.itfuture.www.dao.impl.OrderDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
事务bean
<bean id="baseproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="dao"/>//所在拦截的目标指向
<property name="transactionAttributes">//拦截目标内的哪些方法
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
创建orderservice子类(实现类)的实例,注入 orderDao的实现类orderimpl的实例
<bean name="orderservice"
class="org.itfuture.www.service.impl.OrderServiceImpl">
<property name="dao" ref="baseproxy"></property>//引用baseproxy代理
</bean>
第三种配置:aop与interceptor相接合
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager">//引用transactionManager
</property>
<property name="transactionAttributes">//定义哪些方法是要用到事务的,
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="auto" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
第四种配置:个人认为比较好用这种
<bean id="dao" class="org.itfuture.www.dao">//把所有的*dao上升到接口
<property name="accountDao" ref="accountDao"/>
<property name="categoryDao" ref="categoryDao"/>
<property name="productDao" ref="productDao"/>
<property name="itemDao" ref="itemDao"/>
<property name="orderDao" ref="orderDao"/>
</bean>
<aop:config>
<aop:advisor pointcut="execution(* *..interfaceDao.*(..))" advice-ref="txAdvice"/> //然后在拦截接口
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*"/>//拦截接口中的以insert、update、update开头的所有方法
<tx:method name="update*"/>
<tx:method name="*" read-only="true"/>//其它方法,只read-only
</tx:attributes>
</tx:advice>
第一种:通过aop管理用户的增删改 (aspectJ)
<aop:config>
<aop:advisor pointcut="execution(* *..OrderDao.*(..))"
advice-ref="txAdvice" />
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="save*" />
<tx:method name="update*" />
<tx:method name="delete*" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
第二种:通过拦截器
Dao实现类实例
<bean name="dao" class="org.itfuture.www.dao.impl.OrderDaoImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
事务bean
<bean id="baseproxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
<property name="target" ref="dao"/>//所在拦截的目标指向
<property name="transactionAttributes">//拦截目标内的哪些方法
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="select*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
创建orderservice子类(实现类)的实例,注入 orderDao的实现类orderimpl的实例
<bean name="orderservice"
class="org.itfuture.www.service.impl.OrderServiceImpl">
<property name="dao" ref="baseproxy"></property>//引用baseproxy代理
</bean>
第三种配置:aop与interceptor相接合
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
<property name="transactionManager" ref="transactionManager">//引用transactionManager
</property>
<property name="transactionAttributes">//定义哪些方法是要用到事务的,
<props>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="add*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<bean id="auto" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
</list>
</property>
</bean>
第四种配置:个人认为比较好用这种
<bean id="dao" class="org.itfuture.www.dao">//把所有的*dao上升到接口
<property name="accountDao" ref="accountDao"/>
<property name="categoryDao" ref="categoryDao"/>
<property name="productDao" ref="productDao"/>
<property name="itemDao" ref="itemDao"/>
<property name="orderDao" ref="orderDao"/>
</bean>
<aop:config>
<aop:advisor pointcut="execution(* *..interfaceDao.*(..))" advice-ref="txAdvice"/> //然后在拦截接口
</aop:config>
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="insert*"/>//拦截接口中的以insert、update、update开头的所有方法
<tx:method name="update*"/>
<tx:method name="*" read-only="true"/>//其它方法,只read-only
</tx:attributes>
</tx:advice>
相关文章推荐
- spring配置文件application.xml中的事务总结
- spring配置文件application.xml中的事务总结
- spring配置文件applicationContext.xml中的事务总结
- Spring配置文件applicationContext.xml(3)开启事务和注解
- SSH整合-关于Spring中applicationContext.xml文件对于事务的一些配置
- Spring配置文件总结-applicationContext.xml
- Spring配置文件总结-applicationContext.xml
- 详解Spring的applicationContext.xml配置文件
- 【SpringMVC之配置文件(一)】spring 里面的ApplicationContext.xml 和 xxx-servlet.xml分别的作用是什么?
- 最简单的Spring配置文件模板——applicationContext.xml OR beans.xml
- Spring配置文件的applicationContext.xml文件头部出现红叉
- spring applicationContext.xml 配置文件 详解
- Hibernate SQL方言 (hibernate.dialect) Spring配置文件applicationContext.xml
- 使用Spring中的ClassPathXmlApplicationContext加载配置文件
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring配置文件详解 - applicationContext.xml文件路径
- Spring加载xml配置文件的方式总结
- Spring整合了CXF的一个applicationContext.xml的配置文件
- Spring学习总结(7)——applicationContext.xml 配置文详解
- Spring的applicationContext.xml配置文件的一些问题