In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Springboot how to use Aspectj to achieve AOP aspect-oriented programming, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
To declare AspectJ facets in Springboot
Declare aspectJ facets as Bean instances in the IOC container, i.e. add the @Component annotation; after initializing aspectJ facets in the Spring IOC container, the Spring IOC container creates proxies for beans that match aspectJ facets.
In the Aspect J annotation, an aspect is just a Java class with the @Aspect annotation.
Introduction of jar package
It is said on the Internet that springboot When using Aspectj to do aspect-oriented programming, you only need to introduce the following jar package dependencies
org.springframework.boot spring-boot-starter-aop
However, when I went to write it, when I only introduced the jar dependency of spring-boot-starter-aop, annotations such as @Component and @Aspect could not be used. Later, I found that the jar package aspectjweaver was missing. Finally, I introduced the following jar to solve the problem.
aspectj aspectjweaver 1.5.3 There is also talk on the Internet about adding it to application.properties.
spring.aop.auto =true This configuration can enable scanning of Aspectj annotations, but I went to query the springboot global configuration file, the default configuration is true(spring.aop.auto=true # Add @EnableAspectJAutoProxy), so I did not add it. There is no problem with the function, and the section can be implemented normally.
A final tip AspectJ supports 5 types of notification annotations
1)@Before: pre-notification: notification performed before method execution
2)@After: After the method is executed, i.e., when the method returns a result or throws an exception, the following post-notification records the termination of the method.
3)@AfterRunning: returns notification, executed after method returns result
ps: Postnotification is executed regardless of whether the method returns normally or throws an exception. If you want to log only when a method returns, use return notification instead of post notification.
4)@AfterThrowing: exception notification, after a method throws an exception
5)@Around: Around notification, around method execution (i.e. both before and after method execution)
Surround notifications are the most powerful of all notification types, providing full control over connection points. You can even control whether join points are executed.
Here are some examples of notifications that I wrote that you can refer to /* Identify that this method is an advance notification and that the tangency expression represents the execution of any method of any class. The first * matches any modifier and any return value, The second * represents an arbitrary class of objects, The third * represents arbitrary methods, In the parameter list.. Match any number of parameters */ //@Before: Prior notification @Before("execution (* com.lc.project.. controller..*.* (..)) ") public void beforeMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().toString(); Object result= Arrays.asList(joinPoint.getArgs()); System.out.println("The method name:"+methodName+"--value:"+result); } //@After: Post notification @After("execution (* *.* (..)) ") public void afterMethod(JoinPoint joinPoint){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method name:"+methodName+ " ends"); } //@AfterRunning: Return notifications @AfterReturning(value="execution (* *.* (..)) ",returning="result") public void afterReturningMethod(JoinPoint joinPoint,Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method name:"+methodName+ " ends and result="+result); } //@AfterThrowing: Exception notification @AfterThrowing(value="execution (* *.* (..)) ",throwing="e") public void afterReturningMethod(JoinPoint joinPoint,Exception e){ String methodName = joinPoint.getSignature().getName(); System.out.println("The method name:"+methodName+ " ends and result="+e); After reading the above, do you know how Springboot implements AOP aspect-oriented programming using Aspectj? If you still want to learn more skills or want to know more related content, welcome to pay attention to 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.