`

第十四章 Spring4 切面事物与事物通知与传播行为

阅读更多
配置事务切面:


    <!-- 配置事务切面 -->
    <aop:config>
    	<!-- 切点表达式配置切点,第一个*代表方法的返回值,*.*是通配service和impl,点点代表方法参数 -->
    	<aop:pointcut id="serviceMethod" expression="execution(* com.fx.service.*.*(..))" />
    	<!-- 配置事务通知 -->
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>




事务传播行为:
Spring 中,当一个 service 方法调用另外一个 service 方法的时候,因为每个 service 方法都有事 务,这时候就出现了事务的嵌套;由此,就产生了事务传播行为;


在 Spring 中,通过配置 Propagation,来定义事务传播行为;

PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。

PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。

PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。

PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。

PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。

PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常



<?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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!-- 数据源 -->
	<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}"/>
    </bean>
    
    <!-- jdbc事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 配置事务通知,事物传播行为 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    	<tx:attributes>
    		<!-- name:通配方法名称,propagation:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。  -->
            <tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        </tx:attributes>  
    </tx:advice>
    
    <!-- 配置事务切面 -->
    <aop:config>
    	<!-- 切点表达式配置切点,第一个*代表方法的返回值,*.*是通配service和impl,点点代表方法参数 -->
    	<aop:pointcut id="serviceMethod" expression="execution(* com.fx.service.*.*(..))" />
    	<!-- 配置事务通知 -->
    	<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
    </aop:config>
 	
    <!-- 读取参数文件 -->
	<context:property-placeholder location="jdbc.properties"/>
    
    <!-- 可命名参数jdbcTemplate -->
    <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
    	<constructor-arg ref="dataSource"></constructor-arg>
    </bean>
	
	<!-- 银行数据库操作类 -->
	<bean id="bankDao" class="com.fx.dao.impl.BankDaoImpl">
		<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
	</bean> 
	
	<!-- 银行业务操作类 -->
	<bean id="bankService" class="com.fx.service.impl.BankServiceImpl">
		<property name="bankDao" ref="bankDao"></property>
	</bean> 
	
</beans>

分享到:
评论

相关推荐

    spring自定义切面实例

    通过利用Spring的AOP(面向切面编程)特性,特别是@AspectJ注解的支持,我们可以创建灵活、可重用的业务逻辑切面,从而在不修改现有代码的情况下,对应用程序的行为进行增强。 ### Spring自定义切面的核心概念 ###...

    Spring自定义切面事务问题

    ### Spring自定义切面事务问题 #### 背景与挑战 在开发基于Spring框架的应用程序时,我们经常需要利用AOP(面向切面编程)来实现横切关注点(如日志记录、安全控制、事务管理等)的模块化处理。其中,事务管理是...

    spring AOP切面编程

    Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它扩展了传统的面向对象编程,使得开发者可以方便地实现横切关注点,如日志、事务管理、性能监控等。在Spring中,AOP通过代理...

    spring aop 自定义切面示例

    AspectJ是一个强大的面向切面的编程库,它提供了与Spring AOP集成的能力,使我们可以编写更为灵活和模块化的代码。下面我们将详细探讨如何在Spring中创建和使用自定义切面,以及AspectJ的相关知识。 首先,让我们...

    spring切面小例子

    通过定义切面、切点和通知,我们可以将非业务逻辑的关注点如日志记录与核心业务代码解耦,使得代码更清晰,维护性更强。这个例子对于理解和学习Spring AOP以及注解驱动的编程方式具有很好的实践意义。

    Spring AOP面向切面三种实现

    在IT行业中,Spring框架是Java企业级应用开发的首选,其强大的功能之一就是AOP(面向切面编程)。本文将详细解析Spring AOP的三种实现方式,帮助你深入理解这一重要概念。 首先,理解AOP的基本概念至关重要。AOP是...

    Spring面向切面编程AOP

    4. **切入点(Pointcut)**:切入点是匹配连接点的规范,用于定义哪些连接点将被切面通知所拦截。Spring使用表达式语言来定义切入点,例如可以根据方法名、参数类型等进行匹配。 5. **织入(Weaving)**:织入是将...

    spring切面AOP所使用的jar包

    Spring框架的AOP(面向切面编程)是其核心特性之一,它允许开发者在不修改原有代码的情况下,通过切面来插入额外的功能,比如日志记录、事务管理、性能监控等。在Spring AOP中,主要涉及到两个重要的库:...

    Spring中切面捕获自定义异常

    Spring中,使用切面全局捕获异常的同时,进行自定义异常抛出捕获,方便代码书写。

    spring的aop切面编程实例

    Spring AOP(面向切面编程)是Spring框架的重要组成部分,它允许我们在不修改源代码的情况下对应用程序的行为进行统一管理和控制。在本实例中,我们将深入探讨如何使用AspectJ技术和XML配置来实现AOP。 首先,了解...

    Spring AOP 建立一个简单的切面及测试

    ### Spring AOP 实现简单切面及测试 #### 一、引言 在软件开发过程中,经常遇到一些横切关注点的问题,例如日志记录、性能监控等,这些功能往往贯穿于应用程序的多个模块中。面向切面编程(Aspect Oriented ...

    spring 切面编程实例

    Spring AOP(Aspect Oriented Programming,面向切面编程)是Spring框架的重要组成部分,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。AOP的核心概念包括切面、连接点、通知、切入点表达式等,...

    mybatis 拦截器 + spring aop切面 + spring事务+ 反射工具类

    切面通过通知(advises)来增强其他对象的行为。Spring支持多种通知类型,包括前置通知(Before)、后置通知(After)、环绕通知(Around)等。在实际应用中,我们可以通过配置XML或使用注解方式定义切面,以实现对...

    SpringAOP切面编程依赖jar包.rar

    学习Spring开发的AOP面向切面编程时所需要的jar包,包括com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

    spring事物传播测试表

    本文将深入探讨“Spring事物传播测试表”所涉及的知识点。 首先,理解事务是非常重要的。在数据库操作中,事务确保数据的一致性和完整性。例如,一组相关的数据库操作要么全部成功,要么全部失败,这就是事务的ACID...

    利用SPring AOP配置切面的一个例子

    ### Spring AOP 配置切面实例解析 #### 一、引言 本文将通过一个具体的例子来介绍如何在Spring框架中使用面向切面编程(Aspect Oriented Programming, AOP)。我们将关注如何定义一个切面(Aspect),以及如何在...

    day39-Spring 06-Spring的AOP:带有切点的切面

    在"day39-Spring 06-Spring的AOP:带有切点的切面"这个主题中,我们将深入探讨如何在Spring中实现带有切点的切面,以及它们如何与源码和工具结合使用。 首先,理解AOP的基本概念非常重要。AOP的核心是切面(Aspect...

    AOP_使用spring框架进行面向切面编程

    面向切面编程(AOP)是一种编程范式,它旨在减少代码中的重复部分,特别是那些与核心业务逻辑无关但又必须处理的交叉关注点,如日志、事务管理、安全控制等。Spring框架是Java领域中实现AOP的常用工具,它通过提供...

    spring4 AOP 面向切面编程@Aspect

    在Spring框架中,AOP(面向切面编程)是一种强大的设计模式,它允许开发者将关注点从业务逻辑中分离出来,比如日志记录、事务管理、权限检查等。`@Aspect`是Spring AOP的核心注解,用于定义一个切面。下面我们将详细...

Global site tag (gtag.js) - Google Analytics