典型的Spring的applicationcontext.xml配置文件以及注解事物配置

 注解配置事务:

 

 <!-- 启动注入功能 -->>
    <context:annotation-config />
    <!-- 启动扫描component功能 -->
    <context:component-scan base-package="org.mybatis.jpetstore" />
    <!-- 启动注解实物配置功能 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
<!-- 数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
<!-- 事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

 

 然后再需要配置的地方加上类似于这样的格式即可:

 

  @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
  public void userTest() throws Exception {
	  User u1 = new User();
	  u1.setUsername("service 添加");
	  userMapper.insert(u1);
	  System.out.println("service 添加");
	  String s = null;
	  if(s == null){
		  throw new Exception("自定义异常");
	  }
	  u1 = new User();
	  u1.setUserId(4);
	  u1.setUsername("service 修改");
	  userMapper.update(u1);
	  System.out.println("service 修改");
  }

 

 

spring注解式事务管理实例

 

一.spring配置文件

这里使用spring命名空间,如下:

<?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: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/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
    default-autowire="byName"  default-lazy-init="true">

 

 <context:property-placeholder location="classpath:jdbc.properties" />


 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
  <property name="initialSize" value="${jdbc.initialSize}" />
  <property name="maxActive" value="${jdbc.maxActive}" />
  <property name="maxIdle" value="${jdbc.maxIdle}" />
  <property name="minIdle" value="${jdbc.minIdle}" />
  <property name="maxWait" value="${jdbc.maxWait}" />
 </bean>

 <!-- 设定transactionManager -->
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>

<!--启动spring注解功能-->

 <tx:annotation-driven transaction-manager="transactionManager" />

 
</beans>

 

说明: 

1.如果事务管理器的id是transactionManager,这里可以不对transaction-manager进行配置,即<tx:annotation-driven />就可以。

2.这个配置是告诉spring在类(接口)层面或者方法层面上检查应用程序上下文中的所有标准了@Transactional的bean,spring将自动把事务通知的内容通知给它。

3.这个通知的事务参数将由@Transactional注释的参数来定义。

4.如果注释的是接口,则该接口的所有实现类都将被事务化。

 

二.使用@Transactional标注bean

 

 @Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
 public void method(){}  

 

记得要将这个bean加入到spring上下文中。

 

一般来说,上述两种事务策略就可以满足要求了,不过需要注意,注解功能的使用需要在项目中加入cglib-nodep-x.x_x.jar

 

 

 

一个典型的Spring的applicationcontext.xml配置文件

 

 

<?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: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.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation"
            value="classpath:hibernate.cfg.xml">
        </property>
    </bean>
    
    
    <!--  dao  -->
    <bean id="AdminDAO" class="dao.AdminDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="OnlineDAO" class="dao.OnlineDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="BbsDAO" class="dao.BbsDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
     <bean id="CustomerDAO" class="dao.CustomerDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="DongtaiDAO" class="dao.DongtaiDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="ChanpinDAO" class="dao.ChanpinDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>
    <bean id="DailijiamengDAO" class="dao.DailijiamengDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </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>
    
    
    <bean id="managerSeveice" class="service.managerService"> 
        <property name="adminDAO" ref="AdminDAO"/>
        <property name="onlineDAO" ref="OnlineDAO"/>
        <property name="bbsDAO" ref="BbsDAO"></property>
        <property name="customerDAO" ref="CustomerDAO"></property>
        <property name="dongtaiDAO" ref="DongtaiDAO"></property>
        <property name="chanpinDAO" ref="ChanpinDAO"></property>
        <property name="dailijiamengDAO" ref="DailijiamengDAO"></property>
    </bean>
    
    <bean id="base" class="com.lw.struts.action.BaseAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" >
        <property name="mgr" ref="managerSeveice"></property>
    </bean>
   
    
    <!-- admin -->
    <bean name="/manager_admin"
        class="com.lw.struts.action.Manager_adminAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default">
        <property name="mgr" ref="managerSeveice"></property>
    </bean>    
    <bean name="/manager_save_update_admin"
        class="com.lw.struts.action.Manager_save_update_adminAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>    
    <bean name="/manager_update_admin"
        class="com.lw.struts.action.Manager_update_adminAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    
    <!-- online -->
    <bean name="/manager_online"
        class="com.lw.struts.action.Manager_onlineAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>    
    <bean name="/manager_delete_online"
        class="com.lw.struts.action.Manager_delete_onlineAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>    
    <bean name="/manager_insert_online"
        class="com.lw.struts.action.Manager_insert_onlineAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    
    <!-- bbs -->
    <bean name="/manager_bbs"
        class="com.lw.struts.action.Manager_bbsAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_delete_bbs"
        class="com.lw.struts.action.Manager_delete_bbsAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_insert_bbs"
        class="com.lw.struts.action.Manager_insert_bbsAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>
    
    <!-- customer -->
    <bean name="/manager_customer"
        class="com.lw.struts.action.Manager_customerAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_insert_customer"
        class="com.lw.struts.action.Manager_insert_customerAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_show_customer"
        class="com.lw.struts.action.Manager_show_customerAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_delete_customer"
        class="com.lw.struts.action.Manager_delete_customerAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    
    <!-- dongtai -->
    <bean name="/manager_dongtai"
        class="com.lw.struts.action.Manager_dongtaiAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_insert_dongtai"
        class="com.lw.struts.action.Manager_insert_dongtaiAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_delete_dongtai"
        class="com.lw.struts.action.Manager_delete_dongtaiAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_update_dongtai"
        class="com.lw.struts.action.Manager_update_dongtaiAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_save_update_dongtai"
        class="com.lw.struts.action.Manager_save_update_dongtaiAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    
    <!-- chanpin -->
    <bean name="/manager_chanpin"
        class="com.lw.struts.action.Manager_chanpinAction" abstract="false"
        lazy-init="default" autowire="default" dependency-check="default" parent="base">
    </bean>    
    <bean name="/manager_delete_chanpin"
        class="com.lw.struts.action.Manager_delete_chanpinAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_insert_chanpin"
        class="com.lw.struts.action.Manager_insert_chanpinAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_update_chanpin"
        class="com.lw.struts.action.Manager_update_chanpinAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_save_update_chanpin"
        class="com.lw.struts.action.Manager_save_update_chanpinAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    
    <!-- dailijiameng -->
    <bean name="/manager_dailijiameng"
        class="com.lw.struts.action.Manager_dailijiamengAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_delete_dailijiameng"
        class="com.lw.struts.action.Manager_delete_dailijiamengAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_insert_dailijiameng"
        class="com.lw.struts.action.Manager_insert_dailijiamengAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_update_dailijiameng"
        class="com.lw.struts.action.Manager_update_dailijiamengAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/manager_save_update_dailijiameng"
        class="com.lw.struts.action.Manager_save_update_dailijiamengAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    <bean name="/testpage" class="com.lw.struts.action.TestpageAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default">
    </bean>
    
    <!-- login -->
    <bean name="/mlogin" class="com.lw.struts.action.MloginAction"
        abstract="false" lazy-init="default" autowire="default"
        dependency-check="default" parent="base">
    </bean>
    </beans>

 

你可能感兴趣的:(spring,注解事物配置)