Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

@ Aspect@Order what is the order in which notifications are executed

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

I would like to share with you the order in which @ Aspect@Order notices are executed. I believe most people don't know much about it, so share this article for your reference. I hope you will learn a lot after reading this article. Let's learn about it.

@ Aspect@Order the execution order of each notification

Two aspect classes: [log] and [judgment parameters], corresponding to the order @ Order (0) and @ Order (1), respectively.

This article will only focus on the order of execution.

Code

[business category]

/ * login controller * / @ Controllerpublic class LoginController {/ / throws an exception public void loginWithThrow (String username, String password) throws Exception {if (username = = null | | password = = null) {throw new Exception ("login information cannot be empty");} System.out.println ("LoginController#login...") } / / public void loginWithTryCatch (String username, String password) {try {if (username = = null | | password = = null) {throw new Exception ("login information cannot be empty");} System.out.println ("LoginController#login...");} catch (Exception e) {e.printStackTrace () }}}

[section class]

/ * * output log annotations * / @ Order (0) @ Aspect@Componentpublic class LogAspect {/ / extract the methods of all classes under the common execution / / com.yuki.demo.aop.aspect package or subpackage @ Pointcut ("execution (* com.yuki.demo.aop.aspect..*.* (..)") Public void pointcut () {} / / pre-notice / / @ Before ("execution (public void com.yuki.demo.aop.aspect.LoginController.* (String,String))") @ Before ("pointcut ()") public void before () {System.out.println ("LogAspect#before...") } / / ProceedingJoinPoint only has @ Around ("execution (public void com.yuki.demo.aop.aspect.LoginController.* (String,String))") public void around (ProceedingJoinPoint joinPoint) throws Throwable {System.out.println ("LogAspectA#around start...") / / the execution of the proxy method. If there is no joinPoint.proceed (), the pre-notification @ Before will not be executed, and the other notifications will be normal joinPoint.proceed (). / / after executing the method, if joinPoint.proceed () throws an exception, the sentence will not be executed. After throwing an exception, the aroud method will jump out of System.out.println ("LogAspectA#around ends..."). } / / Post notification (as long as the join point is executed, regardless of whether an exception is thrown) @ After ("execution (public void com.yuki.demo.aop.aspect.LoginController.* (String,String)") public void after () {System.out.println ("LogAspect#after...") } / / exception notification (this notification is executed only if an exception is thrown outside the execution of the joinPoint.proceed () method) @ AfterThrowing ("execution (public void com.yuki.demo.aop.aspect.LoginController.* (String,String))") public void afterThrowing () {System.out.println ("LogAspect#afterThrowing...") } / / normal return notification notification (the notification will not be executed until it ends normally) @ AfterReturning ("execution (public void com.yuki.demo.aop.aspect.LoginController.* (String,String))") public void afterReturning () {System.out.println ("LogAspect#afterReturning...");}}

[section class]

/ * * facet class to determine whether the sign of the request parameter is correct * / @ Order (1) @ Aspect@Componentpublic class SignAspect {@ Around ("execution (public void com.yuki.demo.aop.aspect.LoginController.* (String,String))") public void around (ProceedingJoinPoint joinPoint) throws Throwable {System.out.println ("SignAspect#around start..."); joinPoint.proceed () System.out.println ("SignAspect#around ends...");}}

[startup configuration]

Omit... Non-key point

[test class]

@ SpringBootTestclass AopApplicationTests {@ Autowired private LoginController loginController; @ Test void contextLoads () {loginController.loginWithTryCatch ("yuki", "1234");}}

[console output]

LogAspectA#around, start.

LogAspect#before...

SignAspect#around, start.

LoginController#login...

The end of SignAspect#around...

The end of LogAspectA#around...

LogAspect#after...

LogAspect#afterReturning...

Summary

