线程、数据库、算法、JVM、分布式、微服务、框架、Spring相关知识
一线互联网P7面试集锦+各种大厂面试集锦
学习笔记以及面试真题解析
Log注解
当数据初始化完成之后,就是编写一个自定义的Log注解。本次使用的注解主要是针对方法进行注解。具体如下:
-
@Target:注解的目标位置,主要可以有接口、类、枚举、字段、方法、构造函数、包等位置。可以根据需要进行配置。日志使用的本次基于方法注解。
-
@Retention:是指注解保留的位置,可以在源码中、类中、运行中。本次日志操作记录肯定是在运行中使用,所以选择RUNTIME。
-
@Documented:字面意思文档,也就是说明该注解将被包含在javadoc中。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Log {
/**
-
模块
-
*/
String model() default “”;
/**
-
操作
-
*/
LogTypeEnum logType() default LogTypeEnum.OTHER;
}
复制代码
LogAspect
定义一个日志的LogAspect切面。在类中需要使用@Aspect和 @Component指明这是一个切面方法在运行中进行扫描包。需要注意的是这个方法中需要定义切面和通知类型。最后根据注解和请求参数中的信息,查询到操作日志需要的信息。由于本次无需登录直接接口请求,所以操作人和操作id在演示中使用了默认值。本次示例只提取了部分操作日志信息,在项目中需要加入的日志信息多,可以根据需求进行修改。
@Aspect
@Component
public class LogAspect {
@Resource
private LogInfoMapper logInfoMapper;
/**
-
@ClassName logPointCut
-
@Description:切点信息
-
@Author JavaZhan @公众号:Java全栈架构师
-
@Version V1.0
**/
@Pointcut(“@annotation(com.example.demo.log.Log)”)
public void logPointCut(){
}
/**
-
@ClassName aroundForLog
-
@Description:环绕通知
-
@Author JavaZhan @公众号:Java全栈架构师
-
@Version V1.0
**/
@Around(“logPointCut()”)
public Object aroundForLog(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
Object result = point.proceed();
saveSmsLog(point);
return result;
}
/**
-
@ClassName saveLogInfo
-
@Description: 保存操作日志
-
@Author JavaZhan @公众号:Java全栈架构师
-
@Version V1.0
**/
private void saveLogInfo(ProceedingJoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
LogInfo logInfo = new LogInfo();
logInfo.setClassName(joinPoint.getTarget().getClass().getName());
logInfo.setMethodName(signature.getName());
Log log = method.getAnnotation(Log.class);
if(log != null){
logInfo.setModel(log.model());
logInfo.setLogType(log.logType().getValue());
}
Object[] args = joinPoint.getArgs();
try{
String params = JSONObject.toJSONString(args);
logInfo.setParams(params);
}catch (Exception e){
}
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = servletRequestAttributes.getRequest();
logInfo.setIp(IpUtils.getIpAddr(request));
logInfo.setUrl(request.getServletPath());
logInfo.setMethod(request.getMethod());
logInfo.setUserId(123);
logInfo.setUserName(“admin”);
logInfo.setCreateTime(new Date());
logInfo.setDataState(1);
//保存操作日志
logInfoMapper.insert(logInfo);
}
}
复制代码
测试日志
本示例将基于常用的接口进行测试。在Controller方法中,我们调用自定义注解,根据方法的实际使用含义指定方法的model信息和操作类型信息即可。
@RequestMapping(“user”)
@Controller
public class UserController {
@Resource
private UserService userService;
@RequestMapping(“getAllUser”)
@ResponseBody
@Log(model = “查询用户列表”,logType = LogTypeEnum.QUERY)
public List getAllUser(){
return userService.getAllUser();
}
@RequestMapping(“getUserById”)
@ResponseBody
@Log(model = “获取指定用户”,logType = LogTypeEnum.QUERY)
public User getUserById(Integer id ){
return userService.getById(id);
}
}
复制代码
调用接口:http://127.0.0.1:8888/user/getAllUser 返回的数据信息如下。
查看日志表中,可以看到已经新增一条查询用户列表的日志信息。
最后
这份《“java高分面试指南”-25分类227页1000+题50w+字解析》同样可分享给有需要的朋友,感兴趣的伙伴们可挑战一下自我,在不看答案解析的情况,测试测试自己的解题水平,这样也能达到事半功倍的效果!(好东西要大家一起看才香)
)
[外链图片转存中…(img-sumbguuc-1715481382045)]
[外链图片转存中…(img-ZWqJ7Wrt-1715481382046)]