@Aspect
@Component
public class LogApsect {
private static final Logger logger = LoggerFactory.getLogger(LogApsect.class);
ThreadLocal<Long> startTime = new ThreadLocal<>();
// 第一个*代表返回类型不限
// 第二个*代表所有类
// 第三个*代表所有方法
// (..) 代表参数不限
@Pointcut("execution(public * com.lmx.blog.controller.*.*(..))")
@Order(2)
public void pointCut(){};
@Pointcut("@annotation(com.lmx.blog.annotation.RedisCache)")
@Order(1) // Order 代表优先级,数字越小优先级越高
public void annoationPoint(){};
@Before(value = "annoationPoint() || pointCut()")
public void before(JoinPoint joinPoint){
System.out.println("方法执行前执行......before");
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
logger.info("<=====================================================");
logger.info("请求来源: =》" request.getRemoteAddr());
logger.info("请求URL:" request.getRequestURL().toString());
logger.info("请求方式:" request.getMethod());
logger.info("响应方法:" joinPoint.getSignature().getDeclaringTypeName() "." joinPoint.getSignature().getName());
logger.info("请求参数:" Arrays.toString(joinPoint.getArgs()));
logger.info("------------------------------------------------------");
startTime.set(System.currentTimeMillis());
}
// 定义需要匹配的切点表达式,同时需要匹配参数
@Around("pointCut() && args(arg)")
public Response around(ProceedingJoinPoint pjp,String arg) throws Throwable{
System.out.println("name:" arg);
System.out.println("方法环绕start...around");
String result = null;
try{
result = pjp.proceed().toString() "aop String";
System.out.println(result);
}catch (Throwable e){
e.printStackTrace();
}
System.out.println("方法环绕end...around");
return (Response) pjp.proceed();
}
@After("within(com.lmx.blog.controller.*Controller)")
public void after(){
System.out.println("方法之后执行...after.");
}
@AfterReturning(pointcut="pointCut()",returning = "rst")
public void afterRunning(Response rst){
if(startTime.get() == null){
startTime.set(System.currentTimeMillis());
}
System.out.println("方法执行完执行...afterRunning");
logger.info("耗时(毫秒):" (System.currentTimeMillis() - startTime.get()));
logger.info("返回数据:{}", rst);
logger.info("==========================================>");
}
@AfterThrowing("within(com.lmx.blog.controller.*Controller)")
public void afterThrowing(){
System.out.println("异常出现之后...afterThrowing");
}
}
@RequestMapping("/achieve")
public Response achieve(){
System.out.println("方法执行-----------");
return Response.ok(articleDetailSercice.getPrimaryKeyById(1L));
}
方法执行前执行......before 2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - <===================================================== 2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1 2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/achieve 2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求方式:GET 2018-11-23 16:31:59.795 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.achieve 2018-11-23 16:31:59.796 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 请求参数:[] 2018-11-23 16:31:59.796 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - ------------------------------------------------------ 方法执行----------- 2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Preparing: select * from article_detail where id = ? 2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Preparing: select * from article_detail where id = ? 2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Parameters: 1(Long) 2018-11-23 16:31:59.806 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - ==> Parameters: 1(Long) 2018-11-23 16:31:59.814 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - <== Total: 1 2018-11-23 16:31:59.814 [http-nio-8888-exec-9] DEBUG c.l.b.m.A.selectPrimaryKey - <== Total: 1 方法之后执行...after. 方法执行完执行...afterRunning 2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 耗时(毫秒):27 2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - 返回数据:com.lmx.blog.common.Response@8675ce5 2018-11-23 16:31:59.824 [http-nio-8888-exec-9] INFO c.l.blog.config.LogApsect - ==========================================>
@RedisCache(type = Response.class)
@RequestMapping("/sendEmail")
public Response sendEmailToAuthor(String content){
System.out.println("测试执行次数");
return Response.ok(true);
}
name:第二封邮件呢 方法环绕start...around 方法执行前执行......before 2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - <===================================================== 2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1 2018-11-23 16:34:55.347 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/sendEmail 2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求方式:GET 2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.sendEmailToAuthor 2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求参数:[第二封邮件呢] 2018-11-23 16:34:55.348 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - ------------------------------------------------------ 测试执行次数 com.lmx.blog.common.Response@6d17f2fdaop String 方法环绕end...around 方法执行前执行......before 2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - <===================================================== 2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1 2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/sendEmail 2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求方式:GET 2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.sendEmailToAuthor 2018-11-23 16:34:55.349 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 请求参数:[第二封邮件呢] 2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - ------------------------------------------------------ 测试执行次数 方法之后执行...after. 方法执行完执行...afterRunning 2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 耗时(毫秒):0 2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - 返回数据:com.lmx.blog.common.Response@79f85428 2018-11-23 16:34:55.350 [http-nio-8888-exec-2] INFO c.l.blog.config.LogApsect - ==========================================>
// 定义需要匹配的切点表达式,同时需要匹配参数
@Around("pointCut() && args(arg)")
public Response around(ProceedingJoinPoint pjp,String arg) throws Throwable{
System.out.println("name:" arg);
System.out.println("方法环绕start...around");
String result = null;
try{
result = pjp.proceed().toString() "aop String";
System.out.println(result);
}catch (Throwable e){
e.printStackTrace();
}
System.out.println("方法环绕end...around");
return (Response) pjp.proceed();
}
@Around("pointCut() && args(arg)")
public Response around(ProceedingJoinPoint pjp,String arg) throws Throwable{
System.out.println("name:" arg);
System.out.println("方法环绕start...around");
String result = null;
Object object = pjp.proceed();
try{
result = object.toString() "aop String";
System.out.println(result);
}catch (Throwable e){
e.printStackTrace();
}
System.out.println("方法环绕end...around");
return (Response) object;
}
name:第二封邮件呢 方法环绕start...around 方法执行前执行......before 2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - <===================================================== 2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 请求来源: =》0:0:0:0:0:0:0:1 2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 请求URL:http://localhost:8888/user/sendEmail 2018-11-23 16:52:14.315 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 请求方式:GET 2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 响应方法:com.lmx.blog.controller.UserController.sendEmailToAuthor 2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 请求参数:[第二封邮件呢] 2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - ------------------------------------------------------ 测试执行次数 com.lmx.blog.common.Response@1b1c76afaop String 方法环绕end...around 方法之后执行...after. 方法执行完执行...afterRunning 2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 耗时(毫秒):0 2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - 返回数据:com.lmx.blog.common.Response@1b1c76af 2018-11-23 16:52:14.316 [http-nio-8888-exec-4] INFO c.l.blog.config.LogApsect - ==========================================>
@Pointcut("execution(public * com.lmx.blog.controller.UserController.*(..))")
@Pointcut("@annotation(com.lmx.blog.annotation.RedisCache)")
@Order(1) // Order 代表优先级,数字越小优先级越高
public void annoationPoint(){};
@Pointcut("execution(public * com.lmx.blog.controller.UserController.*(..))")
@Order(2)
public void pointCut(){};