Aspect: Aspects, usually a class, define pointcuts and advice.
Aspects can be implemented using @Aspect annotations or based on XML configuration files. Here, only annotations are involved.
Two annotations are usually added to the tangent class:
@ Aspect: Turn classes into tangent classes
@ Component: Add the cut class to the IOC container
@ Aspect annotation can realize pre-notification, return notification, post-notification, exception notification and surround notification.
@ Before: Pre-notification, execution before target method
@ After: Post-notification, after the target method is executed (always executed)
@ AfterReturning: Returns the notification and executes after the target method (exception does not execute)
@ AfterThrowing: Exception notification, executed when an exception occurs
@ Around: surround notification, surround target method execution
The code implementation process is as follows
(1) Add business code that needs to be notified:
public class UserController { public void fun(){ System.out.println("The company's original code..."); } }
(2) Sectional Class
@Aspect @Component public class BDadvice { @Pointcut("execution(public void com.zparkep.controller.UserController.fun())") public void fun(){ } //Before advice @Before("fun()") public void before(){ System.out.println("before advice......"); } //after returning advise @After("fun()") public void after(){ System.out.println("after advice........"); } //Notification of return @AfterReturning("fun()") public void afterReturning(){ System.out.println("afterReturning advice........"); } //Exception Notification @AfterThrowing("fun()") public void afterThrowing(){ System.out.println("afterThrowing advice........"); } }
(3) Testing
@RunWith(SpringRunner.class) @ContextConfiguration("classpath:app.xml") public class AdviceTest { //Enhancement testing of UserController @Autowired private UserController userController; @Test public void fun(){ userController.fun(); } }
Result presentation
As you can see, exception notifications do not occur when the target code ends normally.
(4) Modifying the object code
public class UserController { public void fun(){ System.out.println(5/0); } }
Testing again, the results are as follows
As you can see, post-notifications will always appear, regardless of whether the target code is reporting errors or not. In the case of an error in the target code, the return notification will not appear.
(5) Modifying test surround notifications for facet classes
@Aspect @Component public class BDadvice { @Pointcut("execution(public void com.zparkep.controller.UserController.fun())") public void fun(){ } /**Around Advice * Print "around advice -- after Throwing advice" when the target code runs an error */ @Around("fun()") public void around(ProceedingJoinPoint proceedingJoinPoint){ System.out.println("around advice----before advice"); try { proceedingJoinPoint.proceed(); //Executing object code System.out.println("around advice----afterReturning advice"); } catch (Throwable throwable) { System.out.println("around advice----afterThrowing advice"); } System.out.println("around advice----after advice"); } }
The results are as follows:
Again, in the case of target code error, the return notification will not appear.