zoukankan      html  css  js  c++  java
  • 面向切面编程(Spring AOP)

    一、什么是AOP

    AOP即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术。主要体现在日志记录、性能统计、安全控制、事务处理和异常处理等。

    1.相关概念

    二、切面、切入点配置

    切面相当于一个功能的某一个类,切入点是这个类的某部分,执行到切入点时需要额外执行其他代码块,执行的代码块在引用切入点时指定。

    1.切面aspect

    代码示例:

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
        
        <bean id="aspect1" class="com.yh.aop.schema.advice.myAspect.Aspect1"></bean>
        
        <aop:config>
            <aop:aspect id="MyAspectAOP" ref="aspect1">
            </aop:aspect>
        </aop:config>
    
    </beans>

    2.切入点pointcut

    相关规则:

     代码示例:

    <?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:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
        
        <bean id="aspect1" class="com.yh.aop.schema.advice.myAspect.Aspect1"></bean>
        
        <aop:config>
            <aop:aspect id="MyAspectAOP" ref="aspect1">
                <aop:pointcut expression="execution(com.yh.aop.schema.advice.myAspect.Aspect1.*(..))" id="pointcut1"/>
            </aop:aspect>
        </aop:config>
    
    </beans>        

    三、advice配置

    1.advice的类型

  • 相关阅读:
    Spring Boot → 08:嵌入式Servlet容器自定义
    Spring Boot → 09:使用外置Servlet容器_tomcat9.0
    Spring Boot → 07:错误处理机制
    Spring Boot → 06:项目实战-账单管理系统
    Spring Boot → 05:Web开发
    zzz Objective-C的消息传递机制
    Objective-C中的SEL、IMP和Class类型(转)
    zzzObjective-C的动态特性
    zzz KVC/KVO原理详解及编程指南
    zzzzz iOS绘图教程
  • 原文地址:https://www.cnblogs.com/YeHuan/p/11106956.html
Copyright © 2011-2022 走看看