Spring_ slice-oriented programming AOP_hehe.employment.over.35.2

35.2 AOP related concepts

35.2.1 What is AOP

  • AOP : The full name is Aspect Oriented Programming, namely:Aspect-oriented programming
  • Simply put, it is to extract the repetitive code of our program, when it needs to be executed, the technology of dynamic proxy is used to enhance our existing methods without modifying the source code.

35.2.2 The role and advantages of AOP

  • effect:
    • During the running of the program, the source code is not modified to enhance the existing methods.
  • Advantage:
    • Reduce repetitive code
    • Improve development efficiency
    • Easy to maintain

35.2.3 Implementation of AOP

  • Use dynamic proxy technology

35.3 AOP in Spring

35.3.1 AOP related terms

  • Joinpoint:
    • The so-called connection points are those points that are intercepted. In spring, these points refer to methods, because spring only supports connection points of method types.
  • Pointcut:
    • The so-called entry point refers to the definition of which Joinpoint we want to intercept.
  • Advice (notification/enhancement):
    • The so-called notification means that the thing to do after intercepting Joinpoint is notification.
    • Notification types: pre-notification, post-notification, exception notification, final notification, surround notification.
  • Introduction(引介):
    • Introduction is a special kind of notification. On the premise of not modifying the class code, Introduction can dynamically add some methods or fields to the class at runtime.
  • Target:
    • The target object of the agent.
  • Weaving:
    • Refers to the process of applying enhancements to the target object to create a new proxy object.
    • Spring uses dynamic proxy weaving, while AspectJ uses compile-time weaving and class-loading weaving.
  • Proxy:
    • After a class is woven and enhanced by AOP, a result proxy class is generated.
  • Aspect:
    • It is a combination of entry point and notification (introduction).

35.4 XML-based AOP configuration steps in spring

  • 1. Give the notification bean to spring to manage it;
  • 2. Useaop:config tagshowStart AOP configuration;
  • 3. Useaop:aspectLabel indicatesConfiguration aspect;
    • id attribute: provides a unique identifier for the aspect
    • ref attribute: is specifiedId of the notification bean
  • 4. inaop: aspect tagThe internal use of the corresponding label to configure the type of notification
    • aop:before : Indicates that the pre-notification is configured
    • method attribute : used to specify which method in the Logger class is a pre-notification
    • pointcut attribute : used to specifyPointcut expression, The meaning of the expression refers to which methods in the business layer are enhanced
  • How to write the entry point expression:
    • Keywords: execution(表达式)
    • expression:
      • 访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表)
    • Standard expression writing:
      • public void com.itheima.service.impl.AccountServiceImpl.saveAccount()
    • Access modifier can be omitted
      • void com.itheima.service.impl.AccountServiceImpl.saveAccount()
    • The return value can use wildcards to indicate any return value
      • * com.itheima.service.impl.AccountServiceImpl.saveAccount()
    • The package name can use wildcards to indicate any package. But there are several levels of packages, you need to write a few*
      • * *.*.*.*.AccountServiceImpl.saveAccount())
    • The package name can be used to ..indicate the current package and its sub-packages
      • * *..AccountServiceImpl.saveAccount()
    • Both class name and method name can be used *to achieve wildcarding
      • * *..*.*()
    • parameter list:
      • You can write data types directly:
        • The basic type directly write the name int
        • Reference type write package name. The way of class name java.lang.String
      • You can use wildcards to represent any type, but there must be parameters
      • Can be used... means that there are no parameters, and there are parameters that can be of any type
    • All wildcard writing:
      • * *..*.*(..)
    • The usual way of writing the entry point expression in actual development:
      • Cut to all methods under the business layer implementation class
        • * com.itheima.service.impl.*.*(..)
  • Example:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置srping的Ioc,把service对象配置进来-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
    <!-- 配置Logger类 -->
    <bean id="logger" class="com.itheima.utils.Logger"></bean>

    <!--配置AOP-->
    <aop:config>
        <!--配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
            <!-- 配置通知的类型,并且建立通知方法和切入点方法的关联-->
            <aop:before method="printLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:before>
        </aop:aspect>
    </aop:config>

</beans>

35.5 Four general notification types

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置srping的Ioc,把service对象配置进来-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>


    <!-- 配置Logger类 -->
    <bean id="logger" class="com.itheima.utils.Logger"></bean>

    <!--配置AOP-->
    <aop:config>
        <!--配置切面 -->
        <aop:aspect id="logAdvice" ref="logger">
             <!--配置前置通知:在切入点方法执行之前执行-->
            <aop:before method="beforePrintLog" pointcut="execution(* com.itheima.service.impl.*.*(..))" ></aop:before>

            <!--配置后置通知:在切入点方法正常执行之后值。它和异常通知永远只能执行一个-->
            <aop:after-returning method="afterReturningPrintLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:after-returning>

             <!--配置异常通知:在切入点方法执行产生异常之后执行。它和后置通知永远只能执行一个-->
            <aop:after-throwing method="afterThrowingPrintLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:after-throwing>

             <!--配置最终通知:无论切入点方法是否正常执行它都会在其后面执行-->
            <aop:after method="afterPrintLog" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:after>
        </aop:aspect>
    </aop:config>

</beans>

Guess you like

Origin blog.csdn.net/qq_44686266/article/details/114656842