zoukankan      html  css  js  c++  java
  • Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect

        Spring所有的切面和通知器都必须放在一个<aop:config>内(可以配置包含多个<aop:config>元素),每个<aop:config>包含pointcut,advisor和apsect元素。ps:他们必须按照这个顺序进行声明

    • <aop:pointcut>:用来定义切入点,该切入点可以重用;
    • <aop:advisor>:用来定义只有一个通知和一个切入点的切面;
    • <aop:aspect>:用来定义切面,该切面可以包含多个切入点和通知,而且标签内部的通知和切入点定义是无序的;和advisor的区别就在此,advisor只包含一个通知和一个切入点。
    <aop:config> AOP定义开始
           <aop:pointcut/>      切入点定义(0或多个)
           <aop:advisor/>      advisor定义(0或多个)
           <aop:aspect>      切面定义开始(0或多个)
                    <aop:pointcut/>            切入点定义(0或多个)
                     <aop:before/>            前置通知(0或多个)
                     <aop:after-returning/>            返回后通知(0或多个)
                     <aop:after-throwing/>            抛出异常后通知(0或多个)
                     <aop:after/>            后通知(0或多个)
                     <aop:around/>            环绕通知(0或多个)
                        <aop:declare-parents/>            引入定义(0或多个)
             <aop:aspect>      切面定义结束
    </aop:config> AOP定义结束

        <aop:config>风格的配置大量使用了Spring的自动代理机制.

    实现:

         <aop:config>
              <aop:aspect id="myAspect" ref="aBean">
                 ...
              </aop:aspect>
         </aop:config>
         
         <bean id="aBean" class="...">
               ...
         </bean>
    

    例子:

    新建2个类

    package com.aop.schema;
    /**
     * 
     * 切面类
     *
     */
    public class MyAspect {
    
    }
    
    package com.aop.schema;
    
    /**
     * 
     * 业务类
     *
     */
    public class ApsectBiz {
    
    }
    

     XML配置:

    <?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"
            xmlns:context="http://www.springframework.org/schema/context"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
             
         <bean id="myAspect" class="com.aop.schema.MyAspect"></bean>
          
         <bean id="apsectBiz" class="com.aop.schema.ApsectBiz"></bean>
          
         <aop:config>
              <aop:aspect id="myAspectAOP" ref="myAspect">
                
              </aop:aspect>
         </aop:config>
     
    </beans>
    
  • 相关阅读:
    现代C语言程序设计之数据存储
    Linux系统运维与架构设计之文件管理
    Linux系统运维与架构设计之系统基本使用
    Linux系统运维与架构设计之搭建运维环境
    Linux系统运维与架构设计之Linux概述
    Linux系统运维与架构设计技术栈
    架构师成长之道-C语言基础之C语言概述
    K3/Cloud树形单据体的rowId赋值
    K3违反内码唯一键约束
    K3修改字段名
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4633529.html
Copyright © 2011-2022 走看看