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.测试结果