Spring-Aop配置:xml配置

切面编程 = 连接点 + 方位点 + 横切逻辑

1. 横切逻辑

public class LogUtil {

    public void beforeMethod(JoinPoint joinPoint){
        System.out.println("方法执行之前执行......");
        Object[] args = joinPoint.getArgs();
        for(Object arg:args){
            System.out.println(arg);
        }
        System.out.println("方法执行之前执行......");
    }

    public void afterMethod(){
        System.out.println("方法执行之后执行......");
    }

    public void exceptionMethod(){
        System.out.println("异常方法执行之后执行......");
    }

    public void finallyMethod(){
        System.out.println("finally执行......");
    }

    public Object surroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("事务开始!!!");
        Object returnValue = null;
        try{
            returnValue = joinPoint.proceed();
        } catch (Exception throwable) {
            throwable.printStackTrace();
            System.out.println("事务回滚!!!");
            throw throwable;
        }
        System.out.println("事务提交!!!");
        return returnValue;
    }
}

2. xml配置

<!--横切逻辑-->
<bean id="logUtil" class="deppon.aspectjweaver.LogUtil"></bean>
<!--配置切面-->
<aop:config>
    <!--引用横切逻辑-->
    <aop:aspect id="logAspect" ref="logUtil">
        <!--配置切入点-->
        <aop:pointcut id="log1" expression="execution(public void deppon.service.AccountServiceImpl.transfer(java.lang.String,java.lang.String,int))"/>
        <!--方位点定位方法,引用切入点-->
        <aop:around method="surroundMethod" pointcut-ref="log1"></aop:around>
    </aop:aspect>
</aop:config>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值