spring注解式事务的applicationContext.xml怎么配置~~~~

如题所述

在applicationContext.xml里面加上下面的内容

<!-- 配置事务 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 启用注解 -->
<tx:annotation-driven transaction-manager="txManager"/>

前提是你已经配好了sessionFactory

这样就可以在需要加事务的方法上加上 @Transactional 来加上事务了追问

本来我用的声明式事务,能正常运行。后来改成注解就报错,就是这样写的,不知道为什么。。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-01-19
<bean id="transactionService"class="org.springframework. transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</<property>
<property name="target">
<ref local="transactionServiceControl"/>
</<property>
<property name="transactionAttributes">
<props>
<prop key=”insert*”>PROPAGATION_REQUIRED,-MyCheckedException</<prop>
<prop key=”update*”>PROPAGATION_REQUIRED</<prop>
<prop key=”*”>PROPAGATION_REQUIRED,readOnly</<prop>
</<props>
</<property>
</<bean>
第2个回答  2013-03-06
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<!-- 配置TransactionManager -->
<bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务策略 -->
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="productServiceMethods" expression="execution(* service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="productServiceMethods" />
</aop:config>

参考资料:

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网