catalogue
Reasons for using AOP (Introduction to AOP)
Writing facet classes and test methods
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
<dependency> <groupId>org.springframework.boot </groupId> <artifactId>spring-boot-starter-aop </artifactId> </dependency>
Add @ EnableAspectJAutoProxy} annotation to startup class
Writing facet classes and test methods
@Aspect @Component public class MyAop { }
@RestController public class OneController { @GetMapping("/doCheck") public String doCheck (int age) { System.out.println( "doCheck"); if (age > 1) { throw new MyException(ExceptionEnu.SUCCESS); } else { throw new MyException(ExceptionEnu.FAILD); } } }
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
@Before(value = "execution (* own.study.web.OneController.*(..))") public void doAop( ) { System.out.println( "before aop"); }
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
@After(value = "execution (* own.study.web.OneController.*(..))") public void doAfter() { System.out.println( "after aop"); }
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
@Around(value = "execution (* own.study.web.OneController.*(..))") public Object doAround (ProceedingJoinPoint point) throws Throwable { Gson gson = new Gson(); System.out.println( "get into AOP --->" + System.currentTimeMillis()); System.out.println( "Method name = " + point.getSignature().toShortString()); Object result = point.proceed(); System.out.println( "The response parameter is = " + gson.toJson(result)); System.out.println( "AOP Done. --->" + System.currentTimeMillis()); return result; }
@RestController public class OneController { @GetMapping("/doCheck") public Object doCheck (int age) throws InterruptedException { System.out.println( "This is controller Method of --->" + System.currentTimeMillis()); Thread.sleep( 2000l); System.out.println( "doCheck"); return new MyRsp( "1", "success"); } }
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
@RestController public class OneController { @GetMapping("/doCheck") public Object doCheck (int age) throws InterruptedException { System.out.println( "This is controller Method of --->" + System.currentTimeMillis()); Thread.sleep( 2000l); System.out.println( "doCheck"); throw new MyException( "1", "success"); // return new MyRsp("1", "success"); } }
Look, AOP follow-up has not been implemented