SpringAop的简单使用

1、xml版配置aop

1.1 配置前后置通知

<!-- 配置自定义事物类 -->
<bean id="txManage" class="cn.itsource._03_aopxml.TxManage"/>

<!--配置aop-->
<aop:config>
    <!--配置切点(这里面就是需要进行添加自定义代码的地方)-->
    <aop:pointcut id="txAop" expression="execution(* cn.itsource._03_aopxml.service.I*Service.*(..))"/>

    <!--配置切面(里面包含需要执行的自定义代码)-->
    <aop:aspect ref="txManage">
        <!--在这个切点(地方),执行这个事物类里的begin方法-->
        <aop:before method="begin" pointcut-ref="txAop"/>
        <!--后置通知-->
        <aop:after-returning method="commit" pointcut-ref="txAop"/>
        <!--异常通知-->
        <aop:after-throwing method="rollback" pointcut-ref="txAop" throwing="e"/>
        <!--最终通知-->
        <aop:after method="close" pointcut-ref="txAop"/>

        <!--环绕通知-->
        <!--<aop:around method="around" pointcut-ref="txAop"/>-->
    </aop:aspect>
</aop:config>

1.2 配置环绕通知(里面就包含前后置通知)

<bean id="txManage" class="cn.itsource._03_aopxml.TxManage"/>

<!--配置aop-->
<aop:config>
    <!--配置切点-->
    <aop:pointcut id="txAop" expression="execution(* cn.itsource._03_aopxml.service.I*Service.*(..))"/>

    <!--配置切面-->
    <aop:aspect ref="txManage">
    
        <!--环绕通知-->
        <aop:around method="around" pointcut-ref="txAop"/>
    
    </aop:aspect>
</aop:config>


=====================================
//ProceedingJoinPoint :这里面能得到自己的代码
public static void around(ProceedingJoinPoint joinPoint){
        try {
            begin();
            //这能获取到要执行的代码
            joinPoint.proceed();
            commit();
        } catch (Throwable e) {
            e.printStackTrace();
            rollback(e);
        } finally {
            close();
        }
    }

2、注解配置aop

2.1 前后置通知

@Component
@Aspect //切面注解
public class TxManage {

    //配置切点
    @Pointcut("execution(* cn.itsource._04_aopanno.service.I*Service.*(..))")
    public static void  pointcut(){

    }

    //前置通知(需要切点才能定位要在哪个方法之前执行)
    @Before("pointcut()")
    public static void begin(){
        System.out.println("前");
    }

    //后置通知
    @AfterReturning("pointcut()")
    public static void commit(){
        System.out.println("后");
    }

    //异常通知,throwing:这里面的参数,必须和下面传的参数一样
    @AfterThrowing(value = "pointcut()",throwing = "e")
    public static void rollback(Throwable e){
        System.out.println("异常,原因:"+e.getMessage());
    }

    @After("pointcut()")
    public static void close(){
        System.out.println("最终");
    }
}

2.2 环绕通知

@Component
@Aspect //切面注解
public class TxManage {

    //配置切点
    @Pointcut("execution(* cn.itsource._04_aopanno.service.I*Service.*(..))")
    public static void  pointcut(){

    }

    public static void begin(){
        System.out.println("前");
    }

    public static void commit(){
        System.out.println("后");
    }

    public static void rollback(Throwable e){
        System.out.println("异常,原因:"+e.getMessage());
    }

    public static void close(){
        System.out.println("最终");
    }

    @Around("pointcut()")
    public static void around(ProceedingJoinPoint joinPoint){
        try {
            begin();
            //这能获取到要执行的代码
            joinPoint.proceed();
            commit();
        } catch (Throwable e) {
            e.printStackTrace();
            rollback(e);
        } finally {
            close();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值