@Aspect - SpringBoot切面编程

切面编程用来写操作日志真好用啊!

这里我只写例子来记录,想深入了解的去看别的文章哈!

/**
 * 定义一个切面容器类
 *
 */
@Aspect
@Component
public class XXXAspect {

    // 可以表名整个类的方法都走此切面
    // 这里定义OpLog注解来指定方法走此切面
    @Pointcut("@annotation(com.xxx.OpLog)")
    public void logPointCut() {
    }

    //环绕切面
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        // 方法执行前的操作
    	xxx(point);
        // 执行方法
        Object result = point.proceed();
        // 方法执行后的操作
        yyy();
        return result;
    }

    private void xxx(ProceedingJoinPoint point) throws JsonProcessingException {
        // 获取方法
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        // 获取类
        Class class = point.getTarget().getClass();
        // 获取方法参数
        point.getArgs();
        // 获取方法的注解
        method.getAnnotation(OpLog.class);
        // 获取类的注解
        class.getAnnotation(xxx.class);
        // do something...
    }

    private void yyy(){
        // do something...
    }

}

/**
 * 自定义注解
 *
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OpLog {
    String operateType();
}




/**
 *
 *具体的实现类
 */
@Service
private class xxxServiceImpl implements xxxService{

    //**********

    @Override
    @OpLog(operateType = "新增") // 这里建议写个注解,不要写 新增 二字
    @Transactional(rollbackFor = Exception.class)
    public void save(Object entity) {
        // do something...    
    }

    //**********
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值