In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to achieve aspect-oriented programming in Spring AOP. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
1. Overview
What is aspect-oriented programming?
Aspect-oriented programming is a kind of programming paradigm (other common programming paradigms include process-oriented programming, object-oriented programming OOP, function-oriented programming, event-oriented programming, aspect-oriented programming). It is not a programming language, aspect-oriented programming can solve specific problems, but can not solve all problems, it is a supplement to object-oriented programming, not a substitute.
It can solve the problem of code repeatability to a large extent, and can achieve the separation of concerns, such as the separation of functional requirements and non-functional requirements, so as to achieve centralized management, enhance the readability and maintainability of the code.
2. Common usage scenarios of AOP
The common usage scenarios in the process of system development are
Authority control
Cache control
Transaction control
Audit log
Performance monitoring
Distributed tracking
Exception handling
3. Pointcut express, the two main concerns of Spring AOP
Section expression, mainly expresses how to find the logical point of section insertion. Pointcut express provides a wealth of expressions that allow us to insert section.
Five kinds of Advice
After finding the pointcut, you need to know when to inject the code. There are five main types, as follows:
@ Before pre-notification
@ After (finally), post notification, cut in after the method has been executed
@ AfterReturning, return the notification, after the return value is returned
@ AfterThrowing, exception notification, after the exception is thrown
@ Around, surround notification, which contains all the above types
The above two concerns can be summed up in a sentence, that is, where and when to cut into our code.
4. Common aspect expressions 1, within expressions, matching packages or methods under the class import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component / * / match all methods in the ProductService class * @ Pointcut ("within (com.ruoli.service.ProductService)") * / / match the methods of all classes under the com.ruoli package and subpackages * @ Pointcut ("within (com.ruoli..*)") * / @ Aspect@Componentpublic class PkgTypeAspectConfig {@ Pointcut ("within (com.ruoli.service.sub.*)") public void matchType () {} @ Before ( "matchType ()" public void before () {System.out.println (") System.out.println ("# before");}
2. Object matching
Import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component / * / / the target object matching the AOP object is a method of the specified type That is, the method of LogService's aop proxy object * @ Pointcut ("this (com.ruoli.log.Loggable)") * / / matches the target object that implements the Loggable interface (not the object behind the aop proxy) * @ Pointcut ("target (com.ruoli.log.Loggable)") * / / this can intercept DeclareParents (Introduction) * / / target does not intercept DeclareParents (Introduction) * / / matches all Service knots The method in the tail bean * @ Pointcut ("bean (* Service)") * Created by cat on 2016-12-04. * / @ Aspect@Componentpublic class ObjectAspectConfig {@ Pointcut ("bean (logService)") public void matchCondition () {} @ Before ("matchCondition ()") public void before () {System.out.println ("); System.out.println (" # before ");}}
3. Parameter matching and method of configuring specified parameters
Import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component / * / match any method that begins with find and has only one Long parameter * @ Pointcut ("execution (* *.. find * (Long)") * / / matches any method that begins with find and the first argument is long * @ Pointcut ("execution (* *.. find * (Long)") )) ") * / / matches any method that has only one Long parameter * @ Pointcut (" within (com.ruoli..*) & & args (Long) ") * / / matches the first method * @ Pointcut (" within (com.ruoli..*) & & args (Long,..) ") * Created by cat on 2016-12-04. * / @ Aspect@Componentpublic class ArgsAspectConfig {@ Pointcut ("args (Long,String) & & within (com.ruoli.service.*)") public void matchArgs () {} @ Before ("matchArgs ()") public void before () {System.out.println ("); System.out.println (" # before ");}}
4. Annotation matching
There are method-level annotations, class-level annotations and parameter-level annotations.
Import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component / * / matching methods annotated with AdminOnly * @ Pointcut ("@ annotation (com.ruoli.anno.AdminOnly) & & within (com.ruoli..*)") * / / matching methods under classes marked with NeedSecured / / class level * @ Pointcut ("@ within (com.ruoli.anno.NeedSecured) & & within (com.ruoli..*)") * / / matching is marked with Methods of NeedSecured's class and its subclasses / / runtime level * in the environment of spring context There is no difference between the two * @ Pointcut ("@ target (com.ruoli.anno.NeedSecured) & & within (com.ruoli..*)") * / / matches the method * @ Pointcut ("@ args (com.ruoli.anno.NeedSecured) & & within (com.ruoli..*)") * Created by cat on 2016-12-04 when the parameter class is annotated with Repository. * / @ Aspect@Componentpublic class AnnoAspectConfig {@ Pointcut ("@ args (com.ruoli.anno.NeedSecured) & & within (com.ruoli..*)") public void matchAnno () {} @ Before ("matchAnno ()") public void before () {System.out.println ("); System.out.println (" # # before ");}}
5. Execution expression
Import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component / * / match any public method @ Pointcut ("execution (public * com.ruoli.service.*.* (..) / / match the no-parameter method @ Pointcut (" execution (* com.ruoli..*Service.* ()) ") in the Service class under the com.imooc package and subpackage / / match any method @ Pointcut that has only one parameter in the Service class under the com.imooc package and subpackage (" execution (* com) "). .ruoli..*Service.* (*) ") / / matches any method of any class under the com.imooc package and subpackage @ Pointcut (" execution (* com.ruoli..*.* (..) ") / / matches any method that returns String under the com.imooc package and subpackage @ Pointcut (" execution (String com.ruoli..*.* (..) ") / / matches exception execution (public * com.ruoli.service.*.* (..)) Throws java.lang.IllegalAccessException) * * / @ Aspect@Componentpublic class ExecutionAspectConfig {@ Pointcut ("execution (public * com.ruoli.service..*Service.* (..) throws java.lang.IllegalAccessException") public void matchCondition () {} @ Before ("matchCondition ()") public void before () {System.out.println ("); System.out.println (" # # before ");}}
5. Examples of five notification codes
Import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.*;import org.omg.CORBA.Object;import org.springframework.stereotype.Component / * * @ Before ("matchAnno ()") * @ After ("matchAnno ()") / / equivalent to finally * @ AfterReturning ("matchException ()") * @ AfterThrowing ("matchException ()") * @ Around ("matchException ()") * @ Before (value = "matchLongArg () & args (productId)") * public void beforeWithArgs (Long productId) * @ AfterReturning (value = "matchReturn ()" Returning = "returnValue") * public void getReulst (Object returnValue) * * / @ Aspect@Componentpublic class AdviceAspectConfig {/ * pointcut*/ @ Pointcut ("@ annotation (com.ruoli.anno.AdminOnly) & & within (com.ruoli..*)") public void matchAnno () {} @ Pointcut ("execution (* *.. find * (Long)) & within (com.ruoli..*)") Public void matchLongArg () {} @ Pointcut ("execution (public * com.ruoli.service..*Service.* (..)) Throws java.lang.IllegalAccessException) & & within (com.ruoli..*)) public void matchException () {} @ Pointcut ("execution (String com.ruoli..*.* (..) & & within (com.ruoli..*)") public void matchReturn () {} / * advice*/ @ Before ("matchLongArg () & & args (productId)") public void before (Long productId) ) {System.out.println ("# before Get args: "+ productId) } @ Around ("matchException ()") public java.lang.Object after (ProceedingJoinPoint joinPoint) {System.out.println ("# before"); java.lang.Object result = null; try {result = joinPoint.proceed (joinPoint.getArgs ()); System.out.println ("# # after returning");} catch (Throwable e) {System.out.println ("# ex") / / throw e.printStackTrace ();} finally {System.out.println ("# finally");} return result;}} above is how to implement aspect-oriented programming in the Spring AOP shared by the editor. If you happen to have similar doubts, please refer to the above analysis for understanding. If you want to know more about it, you are 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.