配置声明是式事务首先把hibernate.cfg.xml加载进来配置SessionFactory,在Spring中如下配置:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--property中的name值固定为configLocation
-->
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
接着配置事务管理器:
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!--
property中的name属性固定为session,ref中的bean为上面的bean中的id值
-->
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
然后配置事务的传播特性如下:
<!--transaction-manager中的值为上面的事务管理器的id值,在这里tx:advice可以理解为AOP中的advice,相当于方法的拦截,这里只是加上声明式事务
-->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
这里配置AOP:
<!--pointcut中的expression中的表达式为具体的方法
advice-ref中指向刚才的tx:advice
pointcut-ref指向的事这里定义的pointcut
-->
<aop:config>
<aop:pointcut id= "addallmethod" expression="execution(* com.sun.manager.*.*(..))"/>
<aop:advisor pointcut-ref="addallmethod" advice-ref="txadvice"/>
</aop:config>
到此整个声明式事务配置完毕!
Spring+Hibernate声明式事务
最新推荐文章于 2024-09-21 00:43:01 发布