Spring学习之AOP与事务

时间:2023-03-08 15:40:16
Spring学习之AOP与事务

一、概述

  在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

  简而言之,aop的作用就是在不修改源码的情况下对程序进行增强,如权限校验,日志记录,性能测试,事务控制。

二、aop的操作术语

Joinpoint(连接点): 类里面可以被增强的方法,这些方法称为连接点

Pointcut(切入点):所谓切入点是指我们要对哪些Joinpoint进行拦截的定义.

Advice(通知/增强):所谓通知是指拦截到Joinpoint之后所要做的事情就是通知.通知分为前置通知,后置通知,异常通知,最终通知,环绕通知(切面要完成的功能)

Aspect(切面): 是切入点和通知(引介)的结合

Introduction(引介):引介是一种特殊的通知在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.

Target(目标对象):代理的目标对象(要增强的类)

Weaving(织入):是把增强应用到目标的过程.把advice 应用到 target的过程

Proxy(代理):一个类被AOP织入增强后,就产生一个结果代理类

三、在spring中aop操作之HelloWorld

  1.基于aspectj的xml配置

    例如:测试程序运行时间

    a.导入aop操作所需的jar包 在spring的配置文件中导入相关约束     

Spring学习之AOP与事务

    b.配置aop操作的

    

<!--配置需要被加强的方法的对象以及用来加强类的对象-->
<bean id="student"  class="cn.Student"></bean>
<bean id="strengthen"  class="cn.strengthen"></bean>

 <!-- 2配置aop操作 -->
 <aop:config>

   <!-- 配置切入点 -->  

 <aop:pointcut expression="execution(* cn.Student.study(..))"    id="poitcutAdd" />  

 <!-- 配置切面(把增强用到方法上去) --> 

  <aop:aspect ref="strengthen">  

  <!-- 指定增强类型    method:用那个方法来增强    pointcut-ref:被增强的切入点  -->  

 <aop:before method="front1" pointcut-ref="poitcutAdd" />   

 <!--配置切入方式为环绕(环绕通知)-->

  <aop:around method="aroundFront" pointcut-ref="poitcutAdd" />

   </aop:aspect>  

</aop:config>

  c.student和strengthen类

public class Strengthen {

    // ProceedingJoinPoint 用来执行被增强的方法
    public void aroundFront(ProceedingJoinPoint proceedingJoinPoint)
            throws Throwable {
        long f = System.currentTimeMillis();
    // 执行被增强的方法
proceedingJoinPoint.proceed();

System.out.println("程序执行用时:" + (System.currentTimeMillis() - f)); } }
public class Student {
    public void study() {
        System.out.println("测试方法");
    }

}

2.基于aspectj的注解配置

//注解方式进行aop操作
@Aspect
public class Student {
    @Before(value = "execution(* cn.mycookies.aop.Book.add(..))")
    public void front1() {
        System.out.println("前置增强");
    }

    @Around(value = "execution(* cn.mycookies.aop.Book.add(..))")
    public void aroundFront(ProceedingJoinPoint proceedingJoinPoint)
            throws Throwable {
        long f = System.currentTimeMillis();
        proceedingJoinPoint.proceed();

        System.out.println("程序执行用时:" + (System.currentTimeMillis() - f));
    }

}

四、在spring中使用开启事务有两种方式

  1.基于xml配置文件的事务管理

       <!--  配置事务管理器-->
    <bean id="dataSourceTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务增强-->
    <tx:advice id="txadvice1" transaction-manager="dataSourceTransactionManager">
        <!--做事务操作-->
        <tx:attributes>
            <!--设置进行事务操作的方法的匹配规则
            name方法名ing propagation为隔离级别-->
            <tx:method name="ZhuanZhang*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>
        <!--配置切面-->
    <aop:config>
        <aop:pointcut
            expression="execution(* cn.mycookies.service.CustomerService.*(..))"
            id="pointcut1" />
        <aop:advisor advice-ref="txadvice1" pointcut-ref="pointcut1" />
    </aop:config>

  2.基于配置文件和注解方式(简单)

        <!--1 .配置事务管理器 -->
    <bean id="dataSourceTransactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 2.开启事务注解 -->
    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
    <!-- 3.在方法所在类上加上注解即可Transactional -->