AOP description of spring, use code, xml · annotation

Posted by doobster on Tue, 26 Oct 2021 04:08:48 +0200

Popular description of aop terms

Joinpoint connection point: the line of program that calls the pointcut function is.
Target target object: the object of the function body that needs supplementary functions.
Pointcut tangent: the method name of the (Target) in the target object.
Advice enhancement method: adding a program to the tangent point in the target object is called enhancement method.
Aspect aspect: including pointcut and advice methods.
Weaving weaving: the process of adding enhancement methods to the target object (pointcut) is weaving.
Proxy proxy: when the weaving is not started, that is, before the weaving starts, the target object uses dynamic proxy, that is, reflection technology to generate a proxy object, so that the weaving can use dynamic proxy to add enhancement methods to the target object.
Introduction intervention: the whole process of weaving is called intervention. I almost understand it.

aop professional description

Aspect: the aspect declaration is similar to the class declaration in Java. The aspect contains some pointcuts and corresponding Advice.
joint point: it refers to the points clearly defined in the program, typically including method calls, access to class members, execution of exception handling program blocks, etc. it can also nest other joint points.
Pointcut: represents a group of joint points. These joint points are either combined through logical relationships, or gathered through wildcards, regular expressions, etc. it defines where the corresponding Advice will occur.
Advice (enhancement): advice defines the specific operations to be performed by the program points defined in Pointcut. It distinguishes between before, after and around the code executed before, after or instead of each joint point.
Target: the target object woven into Advice.
Weaving: the process of connecting an Aspect with other objects and creating an advised object.

How to use aop

You can look at this and understand aop
Pictures use online resources


xml code

Five types of enhancement methods

First, add the target object and the object of the enhancement method to the bean
Redefine the rules of the pointcut to which the enhancement method should be configured
Then declare which type of enhancement method to use

<!-- join bean-->
<bean id="Target object" class="com.hehehe.Hahaha"/>
<bean id="Enhanced object" class="com.hehehe.Aaaa"/>
<!-- Set cut in rules for enhancement methods-->
<!--aop to configure-->
<aop:config>
    <!--Configure section-->
    <!--Configuring pointcuts can enhance that method, that is, enhance the method bean The method in-->
    <aop:pointcut id="pt" expression="execution(* *..*())"/>
    <!--Configure section-->
    <aop:aspect ref="Enhanced object">
        <!—Relationship between notifications and pointcuts-->
        <aop:before method="logAdvice" pointcut-ref="pt"/>
    </aop:aspect>
</aop:config>

1. Pre enhancement: add an enhancement method in the first line of the pointcut method

<!-- Explain which method to use to add the enhancement method-->
	<aop:aspect ref="Enhanced object">
        <!—Relationship between notifications and pointcuts-->
        <aop:before method="logAdvice" pointcut-ref="pt"/>
	</aop:aspect>

2. Post enhancement, which starts at the end of the pointcut method

	<aop:aspect ref="Enhanced object">
    	<aop:after method="methodName" pointcut-ref="pt"/>
	</aop:aspect>

3. Combination of surround enhancement, pre enhancement and post enhancement

	<aop:aspect ref="Enhanced object">
    	<aop:around method="methodName" pointcut-ref="pt"/>
	</aop:aspect>

4. Enhanced after return. The original method is executed normally and the result is returned

	<aop:aspect ref="Enhanced object">
    	<aop:after-returning method="methodName" pointcut-ref="pt"/>
	</aop:aspect>

5. Enhance after throwing an exception, and execute after the original method throws an exception

	<aop:aspect ref="Enhanced object">
   	 	<aop:after-throwing method="methodName" pointcut-ref="pt"/>
	</aop:aspect>

The enhancement method obtains the parameters of the objective function

1. Use JoinPoint to obtain parameters

public void before(JoinPoint jp) throws Throwable {
    Object[] args = jp.getArgs();
}

2 configuration entry point

<aop:config>
    <!--Configure pointcuts-->
    <aop:pointcut id="pt" expression="execution(* *..*()) &amp;&amp; args(a,b)"/>
</aop:config>	
// Same as the parameter name of the pointcut
public void before(int a,int b) throws Throwable {
    Object[] args = jp.getArgs();
}

3 entry points and enhancement methods

    <!--Configure section-->
    <!--Configuring pointcuts can enhance that method, that is, enhance the method bean The method in-->
    <aop:pointcut id="pt" expression="execution(* *..*()) &amp;&amp; args(a,b)"/>
    <!--Configure section-->
    <aop:aspect ref="Enhanced object">
        <!—Relationship between notifications and pointcuts-->
        <aop:before method="logAdvice" pointcut-ref="pt" arg-names="b,a"/>
    </aop:aspect>
</aop:config>
// Same as the parameter name of the pointcut
public void before(int a,int b) throws Throwable {
    Object[] args = jp.getArgs();
}

Configuration rules for pointcuts

Interception rules

Keyword (return value of access modifier package name, class name, method name (parameter) exception name)

execution(public int *.*.*(int a,int b) )
execution(public int *.*.*(..) )

Scope of interception rules

<aop:config>
    <!--1 Configure common pointcuts-->
    <aop:pointcut id="pt1" expression="execution(* *(..))"/>
    <aop:aspect ref="myAdvice">
        <!--2 Configure local pointcuts-->
        <aop:pointcut id="pt2" expression="execution(* *(..))"/>
        <!--3 Direct configuration pointcuts-->
        <aop:before method="logAdvice" pointcut="execution(* *(..))"/>


        <!--Reference public pointcuts-->
        <aop:before method="logAdvice" pointcut-ref="pt1"/>
        <!--Reference local pointcuts-->
        <aop:before method="logAdvice" pointcut-ref="pt2"/>
    </aop:aspect>
</aop:config>

Annotation code

Enable scanning of enhanced type annotation packages
First, add the target object and the object of the enhancement method to the bean
Reputation enhancement class
Rule functions for setting pointcuts
Sets the type of enhancement method

@EnableAspectJAutoProxy
@Compaonse
@Aspect
@Pointcut("execution(* *(..))")
public void pt213() {
}
@Before("pt213()")
public void before(){
}

aop principle

1 dynamic agent technology
Using dynamic agent technology to enhance the objective function at runtime
jdk proxy default
CGLIB
2 precompiling Technology
Enhance the objective function at compile time (manual write)

Topics: Java Spring