一、自定义注解
/**
* @Description: TODO
* @Date: 2022-11-16 9:12
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Document
public @interface TraceLog {
}
二、切面
@Aspect
@Order(1)
@Component
public class TraceLogAspect {
@Resource
private IDemoLogService demoLogService;
@Pointcut("@annotation(TraceLog)")
public void traceLogPointcut(){};
@Around("traceLogPointcut() && args(demoLog)")
public Object around(final ProceedingJoinPoint point, DemoLog demoLog) throws Throwable{
try {
Object result = point.proceed();
demoLogService.save(demoLog);
return result;
} catch (Exception e) {
throw e;
}
}
}
三、service接口
/**
* @Description: TODO
* @Date: 2022-11-21 14:56
*/
public interface IDemoLogService extends IService<DemoLog> {
}
四、service实现
/**
* @Description: TODO
* @Date: 2022-11-21 14:59
*/
@Service
@AllArgsConstructor
public class DemoLogServiceImpl extends ServiceImpl<DemoLogMapper, DemoLog> implements IDemoLogService {
}
五、控制器
@RestController
@AllArgsConstructor
public class DemoLogTestController {
private final ITestDemoLogServce testDemoLogServce;
@GetMapping("/insert")
public void testDemoLog(){
DemoLog demoLog = new DemoLog();
demoLog.setId(1111L);
demoLog.setLogType(1);
demoLog.setOperatorParam("test");
demoLog.setRemark("测试");
demoLog.setCreatedTime(new Date());
testDemoLogServce.demoLogTrace(demoLog);
}
}
六、ITestDemoLogServce 接口
/**
* @Description: TODO
* @Date: 2022-11-21 15:04
*/
public interface ITestDemoLogServce {
void demoLogTrace(DemoLog demoLog);
}
七、TestDemoLogServceImpl实现类
/**
* @Description: TODO
* @Date: 2022-11-21 15:05
*/
@Service
public class TestDemoLogServceImpl implements ITestDemoLogServce {
@TraceLog
@Override
public void demoLogTrace(DemoLog demoLog) {
System.out.println(JSON.toJSONString(demoLog));
}
}