In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people have no idea about how to build new aop section in springboot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Springboot introduces AOP
It is mainly divided into the following steps:
Introduce related dependencies
Create a new section
Write appropriate pointcuts and notifications
Introduce related dependencies
In addition to the web package, you also need to quote an aop-related starter-related package
Compile group: 'org.springframework.boot', name:' spring-boot-starter-aop', version: '2.2.0.RELEASE' New Section
Generally speaking, a section consists of two parts.
1 means which methods to modify, strengthen, inject, etc., all have the same meaning
2 what kind of changes are made to these methods, before and after execution, before and after execution, and so on
/ * declare a facet, just add a @ Aspect annotation * and a note @ Component is the bean * / @ Aspect@Componentpublic class TestAspect {that declares the class as Spring
/ * part of the section that tells the program which methods to operate on * A method is directly specified in this DEMO, and subsequent annotations are recommended to find the method. In the current era of popular annotations, it is no longer useful to find the method according to the method name * it is more likely to find the corresponding method through a certain annotation. Later, DEMOb will come out * / @ Pointcut ("execution (public * cloud.javastudy.demo.controller.TestController.testAspect (..)") public void testPointcut () {}
/ * * modify the found method * @ Around, modify the method before and after execution * @ Before, modify before the method execution * @ After, modify after the method execution * @ AfterReturning, modify after the method returns * @ AfterThrowing @ Around is commonly used to modify * after the method throws an exception, which is often used to add Try-catch to the method body, time the method, input parameters to the method, log the method, and so on * / @ Around ("testPointcut ()") public Object doAround (ProceedingJoinPoint joinPoint) throws Throwable {System.out.println ("before pointcut") Object proceed = joinPoint.proceed (); System.out.println ("after pointcut"); return proceed;}}
The method being tested package cloud.javastudy.demo.controller
Import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController
@ RestControllerpublic class TestController {
/ * the method of testing the cut section * @ return * / @ RequestMapping ("test/testAspect") public String testAspect () {System.out.println ("in method"); return "OK";}}
Result output:
AOP is one of the two major features of spring. Compared with the previous complex declaration of spring using xml, using aop in springboot only needs to quote a dependency, and a new aspect class can be solved. Simple demo is very simple, and you should really think about it. For example, when using Around, complex logic needs to remember to open a new subthread to deal with it. I have recorded the key hit of redis because there is no asynchronous record. Cause redis to be surprisingly slow, come on, young man!
After reading the above, have you mastered the method of how springboot creates new aop sections? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.