Spring中怎么使用事务呢

解决方案 »

  1.   

    有事找度娘 spring控制事务的例子很多了
      

  2.   

    <?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"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd 
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> <!-- 配置接口UserServer -->
    <bean id="UserServer" class="com.feng.aop.UserServerImpl"></bean>
    <!-- 配置关注点 -->
    <bean id="SecurtyHandler" class="com.feng.aop.SecurtyHandler"></bean>
    <!-- AOP的配置 -->
    <aop:config>
    <!-- 切点的配置,当切点位于方面外时,此切点可以被所有的方面所共享 -->
    <aop:pointcut id="del"
    <!-- 切点表达式,用于配对连接点(方法)是否符合表达式的定义 -->
    <!-- 第一个*表示方法的返回值,del*表示配对以del开头的所有方法,最后..表示方法的参数可以为任意类型 -->
    expression="execution(*
    com.feng.aop.UserServerImpl.del*(..))" />
    <!-- 方面配置,一个方面用于处理相同类型的功能的模块 -->
    <aop:aspect id="allMethod" ref="SecurtyHandler">
    <aop:pointcut id="query"
    expression="execution(* com.feng.aop.UserServerImpl.query*(..))" />
    <!-- 通知的使用,当某个切点的表达式成立时,即调用此通知注入的方法 -->
    <aop:before method="check" pointcut-ref="query" />
    <aop:before method="check" pointcut-ref="del" />
    </aop:aspect>
    </aop:config>
    </beans>
      

  3.   

    Spring一般用来管理数据库的事物http://blog.csdn.net/luohuijun619/article/details/4988679百度上很多用法,Lz要学会使用网络资源
      

  4.   

    错了,上面是AOP使用ProxyFactoryBean 和TransactionInterceptor<!--定义本地数据源--><bean id="dataSource" name="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}"/>
    </bean><!-- !定义单个jdbc数据源的事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean><!—定义拦截器-->
    <bean id="transactionInterceptor"
    class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager">
    <ref bean="transactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_SUPPORTS,readOnly</prop>
    <prop key="get*">PROPAGATION_SUPPORTS,readOnly</prop>
    <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
    </props>
    </property>
    </bean><!—定义业务对象-->
    <bean id="com.prs.application.ehld.sample.biz.service.sampleService.target"
    class="com.prs.application.ehld.sample.biz.service.impl.SampleServiceImpl">
    <property name="userInfoDAO"
    ref="com.prs.application.ehld.sample.integration.dao.userInfoDAO">
    </property>
    </bean><!—定义业务对象的事务代理对象-->
    <bean id="com.prs.application.ehld.sample.biz.service.sampleService" class="org.springframeword.aop.framework.ProxyFacgtoryBean">
    <property name="target"
    ref="com.prs.application.ehld.sample.biz.service.sampleService.target">
    </property>
    <property name="interceptorNames">
    <value>transactionInterceptor</value>
    </property>
    </bean>
    搞错了,呵呵http://blog.csdn.net/sghys/article/details/1916278
      

  5.   

    如果你是刚开始学的话可以下载尚学堂或者浪曦的视频,都讲得很好,没时间的话就查看Spring的api文档也行(这个难度挺高的,我只是看的一知半解)
    以下是我自己一个学习项目的代码,希望对大家有所帮助.<?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: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/aop
               http://www.springframework.org/schema/aop/spring-aop-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"> <context:annotation-config />
    <context:component-scan base-package="com.kzl" /> <bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
    <value>classpath:jdbc.properties</value>
    </property>
    </bean> <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    </bean>
    <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan">
    <list>
    <value>com.kzl.model</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="sessionFactory"></property> </bean> <aop:config>
    <aop:pointcut id="bussinessService"
    expression="execution(public * com.kzl.service..*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="bussinessService" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="getUser" read-only="true" />
    <tx:method name="add*" propagation="REQUIRED" />
    </tx:attributes>
    </tx:advice></beans>
      

  6.   

    高版本的spring事物支持注解,用起来比xml配置方便!