In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article "how to achieve AOP in Spring" is not quite understood by most people except programmers. Today, in order to make you better understand "how to achieve AOP in Spring", Xiaobian summed up the following content, which has a certain reference value. The detailed steps are clear and the details are handled properly. I hope you can get something through this article. Let's take a look at the specific content.
1. The way xml implements AOP ①, interface UserServicepackage com.ys.aop;public interface UserService {/ / add user public void addUser (); / / delete user public void deleteUser ();} ②, implementation class UserServiceImplpackage com.ys.aop;public class UserServiceImpl implements UserService {@ Override public void addUser () {System.out.println ("add User");} @ Override public void deleteUser () {System.out.println ("delete User") }} ③, facet class, that is, notification class MyAspectpackage com.ys.aop;import org.aspectj.lang.JoinPoint;public class MyAspect {/ * JoinPoint can get some basic information about the target method * @ param joinPoint * / public void myBefore (JoinPoint joinPoint) {System.out.println ("pre-notification:" + joinPoint.getSignature (). GetName () } public void myAfterReturning (JoinPoint joinPoint,Object ret) {System.out.println ("post notification:" + joinPoint.getSignature () .getName () + ",-- >" + ret);} public void myAfterThrowing (JoinPoint joinPoint,Throwable e) {System.out.println ("throw exception notification:" + e.getMessage ());} public void myAfter () {System.out.println ("final notification") }} ④, AOP configuration file applicationContext.xml ⑤, test @ Testpublic void testAop () {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); UserService useService = (UserService) context.getBean ("userService"); useService.addUser () UseService.deleteUser ();} ⑥, print the result on the console
The above example is simple, adding pre-notification and post-notification to UserService's addUser () method and deleteUser () method, which is easy to understand in practice. For example, if this is dealing with a database, then when we are in addUser () or deleteUser (), we must start the transaction before and commit the transaction after the operation. Let's configure it with annotations.
2. Annotate to implement AOP ①, import the corresponding jar package, and import the corresponding namespace in the applicationContext.xml file.
②, Annotation configuration beanxml configuration: annotated configuration:
Target class:
Facet class:
③, configuration scan comment recognition
As we talked about earlier, with the annotations configured above, how can Spring recognize that annotations have been added to these classes? We have to tell him.
Add the following configuration to the applicationContext.xml file:
④, Note configuration AOP 1. We configured it with xml as follows:
This is to tell Spring which is the aspect class. Let's configure it with annotations.
Let's add @ Aspect annotation to the aspect class, as follows:
Second, how to let Spring know the AOP annotations we have configured? It is not enough to scan the previous class annotations, here we need to configure additional AOP annotation recognition.
We added the following configuration to the applicationContext.xml file:
III. Pre-notification of annotation configuration
Let's take a look at the pre-notification of xml configuration as follows:
Then the annotations are as follows:
IV. Post-notice of annotation configuration
Xml configuration Post Notification:
Notice that the post notification has a returning= "ret" configuration, which is used to get the return value of the target method.
The annotations are configured as follows:
5. Test @ Test public void testAopAnnotation () {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext_Annotation.xml"); UserService useService = (UserService) context.getBean ("userService"); useService.addUser (); useService.deleteUser ();} VI. Print the results on the console
3. Annotation improvement
We can look at the annotation configuration of pre-notification and post-notification:
Note that the red box is clearly duplicated, and if we have multiple notification methods, we have to write the comment on each method name, and it is easy to make mistakes if the package name is complex enough. So what should I do?
The solution is to declare a public entry point:
①, add a pointcut method myPointCut () to the facet class MyAspect.java, and then add @ Pointcut annotation to this method
②, then pre-notification and post-notification, we can rewrite the configuration as follows:
4. Summary
Above, we have only explained the pre-notification and post-notification, as well as final notification, surround notification, exception notification and so on, which are all configured in the same way, so we will not explain them all here. Then let's take a look at the notes to these notices:
@ Aspect declares the aspect and modifies the aspect class to get the notification.
Notice
@ Before Front
@ AfterReturning rear
@ Around surround
@ AfterThrowing throws an exception
@ After finally
Entry point
@ PointCut, which modifies the method private void xxx () {} and then obtains the pointcut reference through "method name"
Thank you for your reading. I hope you have a certain understanding of the key issue of "how to achieve AOP in Spring". The specific usage needs to be understood by everyone through hands-on experiments. Try it quickly. If you want to read more articles about relevant knowledge points, welcome to follow the industry information channel!
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.