Spring AspectJ order (sequence) @ Aspect@Order (2) public class HelloWorldAspectAnnotation {/ * JoinPoint interface * @ param joinPoint * / / * public interface JoinPoint {String toString (); / / related information about the location of the connection point String toShortString (); / / short related information about the location of the connection point String toLongString () / / all relevant information about the location of the connection point Object getThis (); / / return the AOP proxy object Object getTarget (); / / return the target object Object [] getArgs (); / / return the notified method parameter list Signature getSignature (); / / return the current connection point signature SourceLocation getSourceLocation () / / returns the location of the connection point method in the class file String getKind (); / / connection point type StaticPart getStaticPart () / / return the static part of the connection point} * / define the pre-notification. Note here is sayHello2 / / use @ Before to declare the pre-notification. Where value is used to define a pointcut expression or to reference a named pointcut @ Before (value= "execution (* com.boventech..*.sayHello2 (..)) & & args (param)", argNames= "param") public void beforeAdvice (JoinPoint joinPoint,String param) {System.out.println (1) System.out.println ("= ="); System.out.println ("= param:" + param); System.out.println ("= ="); System.out.println (joinPoint.getArgs () .length); System.out.println ("= ="); System.out.println (joinPoint.toString ()) System.out.println ("= ="); System.out.println (joinPoint.getTarget ()); System.out.println ("= ="); System.out.println (joinPoint.getThis ()); System.out.println ("= ="); System.out.println ("= before advice") } / * value: specify a pointcut expression or name a pointcut Pointcut: also specifies a pointcut expression or named pointcut, and if specified will override the value attribute specified, pointcut has a high priority * / @ AfterReturning (value= "execution (* com.boventech..*.sayHello2 (..)) & & args (param)", argNames= "param", pointcut= "execution (* com.boventech..*.sayHello2 (..) & & args (param)") public void afterFinallyAdvice (JoinPoint joinPoint,String param) {System.out.println ("param:" + param); System.out.println ("=") System.out.println ("= after finally advice");} @ Aspect@Order (1) public class HelloWorldAspectAnnotation2 {/ * JoinPoint interface * @ param joinPoint * / / * public interface JoinPoint {String toString (); / / information about the location of the connection point String toShortString () / / brief related information about the location of the connection point String toLongString (); / / all relevant information about the location of the connection point Object getThis (); / / return AOP proxy object Object getTarget (); / / return target object Object [] getArgs () / / returns the list of notified method parameters Signature getSignature (); / / returns the current connection point signature SourceLocation getSourceLocation (); / / returns the location String getKind () in the class file where the connection point method resides; / / connection point type StaticPart getStaticPart () / / return the static part of the connection point} * / define the pre-notification. Note here is sayHello2 / / use @ Before to declare the pre-notification. Where value is used to define pointcut expressions or to reference named pointcuts @ Before (value= "execution (* com.boventech..*.sayHello2 (..)) & & args (param)", argNames= "param") public void beforeAdvice (JoinPoint joinPoint,String param) {System.out.println (2) System.out.println ("= =");} / * value: specify a pointcut expression or name a pointcut Pointcut: also specifies a pointcut expression or named pointcut, and if specified will override the value attribute specified, pointcut has a high priority * / @ AfterReturning (value= "execution (* com.boventech..*.sayHello2 (..)) & & args (param)", argNames= "param", pointcut= "execution (* com.boventech..*.sayHello2 (..) & & args (param)") public void afterFinallyAdvice (JoinPoint joinPoint,String param) {System.out.println ("order:" + 2) }} public class AopAnnotationTest {@ Test public void testHelloworld () {ApplicationContext ctx = new ClassPathXmlApplicationContext ("/ helloWorld2.xml"); IHelloWorld2Service helloworldService = ctx.getBean ("helloWorld2Service", IHelloWorld2Service.class); String param = "12"; helloworldService.sayHello2 (param) }} above is all the content of the article "what is the order of execution of @ Aspect@Order notifications". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report