Types of Advice in Spring Aspects Framework

Types of Advice in Spring Aspects Framework Spring Aspects is an ASPECTJ -based framework that is used to achieve cut -oriented programming (AOP) in Spring applications.AOP is a programming paradigm that provides modular and reusable code organization methods by dividing the function of the application into various focus points.Spring Aspects provides five types of notifications to insert the cutting logic in different life cycle stages of the application. 1. Before Advice: The front notice is executed before the goal method is executed.This notification is usually used to record functions such as logs and verification parameters.The following is an example code that shows how to use the front notice: public class LoggingAspect { @Before("execution(* com.example.MyService.doSomething())") public void beforeAdvice() { System.out.println("Before executing doSomething() method."); } } public class MyService { public void doSomething() { // Execute business logic } } @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } } In the above example, the BeforeAdvice () method in the loggingaspect class with @Beface annotation.This annotation specifies the cut point expression Execution (* com.example.myService.dosomething ()), which means that the front notification is performed before executing the Dosomething method of the com.example.mYservice class.In the configuration class AppConfig, the Spring ASPECTJ proxy was enabled using @EnableAspectjautoProxy. 2. AFTER Advice: The rear notification is executed after the target method is executed (whether it is returning normally or throwing an abnormality).Relax notifications are usually used to recording logs and cleaning resources.The following is an example code that shows how to use the post notification: public class LoggingAspect { @After("execution(* com.example.MyService.doSomething())") public void afterAdvice() { System.out.println("After executing doSomething() method."); } } public class MyService { public void doSomething() { // Execute business logic } } @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } } In the above example, the afterradvice () method in the Loggingaspect class with @after annotation.This annotation specifies the cut point expression Execution (* com.example.myservice.dosomething ()), which means that the rear notification is executed after executing the Dosomething method of the com.example.mYservice class. 3. Return notification (after returnte): The return notification is executed when the target method is successfully returned.Return notifications are usually used to process the results returned by the target method.The following is an example code that shows how to use the return notice: public class LoggingAspect { @AfterReturning(pointcut = "execution(* com.example.MyService.doSomething())", returning = "result") public void afterReturningAdvice(Object result) { System.out.println("Returning from doSomething() with result: " + result); } } public class MyService { public String doSomething() { // Execute business logic return "Result"; } } @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } } In the example above, the afterrereturningAdvice () method in the loggingaspect class with @Aterreturning annotation.This annotation specifies the cut point expression Execution (* com.example.myService.dosometHing ()), which means that the Dosomething method to execute the Dosomething method of the com.example.Myservice class successfully returns the return notice after returning.The return value of the method is passed through the Returning property. public class LoggingAspect { @AfterThrowing(pointcut = "execution(* com.example.MyService.doSomething())", throwing = "ex") public void afterThrowingAdvice(Exception ex) { System.out.println("Exception thrown from doSomething(): " + ex.getMessage()); } } public class MyService { public void doSomething() throws Exception { throw new Exception("Something went wrong"); } } @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } } 5. Around Advice: The surrounding notification will be executed before and after the target method, and is responsible for the execution time point of controlling the target method.Surrounding notifications are often used in affairs management and performance monitoring functions.The following is an example code that shows how to use the surround notification: public class LoggingAspect { @Around("execution(* com.example.MyService.doSomething())") public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("Before executing doSomething() method."); Object result = proceedingJoinPoint.proceed(); System.out.println("After executing doSomething() method."); return result; } } public class MyService { public void doSomething() { // Execute business logic } } @Configuration @EnableAspectJAutoProxy public class AppConfig { @Bean public LoggingAspect loggingAspect() { return new LoggingAspect(); } } In the above example, the AroundAdvice () method in the Loggingaspect class with @Around annotations.This annotation specifies the cut point expression Execution (* com.example.myservice.dosomething ()), which means that the dosomething method to execute the dosomething method of the com.example.Myservice class can be performed before and after.ProccedInPoint parameters are used to control the execution time point of the target method. To continuously execute the target method by calling the process () method, and you can get the return value of the target method. Summarize: The Spring Aspects framework provides five types of notifications, namely front notifications, rear notifications, return notifications, abnormal notifications and surround notifications.By combining these notification types and cut points expressions, the cutting logic can be inserted flexibly in the application.This method of cutting -oriented programming can realize the modularity and reuse of the code, and improve the maintenance and scalability of the application.