使用XML进行声明式事务管理

当多个方法需要事务时,可用XML方式,表达式可灵活配置

a)     xml(推荐,可以同时配置好多方法)

                i.          <bean txmanager

               ii.          <aop:config

1.     <aop:pointcut

2.     <aop:advisor pointcut-ref advice-ref

             iii.          <tx:advice: id transaction-manager = ...

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <context:annotation-config />
 <context:component-scan base-package="org.spring"/>
 
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">

  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://localhost:3306/spring" />
  <property name="username" value="root" />
  <property name="password" value="root" />
 </bean>
  
 <bean id="mySessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="annotatedClasses">
   <list>
    <value>com.model.User</value>
    <value>com.model.Log</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
   </props>
  </property>
 </bean>

 <bean id="txManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="mySessionFactory" />
 </bean>
 
 <aop:config>
     <aop:pointcut id="bussinesService"
           expression="execution(public * com.service..*.*(..))"/>
     <aop:advisor pointcut-ref="bussinesService" advice-ref="txAdvice"/>
   </aop:config>

 

 <tx:advice id="txAdvice" transaction-manager="txManager">
  <tx:attributes>
   <tx:method name="getUser" read-only="true" />
   <tx:method name="add*" />
  </tx:attributes>
 </tx:advice>
</beans>

------------------------------------------------------------------

txManager指定 使用什么事务类(Hibernate、JDBC、JPA……)

需要参考SessionFactory,从其中读取配置,ref="sessionFactory_id"

而SessionFactory用到了数据源,需参考数据源ID配置

 

aop:config指定切面配置

aop:pointcut定义切面,表达式

aop:advisor建议者,使用XXX建议,参考的是txAdvice,ref="txAdvice_id"

 

tx:advice指定事务的建议,需要指明是哪个事务类txManager

tx:attributes事务属性

tx:method 要在name为XXX的方法上,织入切面

如果不指定propagation 默认是 REQUERED

 

测试:

UserService类:

--------------------

 public void add(User user) {
  Session session = sessionFactory.getCurrentSession();
  session.save(user);
  Log log = new Log();
  logDAO.save(log);
 }

--------------------

Junit:

--------------------

@Test
 public void testAdd() throws Exception {
  ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  
  UserService service = (UserService)ctx.getBean("userService");
  service.add(new User());
    
 }

猜你喜欢

转载自leon-s-kennedy.iteye.com/blog/1533447
今日推荐