调试的时候,经常需要看每个函数的输入输出,以前每次都是System.out.println
输出,调试完后还要删掉,要是每次都能自己输出函数的输入输出就好了。
BaseAspect类
import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; @Slf4j public abstract class BaseAspect { protected Object process(ProceedingJoinPoint pjp, StringBuilder sb) throws Throwable { ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); //获取连接点目标类名 String targetName = pjp.getTarget().getClass().getName(); //获取连接点签名的方法名 String methodName = pjp.getSignature().getName(); //获取连接点参数 Object[] args = pjp.getArgs(); //根据连接点类的名字获取指定类 Class targetClass = Class.forName(targetName); //获取方法签名xx @SysLog("xx") MethodSignature signature = (MethodSignature) pjp.getSignature(); Method method = signature.getMethod(); SysLog syslog = method.getAnnotation(SysLog.class); String description=null; if(syslog != null){ //注解上的描述 description=syslog.value(); } Object object = pjp.proceed(); String mothodurl=targetClass.getCanonicalName(); if(mothodurl.indexOf("Service")!=-1){ sb.append("【service层】"); } if(mothodurl.indexOf("Controller")!=-1){ sb.append("【controller层】"); } sb.append("【方法描述:"+description+"】") .append("【请求地址:"+request.getRequestURL().toString()+"】") .append( "【方法所在类路径:"+mothodurl+"】") .append("【方法名:"+methodName+"】") .append("【参数:"+JSON.toJSONString(args)+"】") .append("【返回结果:"+object+"】"); return object; } }
创建切面 类ControllerOrServiceDebugAspect
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
/**
* Controller调试用切面,可以快速了解输入输出是
*/
@Aspect
@Component // 非常重要
@Slf4j
public class ControllerOrServiceDebugAspect extends BaseAspect{
@Pointcut("execution(* com.fristapp.*.controller.*.*(..)) || execution(* com.fristapp.*.service.*.*(..)) ")
public void Pointcut() {
}
@Around("Pointcut()")
public Object Around(ProceedingJoinPoint pjp) throws Throwable {
StringBuilder sb = new StringBuilder();
Object object = process(pjp, sb);
log.debug(sb.toString());
return object;
}
}
测试类
controller类:
import com.fristapp.aspect.SysLog;
import com.fristapp.book.entity.BookEntity;
import com.fristapp.book.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RequestMapping("book")
@RestController
public class BookController {
@Autowired
private BookService bookService;
@PostMapping("get")
@SysLog("获取书籍信息")
public List<BookEntity> getAllBook(@RequestBody String requestString){
List<BookEntity> list=bookService.getAlls(requestString);
return list;
}
}
业务逻辑层
import com.fristapp.aspect.SysLog;
import com.fristapp.book.dao.bookMapper;
import com.fristapp.book.entity.BookEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
public class BookServiceImpl implements BookService{
@Autowired
private bookMapper bookMapper;
@Override
@SysLog("获取书籍信息service")
public List<BookEntity> getAlls(String requestString) {
List<BookEntity> list=bookMapper.getAll();
return list;
}
@Override
public String ce() {
return "哈哈哈";
}
}