In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the AOP operation in Spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the AOP operation in Spring"?
1. AOP operation terminology 1. Connection point
Which methods in the class can be enhanced are called join points.
two。 Entry point
The method that is actually enhanced is called the pointcut.
3. Notification (enhanced)
(1) the logical part of the actual enhancement is called notification (enhancement).
(2) Notification has the following types, as follows:
Advance notice
Post notification
Surround notification
Exception notification
Final Notification (finally)
4. Cut plane
A facet is an action that refers to the process of applying a notification to a pointcut, which is called a facet.
II. AOP operation
Spring frameworks generally implement AOP operations based on AspectJ. AspectJ is not a part of Spring, but an independent AOP framework, which generally uses AspectJ and Spirng framework together for AOP operations.
There are two ways to implement AOP operation based on AspectJ:
Implementation based on xml configuration file
Implementation based on annotations (using)
2.1 pointcut expression
Pointcut expression function: know which method in which class to enhance, the syntax structure is as follows:
Execution ([permission modifier] [return type] [class full path] [method name] ([parameter list]))
As shown in the following figure:
An example of writing pointcuts is as follows:
1. If you enhance add in the com.wyf.spring5.Book class, the pointcut expression is as follows:
Execution (* com.wyf.spring5.Book.add (..))
two。 Such as enhancements to all methods in the com.wyf.spring5.Book class:
Execution (* com.wyf.spring5.Book.* (..))
3. For example, all the classes in the com.wyf.spring5 package and all the methods in the class are enhanced
Execution (* com.wyf.spring5.*.* (..))
2.2 AOP operation (AspectJ annotation mode)
1) first let's create a class and add a method:
/ * enhanced class * / public class User {public void add () {System.out.println ("add*");}} / * enhanced class * / public class User {System.out.println ("add*");}}
2) then we create the enhancement class (write enhancement logic)
Create methods in the enhancement class so that different methods represent different notification types
/ * * enhanced class * / public class UserProxy {/ * pre-notification logic * / public void before () {System.out.println ("before*");}}
3) configure notification
In the spring configuration file, turn on comment scanning. (implemented with java configuration class or xml configuration file)
Create User and UserProxy objects using annotations (add create object annotations to the class, @ Component here)
Add an annotation @ Aspect to the enhanced class
/ * enhanced class * / @ Componentpublic class User {public void add () {System.out.println ("add*");}} / * enhanced class * / @ Component@Aspect / / generate proxy object public class UserProxy {/ * * pre-notification logic * / public void before () {System.out.println ("before*");}}
Open the build proxy object in the spring configuration file
Open the Aspect generation proxy object, which is equivalent to scanning the class with @ Aspect annotation and generating a proxy object for that object.
4) configure different types of notifications
In the enhanced class, add a notification type annotation above the notification method and configure it with a pointcut expression.
The / * * enhanced class * / @ Component@Aspect / / generate proxy object public class UserProxy {/ * * pre-notification logic * / / @ Before annotation indicates that it is used as a pre-notification / / pointcut expression to specify which method of which class to enhance the notification. @ Before (value = "execution (* com.wyf.aopanno.User.add (..)") Public void before () {System.out.println ("before*");}}
The test code is as follows:
Test public void TestAop () {/ / 1. Load spring configuration file ApplicationContext context = new ClassPathXmlApplicationContext ("beanAop1.xml"); / / get the object User user = context.getBean ("user", User.class); user.add ();}
Execution result:
In the previous article, only the code for the pre-notification @ Before is given. The sample code for all five notifications is given below to complete all the notifications in the above example.
The / * * enhanced class * / @ Component@Aspect / / generate proxy object public class UserProxy {/ * * pre-notification logic * / / @ Before annotation indicates that it is used as a pre-notification / / pointcut expression to specify which method of which class to enhance the notification. @ Before (value = "execution (* com.wyf.aopanno.User.add (..)") Public void before () {System.out.println ("before*");} / * * Post Notification (return Notification) * / @ AfterReturning (value = "execution (* com.wyf.aopanno.User.add (..)") Public void afterReturning () {System.out.println ("afterReturning*");} / * final notification that * / @ After will be executed even if there is an exception (value = "execution (* com.wyf.aopanno.User.add (..)")) Public void after () {System.out.println ("after*");} / * * exception notification * / @ AfterThrowing (value = "execution (* com.wyf.aopanno.User.add (..)") Public void afterthrowing () {System.out.println ("afterthrowing*");} / * * surround the notification, notifying * / @ Around before and after the method (value = "execution (* com.wyf.aopanno.User.add (..)")) Public void around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {System.out.println ("before wrapping *"); / / execute enhanced method proceedingJoinPoint.proceed (); System.out.println ("after wrapping *");}
The result of code execution is as follows:
Where after is executed after the enhanced method, and afterreturning is executed after the method returns a value.
2.3 extraction of the same pointcut
In the above example, we have written five kinds of notifications put by the enhanced party, namely, pre-notification, post-notification, exception notification, final notification and surround notification. When we write the enhancement class, we use the pointcut expression in the notification annotation to indicate which method of which class to enhance. However, we will find that when we enhance the same method, the pointcut expression is the same, in order to avoid repetition, we can extract the same pointcut.
In the following example, we extract the same pointcut with @ Pointcut, with the following code:
/ / extract @ Pointcut for the same pair of pointcuts (value = "execution (* com.wyf.aopanno.User.add (..)") Public void pointdemo () {}
When we want to use the pointcut expression elsewhere, we can just use its method name.
As follows:
@ Component@Aspect@Order (2) public class PersonProxy {/ / pre-notice @ Before (value = "execution (* com.wyf.aopanno.User.add (..)") Public void before () {System.out.println ("Person Before****");}} 2.4 multiple enhancement classes enhance the same method and set the priority of the enhancement class
If we now have another enhancement class that also contains a before () method, we also pre-enhance the add () method of the enhanced class User. So how do we set the order of their enhancements?
We add @ Order (numeric type value) to the enhancement class. The smaller the numeric type value is, the higher the priority is. The code is as follows:
@ Component@Aspect@Order (2) public class PersonProxy {/ / pre-notice @ Before (value = "execution (* com.wyf.aopanno.User.add (..)") Public void before () {System.out.println ("Person Before****");}} at this point, I believe you have a deeper understanding of "what is the AOP operation in Spring". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 202
*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.