Spring-Aop配置:xml配置 + 注解

1、横切逻辑类配置

@Component
@Aspect
public class LogUtil {

    @Pointcut("execution(public void deppon.service.AccountServiceImpl.transfer(java.lang.String,java.lang.String,int))")
    public void info(){

    }

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

    //@After("info()")
    public void afterMethod(){
        System.out.println("方法执行之后执行......");
    }
    // @AfterThrowing("info()")
    public void exceptionMethod(){
        System.out.println("异常方法执行之后执行......");
    }
    // @AfterReturning("info()")
    public void finallyMethod(){
        System.out.println("finally执行......");
    }
    // @Around("info()")
    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-1、xml配置

<aop:aspectj-autoproxy/>

2-2、将 xml 配置替换成注解配置,在实例 bean上添加注解:@EnableAspectJAutoProxy

猜你喜欢

转载自blog.csdn.net/xingcsdnboke/article/details/129418097