spring - Spring's AOP (Aspect Oriented Programming)

Dynamic proxy

                                        

    JDK 's dynamic proxy mode:

        Scenario design: transaction control through dynamic proxy mode

        Dynamic proxy code:

                                

        Dynamic proxy for JDK :

            1. It can realize the loose coupling of code and solve the problem of code duplication.

            2. The agent must implement the interface

            3. The dynamic proxy mode can only handle one type of business. If the business is different, the dynamic proxy needs to be rewritten.


    cglib dynamic proxy mode:

        Using the cglib dynamic proxy mode, a proxy object can be created for the proxied even if it does not implement the interface.

        Steps to create a proxy object:

                                       

                                        

                                        

            Description : The proxy object created by cglib is created in the form of binary code at the bottom layer, and the generated proxy objects are all subclasses of the target object, and proxy objects can be generated with or without interfaces .

    The difference between cglib and jdk:

        jdk creates proxy objects faster, cglib creates proxy objects slower

The dynamic proxy of          jdk needs to implement the ---- InvocationHandler interface



Introduction to the name of Spring AOP:

    Aspect : A class that completes a specific function and can do some extra operations before and after the target method . is a slice, 

    Joinpoint : A specific method called by the client is a method in the interface.

    Advice : It is the method in the aspect (additional work), and the method in the aspect class is the advice.

    Pointcut ( Pointcut) : (that is, a matching rule ) - - In the invoke method, the notification can be combined with the target method only when the pointcut is satisfied. Before using the target method, make certain judgments (such as if-else), and only when the conditions in the judgment are met can the notification be combined with the target method.

    Target Object ( Target Object) : The object that actually calls the method is the object of the method that is actually used. is the object of the delegate class.


AOP programming:

    Scene design: To use AOP to complete the control of things

    step:

        1. Import the jar package, import 5 jar packages

                                                                            

        2: Define Aspects and Notifications
                                            

        3. Write a configuration file

            Configuration order

                                        

            Note: The configuration order cannot fall, if one of the items does not need to be configured, write the next item directly

                                    

The execution principle of AOP :

    When an object is executed , it will be matched with the pointcut expression . If the match is successful, a proxy object will be created for this class (created through the dynamic proxy mode, if the target object has an interface, the jdk dynamic proxy will be used , if the target object does not have an interface) The interface uses cglib to generate proxy objects ), and the advice in the aspect is executed when the proxy object executes the method .

Pointcut expression:

    1-within() The granularity of matching control by class is relatively coarse

        Within (package name. class name)

            1. within(cn.service.UserServiceImpl)       can only match the class User ServiceImpl

            2.within(cn.service.*)     匹配当前包下的全部类(一层)

            3.within(cn.service..*)            匹配当前包下的全部子孙包

    2-execution() 控制的粒度较细,能够控制到方法和参数级别

        execution(建议使用)(返回值类型包名.类名.方法名(参数类型))

      (*)所有子类----只能包含一层,只能表示某包下面的所有子类,孙类不行。

      (.*)所有子孙类----包含多层,表示某包下面的所有子孙类。
      (..)任意的----方法类型

    案例:

        例子1<aop:pointcut expression=“execution(int service.UserServiceImpl.add())"   id="txPointcut"/>

                    该切点表达式表示:返回值为int  包名类名serviceUserServiceImpl   方法为add()的匹配规则

        例子2<aop:pointcutexpression="execution(* service.*.add())"   id="txPointcut"/>

                    规则:返回值值任意,包名service下子类的add(),只能包含一层,子孙类不行。

        例子3<aop:pointcut   expression="execution(* service..*.add())"    id="txPointcut"/>

                    规则:方法返回值任意, service包下的所有子孙类的add()

        例子4<aop:pointcut expression="execution(* service..*.add(int,String))"   id="txPointcut"/>

                    规则:返回值的类型任意   service子孙包下的add方法参数类型为int,String

        例子5<aop:pointcut   expression="execution(* service..*.add(..))"   id="txPointcut"/>

                    规则:返回值类型任意   service下的所有子孙类.add方法() (参数任意)


AOP中的通知类型:

       1.前置通知:在执行目标方法以前调用通知   MethodBeforeAdvice 接口
2.后置通知:在执行目标方法以后调用通知 AfterReturningAdvice 接口
3.异常通知:在执行目标方法执行时,出现异常后调用通知 ThrowsAdvice接口方法固定(afterThrowing)
4.环绕通知:在目标方法前后都会调用通知   MethodInterceptor接口
说明:
异常后通知可能告知 异常是什么,但是不能够处理。
环绕通知中可以对异常进行处理,功能比较强大。
5:通知中的参数:参数应该放在第一位,否则报错
环绕通知中的参数使用的是:ProceedingJoinPoint

其他的都使用:joinPoint

                joinPoint.proceed() 作用有两个

                    1.如果有下一个通知,就执行下一个通知,

                    2.如果没有下一个通知则执行目标方法。

6:通知类型的顺序:
<aop:advisor advice-ref="initAdvice" pointcut-ref="txPointcut" order="10"/>
<aop:advisor advice-ref="initAdvice2" pointcut-ref="txPointcut" order="1"/>
通知执行的顺序 可以通过order属性来控制:
前置通知中 数字越小 越先执行   1 先执行 
后置通知中 数字越大 越先执行   10先执行


通知中的参数

    1-除了环绕通知之外,其余的通知全部使用import org.aspectj.lang.JoinPoint;

        而环绕通知使用的是import org.aspectj.lang.ProceedingJoinPoint;

    规定:如果通知中有多个参数,JoinPoint或.ProceedingJoinPoint必须位于参数的第一位否则报错

    2-配置文件中的返回参数应该与通知中的参数一致


后置通知返回值问题:

                                    

    Returning="number" 表示目标方法执行后的返回值

                          

    通知中必须要包含返回值,并且名称相同


异常通知:

                                    

                                


通知的执行规则:

    1.当异常通知执行时,后置通知将不会执行。异常通知和后置通知(after-return)是互斥的。

    2.最终通知无论如何都会被执行。

    3.当目标方法执行时出现异常时,环绕通知的后半部分将不会被执行。

    4.环绕通知遇到后置通知时,如果后置通知想得到返回值,那么环绕通知必须添加return 将返回值返回

    5.当多个环绕通知同时执行时,其执行顺序是嵌套结构

        joinPoint.proceed() 作用有两个

            1.如果有下一个通知,就执行下一个通知,

            2.如果没有下一个通知则执行目标方法。





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325519603&siteId=291194637