aop 获取方法入参_自定义注解aop切面,拦截方法的入参与出参(自定义属性、计算方法耗时)...

1编写注解

项目包下增加一个annotation的package,增加一个自定义的注解,如MyLog,里面可以写上自定义的属性

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

/**

* 日志注解

* @author xxx

*/

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD,ElementType.TYPE})

public @interface MyLog {

/**

* 自定义属性

* @return

*/

String type() default "xxx";

}

2编写aop处理

增加一个compnonent的package,增加一个MyLogAspect,此处使用的是环绕切,可根据业务调

import com.alibaba.fastjson.JSON;

import com.alibaba.fastjson.JSONObject;

import com.alibaba.fastjson.serializer.SerializerFeature;

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.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.web.context.request.RequestContextHolder;

import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import java.sql.Timestamp;

import java.util.Date;

/**

* Description:

* 日志拦截器

*

* @author xxx 2018/07/23.

*/

@Aspect

@Component

public class MyLogAspect {

private final Logger logger = LoggerFactory.getLogger(MyLogAspect.class);

@Autowired

private XXLogService logService;

@Pointcut(value = "@annotation(com.xxx.annotation.MyLog)")

private void pointCut() {

}

@Around(value = "pointCut() && @annotation(logType)")

public Object around(ProceedingJoinPoint joinPoint, MyLog logType) {

Timestamp start = new Timestamp(System.currentTimeMillis());

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

HttpServletRequest request = attributes.getRequest();

Date createTime = new Date();

String ip = WebUtil.getIpAddr(request);

String url = request.getRequestURL().toString();

String sessionId = request.getRequestedSessionId();

Object outObj = null;

//入参对应的下标args[index]

String paramData = JSON.toJSONString(joinPoint.getArgs()[0],

SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue);

//执行完后返回方法的出参

outObj = joinPoint.proceed();

Timestamp end = new Timestamp(System.currentTimeMillis());

//方法执行的costTime

long cost = end.getTime() - start.getTime();

//可以做其他处理***

return outObj;

}

}

3添加注解

给需要处理的方法上加上自定义的注解,如controller,当然可以加在service中,或带有@Component注解的类中的方法上

import com.alibaba.fastjson.JSON;

import com.xxx.annotation.MyLog;

import com.xxx.dto.RespResult;

import com.xxx.dto.req.ILogReqParams;

import com.xxx.service.ILogService;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @author: xxx

* @Date: Created in 下午6:01 2018/6/6

* @Des:

* @Modifyed By:

*/

@RestController

@RequestMapping("/my/index")

public class ILogController {

private Logger LOGGER = LoggerFactory.getLogger(ILogController.class);

@Autowired

private ILogService logService;

/**

* list

* @param reqParams reqParams

* @return

*/

@MyLog(type = "mytype")

@PostMapping("/list")

public RespResult list(@RequestBody LogReqParams reqParams) {

return logService.list(reqParams);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值