springboot实现自定义注解

1.自定义注解:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 日志注释
 *
 * @author zhangshiwei
 * @since 2019年9月16日 下午9:56:10
 */
@Documented
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface LogMethodAround {
}

2.自定义注解的aspect

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

import lombok.extern.slf4j.Slf4j;

/**
 * 日志注解 切面类 @LogMethodAround
 *
 * @author zhangshiwei
 * @since 2019年9月16日 下午9:58:00
 */
@Slf4j
@Aspect
@Component
public class LogMethodAroundAspect {
    /**
     * 此处的切点是注解的方式,也可以用包名的方式达到相同的效果 '@Pointcut("execution(*
     * com.wwj.springboot.service.impl.*.*(..))")'
     */
    @Pointcut("@annotation(com.test.wei.log.annotation.LogMethodAround)")
    public void logMethodAround() {
    }

    /**
     * 环绕增强,相当于MethodInterceptor
     */
    @Around("logMethodAround()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object response = null;
        long time = System.currentTimeMillis();
        try {
            response = joinPoint.proceed();
            time = System.currentTimeMillis() - time;
            return response;
        } finally {
            try {
                MethodSignature signature = (MethodSignature) joinPoint.getSignature();
                log.info("method:{},request:{},response:{},time:{} ",
                        signature.getDeclaringTypeName() + "." + signature.getName(),
                        JSON.toJSONString(joinPoint.getArgs()), JSON.toJSONString(response), time);
            } catch (Exception e) {
                System.out.println("LogAspect 操作失败:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    @Before("logMethodAround()")
    public void doBeforeAdvice(JoinPoint joinPoint) {
        System.out.println("进入方法前执行....." + joinPoint);
    }

    @AfterThrowing("logMethodAround()")
    public void throwsAfter(JoinPoint jp) {
        System.out.println("方法异常时执行....." + jp);
    }

    @After("logMethodAround()")
    public void after(JoinPoint jp) {
        System.out.println("后置最终通知,final增强,不管是抛出异常或者正常退出都会执行....." + jp);
    }

    @AfterReturning(returning = "ret", pointcut = "logMethodAround()")
    public void doAfterReturning(Object ret) {
        System.out.println("最后,正常处理完请求,返回内容 : " + ret);
    }

}

3. 测试


import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.test.wei.biz.vehicle.entity.Vehicle;
import com.test.wei.biz.vehicle.mapper.VehicleMapper;
import com.test.wei.biz.vehicle.service.VehicleService;
import com.test.wei.log.annotation.LogMethodAround;
import com.test.wei.utils.CodeMsgEnum;
import com.test.wei.utils.Result;
import com.test.wei.utils.TransformValue;

/**
 * 汽车库存基本信息表 服务实现类
 *
 * @author zhangshiwei
 * @since 2019-08-08
 */
@Service
public class VehicleServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements VehicleService {

    @LogMethodAround
    @Override
    public Result<Vehicle> findByVehicleCode(Vehicle condition) {
        Result<Vehicle> result = new Result<>();
        EntityWrapper<Vehicle> wrapper = new EntityWrapper<>();
        wrapper.eq("vehicle_code", condition.getVehicleCode());
        Vehicle vehicle = baseMapper.selectOne(condition);
        result.setValue(vehicle);
        if (vehicle == null) {
            return TransformValue.setErrorInfo(result, CodeMsgEnum.NOT_MATCH_RECORDS.getCode(),
                    "根据车辆编号未查询到车型信息啊啊啊啊啊啊啊啊!");
        }
        return result;
    }
}

4.测试结果

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值