Spring配置AOP,事务

本文介绍了如何通过Spring AOP(面向切面编程)在不侵入业务代码的情况下,实现日志、权限和事务控制等功能,提升代码复用性和维护性。详细讲解了XML配置和注解方式,并强调了通知顺序对AOP效果的重要性。
摘要由CSDN通过智能技术生成

       在开发中,为了给业务方法中增加日志记录,权限检查,事务控制等功能,此时我们需要去修改业务方法代码,考虑到代码的重用性,我们可以考虑使用 OOP 的继承或组合关系来消除重复,但是无论怎么样,我们都会在业务方法中纵向地增加这些功能方法的调用代码。此时,既不遵循开闭原则,也会为后期系统的维护带来很大的麻烦。(即不管怎样都得修改到原来的代码)为了解决该问题,OOP 思想是不行了,得使用 AOP 思想。

(学习之后的总结)

Spring配置AOP有xml和注解两种方法

 xml配置AOP

  在AOP包里面新建AOP(事务)对象

public class MyAspect {
    
    public void before(){
        System.out.println("前置通知");
    }

    public void afterReturning(){
        System.out.println("后置通知");
    }
    
    //环绕通知
    public Object around(ProceedingJoinPoint around) throws Throwable {         

        System.out.println("环绕通知之前");
        Object proceed = around.proceed();          //执行目标方法
        System.out.println("环绕通知之后");
        return proceed;                             //将目标方法的返回值返回
    }

    public void after(){
        System.out.println("最终通知");
    }

    public void afterThrowing(){
        System.out.println("异常抛出通知");
    }

}

 

 在 applicationContext.xml 里面配置。

 <!--AOP(事务)对象-->
   <bean id="aspect" class="com.bohang.tx.MyAspect" />
   <bean id="tx" class="com.bohang.tx.MyTransactionManager" />

    <!--具体实现类-->
    <bean id="employeeService" class="com.bohang.service.impl.EmployeeServiceImpl" />
    <bean id="userService" class="com.bohang.service.impl.UserServiceImpl" />

    <!--xml配置AOP      CGLIB代理 proxy-target-class="true" -->
    <aop:config>
        <aop:aspect ref="aspect">
            <!--定义切入点 where
                id:切点名称
                expression:切点表达式
            -->
            <aop:pointcut id="myPointcut"
                          expression="execution(* com.bohang.service.impl.*ServiceImpl.*(..))"/>
            <!-- 上面切入点切到方法,在方法执行之前,加 MyAspect.before() 方法  -->
            <aop:before pointcut-ref="myPointcut" method="before" />
            <!--环绕通知-->
            <aop:around pointcut-ref="myPointcut" method="around"/>
            <!-- 上面切入点切到方法,在方法正常执行完,加 MyAspect.afterReturning() 方法 -->
            <aop:after-returning pointcut-ref="myPointcut" method="afterReturning" />
            <!-- 上面切入点切到方法,在方法执行抛出异常时,加 MyAspect.afterThrowing() 方法-->
            <aop:after-throwing pointcut-ref="myPointcut" method="afterThrowing"/>
            <!--方法执行最后,最终通知-->
            <aop:after pointcut-ref="myPointcut" method="after" />
        </aop:aspect>
    </aop:config>

 

aop配置注意事项

在我们开发中通知顺序应该为:

  1. 前置通知

  2. 环绕通知之前

  3. 目标方法执行

  4. 环绕通知之后

  5. 后置通知

  6. 最终通知

但是如果在applicationContext.xml配置顺序如果不一样则会导致不同的情况,因此必须严格控制在spring配置文件中配置的通知顺序。

 注解配置AOP

 注解说明

@Aspect    作用:把当前类声明为切面类。贴在方法上(切面)
@Order(10)   作用:表示顺序,越小越早执行
@Pointcut("execution(* com.bohang.service.impl.*ServiceImpl.*(..))")  作用:指定切入点表达式
@Before("txPointCut()")  作用:把当前方法表示为前置通知
@AfterReturning("txPointCut()")   作用:把当前方法表示为正常执行的后置通知
@AfterThrowing("txPointCut()")   作用:把当前方法表示为异常时执行的通知
@After   作用:把当前方法表示为最终通知,不管报不报异常都会执行
@Around  作用:把当前方法表示为环绕通知

applicationContext.xml 文件配置 

 

<!--配置业务对象-->
    <context:component-scan base-package="com.bohang" />

    <!--配置AOP注解解析器          CGLIB  proxy-target-class="true"-->
    <aop:aspectj-autoproxy />

 切面类

@Component
@Aspect
public class AnnoMyAspect {

    @Pointcut("execution(* com.bohang.service.impl.*ServiceImpl.*(..))")
    public void AnnoMyAspect(){}
    
    @Before("AnnoMyAspect()")             //引用切点
    public void before(){
        System.out.println("前置通知");
    }

    @AfterReturning("AnnoMyAspect()")
    public void afterReturning(){
        System.out.println("后置通知");
    }

    //环绕通知
    @Around("AnnoMyAspect()")
    public Object around(ProceedingJoinPoint around) throws Throwable {         

        System.out.println("环绕通知之前");
        //执行目标方法
        Object proceed = around.proceed();          
        System.out.println("环绕通知之后");
        //将目标方法的返回值返回
        return proceed;                             
    }

    @After("AnnoMyAspect()")
    public void after(){
        System.out.println("最终通知");
    }

    @AfterThrowing("AnnoMyAspect()")
    public void afterThrowing(){
        System.out.println("异常抛出通知");
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值