1. Introduction to AOP
1. What is AOP
AOP: Aspect Oriented Programming
AOP focuses not on A certain class or certain methods; control a large number of resources and focus on a large number of classes and methods.
2.AOP application scenarios and common terms
Permission control, cache control, transaction control, distributed tracing, exception handling Wait
Target: target class, that is, the class that needs to be proxied. For example: UserService
Joinpoint: The so-called connection point refers to those methods that may be intercepted. For example: All methods
PointCut Pointcut: A connection point that has been enhanced. For example: addUser()
Advice notification/enhancement, enhance the code. For example: after, before
Weaving (weaving): refers to the process of applying enhanced advice to the target object target to create a new proxy object proxy.
Aspect: It is the combination of pointcut and notification advice
3. Characteristics of AOP
1) Reduce the coupling between modules and improve the aggregation of business code. (High cohesion and low coupling)
2) Improved code reusability
3) Improved system scalability. (Higher versions are compatible with lower versions)
4) New functions can be added without affecting the original functions
2. SpringBoot uses AOP to implement the process
1.Introduce dependencies
<!-- Spring AOP --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2.Encapsulate the logging entity class
@Getter @Setter @ApiModel(value = "Systemlog对象", description = "") public class Systemlog implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty("ID") @TableId(value = "id", type = IdType.AUTO) private Integer id; @ApiModelProperty("用户名") private String userName; @ApiModelProperty("用户ID") private Integer userId; @ApiModelProperty("操作描述") private String operate; @ApiModelProperty("模块") private String module; @ApiModelProperty("创建日志时间") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date createTime; @ApiModelProperty("操作结果") private String result; }
3.Write annotation classes(custom log Annotation class)
/** * controller层切面日志注解 * @author hsq */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SystemControllerLog { // 操作描述 String operate(); // 模块 String module(); }
4. Write the aspect class of the operation log
** * @author hsq */ @Aspect @Component public class SystemLogAspect { private static final Logger logger = LoggerFactory.getLogger(SystemLogAspect.class); @Autowired private ISystemlogService iSystemlogService; @Autowired private UserService userService; /** * Controller层切点 */ @Pointcut("@annotation(com.hsq.demo.config.SystemControllerLog)") public void SystemControllerLog(){ } /** * 前置通知 用于拦截Controller层记录用户的操作的开始时间 * @param joinPoint 切点 * @throws InterruptedException */ @Before("SystemControllerLog()") public void doBefore(JoinPoint joinPoint) throws InterruptedException{ logger.info("进入日志切面前置通知!"); } @After("SystemControllerLog()") public void doAfter(JoinPoint joinPoint) { logger.info("进入日志切面后置通知!"); } /**value切入点位置 * returning 自定义的变量,标识目标方法的返回值,自定义变量名必须和通知方法的形参一样 * 特点:在目标方法之后执行的,能够获取到目标方法的返回值,可以根据这个返回值做不同的处理 */ @AfterReturning(value = "SystemControllerLog()", returning = "ret") public void doAfterReturning(Object ret) throws Throwable { } /*** * 异常通知 记录操作报错日志 * * @param joinPoint * * @param e * */ @AfterThrowing(pointcut = "SystemControllerLog()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { logger.info("进入日志切面异常通知!!"); logger.info("异常信息:" + e.getMessage()); } //使用这个方法先注释前面三个方法,留before方法就行 /** * 通知包裹了目标方法,在目标方法调用之前和之后执行自定义的行为 * ProceedingJoinPoint切入点可以获取切入点方法上的名字、参数、注解和对象 * @param joinPoint */ @Around("SystemControllerLog() && @annotation(systemControllerLog)") public Result doAfterReturning(ProceedingJoinPoint joinPoint, SystemControllerLog systemControllerLog) throws Throwable { logger.info("设置日志信息存储到表中!"); //joinPoint.proceed() 结果集 //参数数组 Object[] args = joinPoint.getArgs(); //请求参数数据 String requestJson = JSONUtil.toJsonStr(args); //方法名 String methodName = joinPoint.getSignature().getName(); //得到request HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); //得到token String token = request.getHeader("token"); String userId = JWT.decode(token).getAudience().get(0); User user = userService.getById(userId); logger.info("得到用户信息:"+user.toString()); //写入数据库操作日志 Systemlog systemlog = new Systemlog(); systemlog.setUserId(user.getUid()); systemlog.setUserName(user.getUname()); systemlog.setOperate(systemControllerLog.operate()); systemlog.setModule(systemControllerLog.module()); systemlog.setCreateTime(new Date()); //存入返回的结果集 joinPoint.proceed() Result proceed = (Result) joinPoint.proceed(); systemlog.setResult(JSONUtil.toJsonStr(joinPoint.proceed())); //保存 saveSystemLog(systemlog); return proceed; } }
5.Use the controller
@GetMapping("/userListPage") @SystemControllerLog(operate = "用户查询",module = "用户管理") public Result findUserList( @RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestParam String username, @RequestParam String loveValue, @RequestParam String address) {} @PostMapping("/addOrUpdate") @SystemControllerLog(operate = "用户修改或者添加",module = "用户管理") public Result addOrUpdateUser(@RequestBody User user){}
6. Database records
The above is the detailed content of How SpringBoot uses AOP to record interface operation logs. For more information, please follow other related articles on the PHP Chinese website!

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

JavaisnotentirelyplatformindependentduetoJVMvariationsandnativecodeintegration,butitlargelyupholdsitsWORApromise.1)JavacompilestobytecoderunbytheJVM,allowingcross-platformexecution.2)However,eachplatformrequiresaspecificJVM,anddifferencesinJVMimpleme

TheJavaVirtualMachine(JVM)isanabstractcomputingmachinecrucialforJavaexecutionasitrunsJavabytecode,enablingthe"writeonce,runanywhere"capability.TheJVM'skeycomponentsinclude:1)ClassLoader,whichloads,links,andinitializesclasses;2)RuntimeDataAr

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

The five major features of Java are polymorphism, Lambda expressions, StreamsAPI, generics and exception handling. 1. Polymorphism allows objects of different classes to be used as objects of common base classes. 2. Lambda expressions make the code more concise, especially suitable for handling collections and streams. 3.StreamsAPI efficiently processes large data sets and supports declarative operations. 4. Generics provide type safety and reusability, and type errors are caught during compilation. 5. Exception handling helps handle errors elegantly and write reliable software.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor
