Chapter 1 AOP and chapter 1 aop
We usually use the AspectJ annotation for AOP. There are 6 middle-Cut sections in total.
- Front: @ Before
- Post: @ After
- Returned value: @ AfterReturing
- Exception: @ AfterThrowing
- Surround: @ Around: can be understood as a set of the above four types
- Reference (not commonly used)
Two difficult parts:
- Connection points: JoinPoint and ProceedJoinPoint
- The connection point is actually a method that overwrites the line expression. Multiple information can be obtained based on the connection point, which is commonly used as follows:
- GetSignature (): Get the return value, full class name, and parameter type of the current method, for example: Shop com. xxx. firstboot. dao. ShopDao. getShop (int)
- GetTarget (): obtains the class instance of the current method, for example, com. xxx. firstboot. dao. ShopDao @ 72f97c4f.
- GetArgs (): Get the parameters of the current method, for example, [Ljava. lang. Object; @ 6fbd538a
- JoinPoint is habitually used in addition to @ Around, And ProceedJoinPoint is habitually used in @ Around (because the ProceedJoinPoint proceed () method must be used to execute the target method)
- Cut expression:
- Execution (* com. xxx. firstboot. dao. *. * (...)
- First *: represents any permission and return type, for example, public Shop. If you need to specify it explicitly, specify
- The second *: represents all classes in this package, for example, ShopDao. If you need to specify it explicitly, specify
- Third *: all methods under the class, for example, getShop. If you need to specify it explicitly, specify
- ...: Indicates that the input parameter type of the method can be any type or any number (including 0), for example, int. If you need to specify it explicitly, specify
- The tangent expression is the matching expression of the method (connection point ).
Two supplements:
- The @ Order (number) annotation can be used to specify the weaving Order. For example, @ Order (1). The smaller the number, this annotation is used on the cut surface (that is, the cut surface class)
- The cut-point expression can be reused.