Section of spring, use of AOP

catalogue

Reasons for using AOP (Introduction to AOP)

Use of AOP

Enable AOP support

Writing facet classes and test methods

Write slice method

@Before

@After

@AfterThrowing

@AfterReturning

@Around

Reasons for using AOP (Introduction to AOP)

We know that spring has two cores, IOC (inversion of control) and AOP (aspect). Why use AOP? What is AOP? Strictly speaking, AOP is a programming specification and a programming idea, not created by spring. AOP can help us free ourselves from redundant general business logic to a certain extent, such as the request of each interface, It is necessary to record logs. If the operation is written everywhere, it will be very cumbersome. Of course, logging is not the only use

 

Spring's AOP can only be managed based on IOC, and it can only act on the bean s of the spring container

 

Moreover, spring's AOP is to solve the most common method weaving in enterprise development, not to become a complete AOP use solution like AspectJ

Use of AOP

Enable AOP support

To use AOP, you must first enable AOP support

   
  1. <dependency>
  2. <groupId>org.springframework.boot </groupId>
  3. <artifactId>spring-boot-starter-aop </artifactId>
  4. </dependency>

Add @ EnableAspectJAutoProxy} annotation to startup class

Writing facet classes and test methods

   
  1. @Aspect
  2. @Component
  3. public class MyAop {
  4. }
   
  1. @RestController
  2. public class OneController {
  3. @GetMapping("/doCheck")
  4. public String doCheck (int age) {
  5. System.out.println( "doCheck");
  6. if (age > 1) {
  7. throw new MyException(ExceptionEnu.SUCCESS);
  8. } else {
  9. throw new MyException(ExceptionEnu.FAILD);
  10. }
  11. }
  12. }

Remember to leave the aspect class to spring management ~ @ Component

Writing slice method

@Before

The usage of this annotation, that is, before executing what you want to execute, execute the method with this annotation

such as

   
  1. @Before(value = "execution (* own.study.web.OneController.*(..))")
  2. public void doAop( ) {
  3. System.out.println( "before aop");
  4. }

That is, if I want to call the method of OneController, I will execute the doAop method before calling it

Let's test it

@After

The usage of this annotation is to execute the annotated method after you execute your method and before it is really returned to the caller

such as

   
  1. @After(value = "execution (* own.study.web.OneController.*(..))")
  2. public void doAfter() {
  3. System.out.println( "after aop");
  4. }

Let's test it

@AfterThrowing

See the meaning of the name. After an exception occurs, execute the method with this annotation

Have you noticed the test method I wrote above? I threw a custom exception

Let's test it

@AfterReturning

The usage of this annotation can be guessed from the name. After execution, execute this method

But! This execution completion refers to the normal execution completion without throwing exceptions. Don't you believe it? Let's try

@Around

This is the most powerful annotation. Around the notification, the annotated method will be executed before and after the method is executed

   
  1. @Around(value = "execution (* own.study.web.OneController.*(..))")
  2. public Object doAround (ProceedingJoinPoint point) throws Throwable {
  3. Gson gson = new Gson();
  4. System.out.println( "get into AOP --->" + System.currentTimeMillis());
  5. System.out.println( "Method name = " + point.getSignature().toShortString());
  6. Object result = point.proceed();
  7. System.out.println( "The response parameter is = " + gson.toJson(result));
  8. System.out.println( "AOP Done. --->" + System.currentTimeMillis());
  9. return result;
  10. }
   
  1. @RestController
  2. public class OneController {
  3. @GetMapping("/doCheck")
  4. public Object doCheck (int age) throws InterruptedException {
  5. System.out.println( "This is controller Method of --->" + System.currentTimeMillis());
  6. Thread.sleep( 2000l);
  7. System.out.println( "doCheck");
  8. return new MyRsp( "1", "success");
  9. }
  10. }

But watch out! This surround notification is not omnipotent. It is not necessarily good. You can use it as needed. For example, in a scenario, when your method throws an exception, the surround notification will not continue to be executed

Let's experiment

Method of rewriting controller

   
  1. @RestController
  2. public class OneController {
  3. @GetMapping("/doCheck")
  4. public Object doCheck (int age) throws InterruptedException {
  5. System.out.println( "This is controller Method of --->" + System.currentTimeMillis());
  6. Thread.sleep( 2000l);
  7. System.out.println( "doCheck");
  8. throw new MyException( "1", "success");
  9. // return new MyRsp("1", "success");
  10. }
  11. }

Look, AOP follow-up has not been implemented

 

 

Keywords: Spring AOP

Added by homer.favenir on Tue, 25 Jan 2022 04:22:10 +0200